0

Good Afternoon,

I apologize if this is a basic question, but I have been struggling with it, also still very new to Powershell.

I have a network mapped folder Z:\Test.

Under Z:\Test is multiple subfolders with the same structure. I need to loop through all of the subfolders and move all PDF files if they exist in a specific location.

Z:\Test\1\Work\PDF\*.PDF - then move Z:\Test\2\Work\PDF\*.PDF - Move So on and so on.

I have tried the following, but like I said I have been struggling with it. Thanks any help

Get-ChildItem -Path Z:\temp\*\Work -File -Include "*.PDF" -Recurse | Copy-Item -Force -Destination Y:\Temp\*\Work 
  • I formated your post to ease the readability a bit. – PatricNox Jul 15 '20 at 20:56
  • How to know which file should goes into which folder? I assume *.pdf means all pdf files, but how do you want to split them up, or should all PDF files go into the same folder? – Alex_P Jul 15 '20 at 21:00
  • If the file exists in the folder on the Z:\ then it needs to be copied to Y:\ for example Z:\1\Work\PDF\1.pdf That should copy to Y:\1\Work\PDF\ – Jason Steffens Jul 15 '20 at 21:02
  • Does this answer your question? [Create directory if it does not exist](https://stackoverflow.com/questions/16906170/create-directory-if-it-does-not-exist) – Alex_P Jul 15 '20 at 21:06
  • Also, I wrote an answer on this question which might help you. https://stackoverflow.com/questions/62797873/how-to-search-for-specific-files-recursively-create-folder-and-move-file-to-it/62798284#62798284 Regarding your last comment, what problem do you actually get when trying to copy the files? Any errors? – Alex_P Jul 15 '20 at 21:07
  • I think I can create the directory in the script I don't think that part is difficult. It's the looping through the folders in the source and copying only the files in the specific subdirectory that is giving me the problems, I should have also said that there are other folders under Z:\Test\1\ that may contain PDF. I do not wish to have those copied only from the specific path – Jason Steffens Jul 15 '20 at 21:12

1 Answers1

0

I would try something like this:

$files = Get-ChildItem -Path Z:\Temp\*\Work -File -Include "*.PDF" -Recurse
foreach ($file in $files) {
    Copy-Item -Path $file.FullName -Destination Y:\Temp\*\Work -Force
}
Alex_P
  • 2,580
  • 3
  • 22
  • 37