Solved

Powershell scripting: Moving files from main folder to specific subfolders

Posted on 2012-03-19
2
741 Views
Last Modified: 2012-03-20
Hi,

I am new to powershell and scripting and as my first task need to accomplish the following:

1. I have a main folder c:\test\

2. The folder has a list of configuration files for multiple applications.
•      c:\test\con_companyname_appname1_config1.txt
•      c:\test\con_companyname_appname1_config2.txt
•      c:\test\con_companyname_appname2_config1.txt
•      c:\test\con_companyname_appname2_config2.txt
•      c:\test\con_companyname_appname3_config1.txt
•      c:\test\con_companyname_appname3_config2.txt
•      etc
 

5.  There are over 40-50 such appnames with corresponding config files.

4.  What the script needs to do is :

a. Create subfolders for each appname: example
•      c:\test\appname1
•      c:\test\appname2
•      c:\test\appname3
•      etc
              b: Then move the corresponding config files for each  ‘appname’ to the respective appnames subfolder.

I started learning PowerShell recently and what I have so far is:

$fileNames = Get-ChildItem | where {!$_.PsIsContainer} | Select-Object Name

ForEach($fileName in $fileNames) {
$fileName = [string]$fileName
$elements = $fileName.Split("_")
$appNames = $elements[2]
mkdir $appNames
}

This successfully creates the subfolders by appname under c:\test.
•      c:\test\appname1
•      c:\test\appname2
•      c:\test\appname3
•      etc

But I am not sure on how to move the corresponding configuration files for each appname to the respective subfolders.


5. So the final results should look like this:

•      c:\test\appname1
               con_companyname_appname1_config1.txt
               con_companyname_appname1_config2.txt
•      c:\test\appname2
              con_companyname_appname2_config1.txt
            con_companyname_appname2_config2.txt
•      c:\test\appname3
             con_companyname_appname3_config1.txt
           con_companyname_appname3_config2.txt

I would appreciate your help tremendously with helping me complete this script.

Thanks
0
Comment
Question by:jpsaini
2 Comments
 
LVL 8

Accepted Solution

by:
Brent Challis earned 500 total points
ID: 37740725
Here is a function that will move the files.  I have used the -Force switch on the Move-Item to force overwriting any existing files that may be read only.  If you want to prevent overwriting files it would be necessary to remove the -Force switch and add a test before the move:

function Move-ConfigFiles
{
    Param
    (
        [string]$BaseFolder
    )
    if (Test-Path $BaseFolder)
    {
        $filesToProcess = Get-ChildItem $BaseFolder |  Where-Object {!$_.PsIsContainer} | Select-Object Name
        foreach ($fileName in $filesToProcess)
        {
            [string]$fileName = $fileName.Name
            $appName = ($fileName).Split("_")[2]
            $appFolder = Join-Path -path $BaseFolder -ChildPath $appName
            if (!(Test-Path $appFolder))
            {
                New-Item -path $BaseFolder -name $appName -type directory | Out-Null
            }
            Move-Item -Path (Join-Path -Path $BaseFolder -ChildPath $fileName) -Destination $appFolder -Force
        }
    }
    else
    {
        Write-Warning "Unable to locate folder $BaseFolder"
    }
}

Move-ConfigFiles C:\test
0
 

Author Closing Comment

by:jpsaini
ID: 37742794
Thank you very much for the solution. It works very well and as expected. Really appreciate the quick turn around.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question