Question

I have 100 folders containing a psd file:

C:\Users\Harvey\Documents\Archive011112\TFG\SG\ET\ET2013\filenameX\psd\logo.psd
C:\Users\Harvey\Documents\Archive011112\TFG\SG\ET\ET2013\filenameY\psd\logo.psd
C:\Users\Harvey\Documents\Archive011112\TFG\SG\ET\ET2013\filenameZ\psd\logo.psd
...

Each psd file contains a picture:'logo.psd'

I want to copy 'logo.psd' of 'filenameX', 'filenameY', 'filenameZ' located respectively in,

C:\Users\Harvey\Documents\Archive011112\TFG\SG\ET\ET2013\filenameX\psd
C:\Users\Harvey\Documents\Archive011112\TFG\SG\ET\ET2013\filenameY\psd
C:\Users\Harvey\Documents\Archive011112\TFG\SG\ET\ET2013\filenameZ\psd
...

to another folder,

C:\Users\Harvey\Documents\Archive011112\TFG\SG\ET\Logos\

and rename 'logo' file with 'logo_parentfoldername' this way: 'logo_filenameX','logo_filenameY', 'logo_filenameZ'.

End Result:

C:\Users\Harvey\Documents\Archive011112\TFG\SG\ET\Logos\logo_filenameX.psd
C:\Users\Harvey\Documents\Archive011112\TFG\SG\ET\Logos\logo_filenameY.psd
C:\Users\Harvey\Documents\Archive011112\TFG\SG\ET\Logos\logo_filenameZ.psd
....

Hope it's clear!

Was it helpful?

Solution

Something like this should do the trick.

You want to use Get-ChildItem with -Recurse to grab all those files within each subdirectory.

Get-ChildItem -Path $dir -Recurse

But filter your results to only those with the .psd extension.

|?{$_.Extension -eq ".psd"} 

Then for each of those files get the name of the parent's parent directory.

$_.Directory.Parent.Name

And finally copy the file to our new file destination.

Copy-Item $_.FullName $copyto 

So the end product looks something like this:

$dir = "C:\source\" 
$destination = "C:\destination\"

Get-ChildItem -Path $dir -Recurse | ?{$_.Extension -eq ".psd"} | % {
$copyto = $destination + $_.Directory.Parent.Name + ".psd"
Copy-Item $_.FullName $copyto 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top