Solved

Powershell scripting: Moving files from main folder to specific subfolders

Posted on 2012-03-19
2
732 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:
bchallis 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now