Nico
Nico

Reputation: 1197

flatten files including foldername in filename

i'd like to flatten folder-structure and in one way include each parent directory name to filename. i've tried this, but get an error:

Missing ')' in method call

I quite don't see what's the problem

(ls -r -include *.ext) | % { mv -literal $_\3 $_\3.Name.Insert(0, [String]::Format("{0} - ", $_\3.Directory.Name))}

Upvotes: 2

Views: 1619

Answers (2)

Keith Hill
Keith Hill

Reputation: 202072

Try this:

ls . -r *.ext -name | ?{!$_.PSIsContainer} | mi -dest {$_ -replace '\\','_'} -whatif

Or if on V3:

ls . -r *.ext -name -file | mi -dest {$_ -replace '\\','_'} -whatif

Remove the -whatif to actually perform the move.

Upvotes: 5

nimizen
nimizen

Reputation: 3429

Do you want to flatten the folder structure and move all of the renamed files to the root directory? For example:

$rootPath = 'C:\TempPath'
(ls $rootPath -r -include *.ext) | %{
    [string]$newFilename = $_.Name.Insert(0, [String]::Format("{0} - ", $_.Directory.Name))
    #write-host $newFilename
    mv -literal $_ "$rootPath$newFilename"
} 

Upvotes: 1

Related Questions