pindare
pindare

Reputation: 2470

Copy files recursively from a hierarchy to a single flat folder

Is it possible to copy all the files contained recursively from a specific folder and its subfolders directly to a only one flat directory without respecting anymore the source hierarchy folders?

Upvotes: 2

Views: 1130

Answers (2)

shlgug
shlgug

Reputation: 1356

Based on Olaf's answer:

$Counter = 0
Get-ChildItem -Path "./Path" -Filter * -Recurse -File |
    Foreach-Object {
        $Counter++
        Copy-Item -Path $_.FullName -Destination "./Destination/$($_.BaseName + '_' + ("{0:000}" -f $Counter) + $_.Extension)"
    }

Upvotes: 0

Olaf
Olaf

Reputation: 5232

Of course that's possible. Why not?

Counter = 0
Get-ChildItem -Path <Path> -Filter * -Recurse -File |
    Copy-Item -Destination <Destination Path> -PassThru |
        Foreach-Object{
            $counter++
            Rename-Item -Path $_.FullName -NewName ($_.BaseName + '_' + ("{0:000}" -f $Counter) + $_.Extension)
        }

Upvotes: 2

Related Questions