All,
I am trying to robocopy some files and retain the folder structure to a specific location.
I have a text file that contains 1 row with the following
- C:\Users\User1\Desktop\Folder\file.doc
- C:\Test\test.xlsx
- C:\Program Files\Test Folder\Test.ppt
Etc...... about 400 lines
I save those results to a variable
$results = get-content -path C:\somefolder\source.log
$Robocopy = C:\windows\system32\robocopy.exe
$Destination = Z:\Documents\Backup\
I want to iterate through that list and use robocopy to copy the files to another location and retain the folder structure.
the issue is that I would love if the destination location can somehow strip the C:\ from the results and robycopy with folder structure after the C:\ so here is an example of how I would like the final folder to look like.
- Z:\Documents\backup\Users\User1\Desktop\Folder\file.doc
- Z:\Documents\backup\Test\test.xlsx
- Z:\Documents\backup\Program Files\Test Folder\Test.ppt
I had to do something similar so I wrote a module for it. Robocopy is a great tool but the use-case at the time wouldn’t allow for robocopy. It’s all PowerShell but only works on Core on Windows at the moment.
See if this is would work:
https://github.com/aarontheadmin/PSPathSync/
Gregory,
Please provide the script you are attempting to run, you have not provided anything for us to reference. Considering I’ve used Robocopy a bunch for M&A migrations, I never ran into an issue with the drive letter being retained. What switches are you using/not using with Robocopy?
The script is huge , but let me try and be a little more specific. I am trying to migrate user data from a windows 7 VM sitting on a MAC ( Fusion ) to the MAC itself. So I am doing a scanstate.exe from the usmt toolkit to create a /listfiles log file identify all the files it will migrate to the mig file.
Since I need to dump the scanstate contents to a specific drive ( Z:) , and retain the created , and modified date stamps on all the docs. ( LOADSTATE.exe can’t redirect its extract to a specific location and USMTUtils.exe part of the USMT toolkit which can extract to any location does not retain these attributes )
The Z drive – ( This is the shared folder that the windows 7 VM has that is automatically mapped to the actual physical MAC laptop ( Documents folder ). This is a fusion feature , VMWare Wks has a similar feature.
So the /listfiles dump gives me full paths to the files it will migrate ;
- C:\Users\User1\Desktop\Folder\file.doc
- C:\Test\test.xlsx
- C:\Program Files\Test Folder\Test.ppt
I want to get-content on this log and copy these file to the Z:\ drive , but I also wanted to retain the folder structure. So I wanted the end result to be
- Z:\Documents\Users\User1\Desktop\Folder\file.doc
- Z:\Documents\Test\test.xlsx
- Z:\documnets\Program Files\Test Folder\Test.ppt
I dont know how to pass the folder structure to the robocopy so that it does not create Z:\documents\C:\Users\User1 etc..
I tried using the copy-item with the containers , which does create folders but the are not structured in anyway that makes sense.
If you could just help me with the logic ? Are you saying that Robocopy should not try and retain the drive letter into the
Try this:
$files = Get-Content -Path C:\somefolder\source.log
$backupPath = 'Z:\Documents'
foreach ($file in $files) {
# Get full path of file to be copied
$sourceDirectory = Split-Path -Path $file -Parent
# Build destination path by replacing 'C:' with 'Z:\Documents'
$destinationDirectory = $sourceDirectory -replace 'C:', $backupPath
$fileName = Split-Path -Path $file -Leaf
# Robocopy the specified file to the destination, preserving folder structure
robocopy $sourceDirectory $destinationDirectory $fileName
}
Depending on any other requirements, you may need to use other robocopy options found here: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy
This will work for you.
$file = Get-Content -Path C:\somefolder\source.log
$Destination = 'Z:\Documents\Backup\'
# Remove qualifier for each path then copy each path's file to destination
foreach ($f in $file) {
$rpath = Split-Path $f ; $rfile = Split-Path $f -Leaf
$path = (Split-Path $rpath -NoQualifier) -replace '^\\'
$newpath = Join-Path -Path $Destination -ChildPath $path
Robocopy.exe $rpath $newpath $rfile /MT:32 /R:0 /W:0 /NP
}
[quote quote=184011]This will work for you.
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.4px; left: 50px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
5
6
7
8
9
10
11
12
$file = Get-Content -Path C:\somefolder\source.log
$Destination = 'Z:\Documents\Backup\'
# Remove qualifier for each path then copy each path's contents to destination
foreach ($f in $file) {
$path = (Split-Path $f -NoQualifier) -replace '^\\'
$newpath = Join-Path -Path $Destination -ChildPath $path
Robocopy.exe $f $newpath /E /MT:32 /R:0 /W:0 /NP
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote]
This is looking great , i am seeing a few errors in the resuts
2019/10/20 03:44:05 ERROR 123 (0x0000007B) Accessing Source Directory C:\Source\Work\Document 4.rtf
The filename, directory name, or volume label syntax is incorrect.
Seems like its trying to append a \ and tries to treat it like a folder name.
I misread your question. I thought you wanted all files and subdirectories copied. The errors were received because robocopy was looking for a directory not a file to copy. I changed my code to tell robocopy what file to copy.