0
votes

I'm trying to copy all of the files with .py extension from one directory to a new one using PowerShell, but I don't want to recreate the directory structure. I want this to work recursively as the the py files are in numerous subfolders. In my destination directory, I want nothing but the py files.

Here's what I have now but it copies the directory structure too:

Get-ChildItem "C:\Johns Stuff\Python\" |
    Copy -Destination C:\Users\dread\python -Recurse -filter *.py
1

1 Answers

3
votes

How about:

Get-ChildItem "C:\Johns Stuff\Python" -File -Filter *.py -Recurse | ForEach-Object {
  Copy-Item $_.FullName C:\Users\dread\python -WhatIf
}

The -File parameter (new in PowerShell 3.0 and later) will get only files and not directories.

Of course, remove -WhatIf to actually execute the command.