powershellcopy-item

Powershell - Copy folders alongside files in each sub folder


I have the following structure

- folder1
 - file1
 - folder2
   - file2
 - folder3
   - file3
   - file4
   - file5
- folder4
 - file6
 - folder5
   - file7
 - folder5
   - file8

I am trying to copy all the file and the folders to a different directory but what happens is that the folders (folder 1, 2, 3) are copied; within folder 3 are all the corresponding files (file3, file4, and file5) but file1 and file2 which files in folder 1 and folder 2 are left out in the copy.

Below is the powershell code snippet i use:

Copy-Item -Path c:\folder1\folder2\folder3\* -Destination d:\ -Recurse -Verbose


Solution

  • This was what had worked for me - it maybe dirty but it works;

    I iterated through the paths copied the files i needed as a complete path into the new location.

    The code is unfortunately a combination of groovy and powershell

    for(i in pathArray ) { if (i.toString() != "jobs") { . . . def copySpec = powershell(returnStdout: true, script: """ Get-ChildItem '${JENKINS_HOME}\\jobs\\${newPath}*' -Filter *.xml | Copy-Item -Destination ${remoteFolder}${newPath} -Force -PassThru -verbose """)