Solved

Powershell scripting: Moving files from main folder to specific subfolders

Posted on 2012-03-19
2
743 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

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 …
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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