Solved

Powershell scripting: Moving files from main folder to specific subfolders

Posted on 2012-03-19
2
737 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
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…

828 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