?
Solved

Powershell scripting: Moving files from main folder to specific subfolders

Posted on 2012-03-19
2
Medium Priority
?
747 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 2000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

777 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