Search my blog articles

Thursday, April 19, 2012

PowerShell script for file copy

I was faced with an issue of moving file from Subfolders to its parent folders, essentially flatten the folder structure and move all the file to a single folder.

Here is what I created in power shell script.

Ping back if you don't follow it.

foreach ($i in Get-ChildItem -recurse C:\temp\xcopy\ -exclude "*_done","*_exception" | where{$_.PsIsContainer})
{
Move-Item $i\* C:\Temp\xcopy -force
Remove-Item $i.FullName
}


Here is what it does,
1. Loops thru and gets all the folders in the c:\temp\xopy folder.
a. 'exception' does not bring back any folder with name ending with '_done' or '_exception'
2. moves all the files in that folder to any other folder.
3. Deletes the folder after it moves it.