13

I am building a small script which should copy all .zip files to a special folder called F:\tempzip.

I tried it with Copy-Item cmdlet, but I didn't manage to do it. The script should copy all files from this folder (recursively) which are ".zip".

This is the part of the script I am talking about:

get-childitem F:\Work\xxx\xxx\xxx -recurse `
   | where {$_.extension -eq ".zip"}       `
   | copy-item F:\tempzip

What do I have to add?

3 Answers 3

13

It's a lot simpler than that. Copy-Item has its own -Recurse switch. All you have to do is:

Copy-Item F:\Work\xxx\xxx\xxx\*.zip F:\tempzip -Recurse
Sign up to request clarification or add additional context in comments.

3 Comments

Yes it does have a built in recures switch but you need to activate it using the -recurse parameter. Also trying to filter at the folder path doesn’t work very well with copy-item you are better of using the -filter param. So it would look something like this Copy-Item F:\Work\xxx\xxx\xxx\ F:\tempzip -Recurse -filter *.zip But this will copy the folder and its subfolders not just the .zip files into the destination folder
How sloppy - the whole point of my answer is that Copy-Item has its own -Recurse switch, but I left out the -Recurse switch. :) I fixed the answer.
Makes a little more sense now :)
11

When piping items to copy-item you need to tell it that "F:\tempzip" is the destination path.

| Copy-Item -Destination F:\tempzip

You can also cutout piping to the where operator by using Get-ChildItem's parameter -filter.

Get-Childitem "C:\imscript" -recurse -filter "*.zip" | Copy-Item -Destination "F:\tempzip"

Edit: Removal of unnecessary foreach loop and updated explanation.

3 Comments

I believe that adding a filter to the get-childitem commandlet is more efficient than filtering the results: Get-ChildItem -Filter *.zip -Recurse
i can't use filter, because it's just a part of my script. i copy many zip files which are created in different subfolders. so i need a recursive search. with your fullpath it worked :) thank you. with filter it didnt work. so i still have my recursive search
Copy-Item should be smart enough to handle a FileInfo object, actually, so you wouldn't need the .FullName there.
5

For whatever reason, the Copy-Item recursion didn't accomplish what I wanted, as mentioned here, and how it is documented to work. If you have a bunch of *.zip or *.jpg files in arbitrarily deep subfolder hierarchies, and you want to copy them to a single place (one flat folder, elsewhere), I had better luck with a piped command involving Get-ChildItem. Say you are currently in the folder containing the root of your search:

Get-ChildItem -Recurse -Include *.zip | Copy-Item -Destination C:\Someplace\Else

That command will copy all the files and not duplicate the folder hierarchies.

1 Comment

This is what I needed, copy all files of a type recursively to a flat structure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.