none
Powershell Robocopy RRS feed

  • Question

  • 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
    Friday, October 18, 2019 8:21 PM

All replies

  • You need to ask RoboCopy questions in a different forum. This s not a systems utilities forum. Ask in the Windows General forum.

    RoboCopy cannot do what you ask.  If you need something that RC cannot so you will have to design and write a script that does what you need.  What you are trying to ask is very vague so think it through and try to formulate a question that you can turn into code.

    You say that you have a file with one row.  The post shows three separate rows.  Consider using a CSV file that is correctly designed for your needs.


    \_(ツ)_/

    Friday, October 18, 2019 8:29 PM
    Moderator
  • Did you look at the Robocopy help?

    C:\>robocopy /?
    -------------------------------------------------------------------------------
       ROBOCOPY     ::     Robust File Copy for Windows
    -------------------------------------------------------------------------------
      Started : Friday, October 18, 2019 5:15:43 PM
                  Usage :: ROBOCOPY source destination [file [file]...] [options]
                 source :: Source Directory (drive:\path or \\server\share\path).
            destination :: Destination Dir  (drive:\path or \\server\share\path).
                   file :: File(s) to copy  (names/wildcards: default is "*.*").
    
    

    Based on your post, the command that you need to execute is:

    robocopy.exe  C:\Users\User1\Desktop\Folder\   Z:\Documents\backup\Users\User1\Desktop\Folder\    file.doc
    
    That assumes that all of the entries in your text file are in the Drive:\Folder\File format. 

    You can iterate through $results with a foreach loop. 

    https://www.itechguides.com/powershell-get-content-foreach/

    .substring can be used to exclude 'C:' and split-path with allow you to pick off the folder and file names.

    $file = 'C:\temp\Accounts.csv'
    $src = Split-Path $file
    $fn = Split-Path $file -leaf 
    $dest = "Z:\Documents\backup{0}" -f $src.Substring(2)
    robocopy.exe $src $dest $fn 

    Saturday, October 19, 2019 12:00 AM
  • $file = 'C:\temp\Accounts.csv'
    $src = Split-Path $file
    $fn = Split-Path $file -leaf 
    $dest = "Z:\Documents\backup{0}" -f $src.Substring(2)
    robocopy.exe $src $dest $fn 

    This is what I am looking for , except that the results have folders and files listed. I am having a difficult time parsing the substrings and split-path commands to account for folders and files all within 1 dump.log file. 

    More clarity

     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 and folder it will migrate ;

    • C:\Users\User1\Desktop\Folder\file.doc
    • C:\Test\test.xlsx
    • C:\Program Files\Test Folder\Test.ppt
    • C:\Program Files\someapp\

    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..

    Or 

    It will mess up with the split path and split-path -leaf because some of the paths are full paths to the file and others are paths to the folder.

     

    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 


    • Edited by GregDron Saturday, October 19, 2019 3:14 PM
    Saturday, October 19, 2019 3:11 PM
  • None of that is necessary if you read and understand how RC works.

    Just send the whole folder structure to the destination and it will do what you ask.

    Robocopy c:\folderA z:\target\folderA *.xlsx /E

    This will copy the whole source folder structure into the destination folder.  "z:\target\folderA will now contain the whole "c"\folderA" structure. 

    An easy way to get the target without modifying things is to make the target the current folder:

    CD z:\target
    Robocopy c:\folderA folderA *.xlsx /E

    To copy the source folder name:

    cd z:\target
    $targetfolder = Split-Path $source -Leaf
    Robocopy $source $targetfolder *.xlsx /E

    Again - this is not a scripting issue it is a matter of learning how the Windows files system and its utility programs work.

    You also need to drop the trailing slash in folder paths or odd things will happen.

    Take time.  Read the help for C.  Experiment with the RC until you understand wat is happening.

    When you understand how to use Windows at a technical level then most things become very easy.


    \_(ツ)_/


    Saturday, October 19, 2019 4:51 PM
    Moderator
  • I don't think you need Robocopy at all. With a datafile of of filenames, just use Copy-Item:

    $Source = 'C:\somefolder\source.log'
    $DestRoot = 'Z:\Documents\Backup\'
    
    Get-Content $Source | Foreach{
       $dest = Split-Path $_.Replace('C:\',$DestRoot')
       If (!(Test-Path $dest)) {md $dest | Out-Null}
       Copy-Item $_ $dest
    }


    Keith

    Saturday, October 19, 2019 5:31 PM
  •  

    It will mess up with the split path and split-path -leaf because some of the paths are full paths to the file and others are paths to the folder.


    For each name that you process you just need to test if you have a path to a file or just a folder name.

    PS C:\WINDOWS\system32> test-path C:\temp\Accounts.csv -PathType leaf
    True
    
    PS C:\WINDOWS\system32> test-path C:\temp\ -PathType leaf
    False

    If you have a folder name, set $fn = '*.*' 

    Sunday, October 20, 2019 11:43 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, November 8, 2019 1:29 PM
    Moderator