RoboCopy Source

2

I am writing a powershell script with robocopy to copy "full" unc/file name paths from a list. The issue I am experiencing is that robocopy appears to be adding a \ to the end of my source paths.

I have a C:\temp\list.txt that lists the UNC paths with file names Example: \server\folder\test.txt

My Code

$Paths = Get-Content -Path C:\Temp\List.txt
ForEach ($p in $Paths) {robocopy $p "\\Server\path\path\"}

The error that powershell is throwing is : 

   -------------------------------------------------------------------------------
  ROBOCOPY     ::     Robust File Copy for Windows                              
  -------------------------------------------------------------------------------

   Started : Monday, March 1, 2021 1:36:52 PM
   Source : \\Server\path\path\test.xls\
   Dest : \\Server\path\path\
  

Files : *.*
    
Options : *.* /DCOPY:DA /COPY:DAT /R:1000000 /W:30 

------------------------------------------------------------------------------

2021/03/01 13:36:52 ERROR 267 (0x0000010B) Accessing Source Directory 
\\Server\path\path\test.xls\
The directory name is invalid.

Does anyone know how I can stop robocopy from adding the \ at the end of my "source" location?

Thank you for your help.

powershell
robocopy
asked on Stack Overflow Mar 1, 2021 by SQLUSER • edited Mar 1, 2021 by Theo

1 Answer

4

I'm not sure the "\" is your issue. You are using an incorrect format for a Robocopy command. The files should be specified as a filter after the source and destination are specified.

RoboCopy <SourceFolderPath> <DestinationFolderPath> <File1> <File2> ...

As such you will have to rework your loop to peel the file name off the input and properly concatenate the Robocopy command, maybe something like:

$Paths = Get-Content -Path C:\Temp\List.txt
ForEach ($p in $Paths)
{    
    $Path = Split-Path -Path $p -Parent
    $File = Split-Path -Path $p -Leaf
    robocopy $Path "\\Server\path\path\" $File
}

From Robocopy.exe /?:

  Started : Monday, March 1, 2021 2:06:53 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 "*.*").

Filters do not have to be exact file names, they can leverage wildcards like *.xls to help select files. I think in your case you are somewhat constrained by the format of your input.

$Paths = Get-Content -Path C:\Temp\List.txt
ForEach ($p in $Paths)
{    
    $Path = Split-Path -Path $p -Parent
    $File = Split-Path -Path $p -Leaf
    robocopy $Path "\\Server\path\path\"
}

If you want to copy subfolders and their files there are 3 options, again quoting from the help file:

/S :: copy Subdirectories, but not empty ones.
/E :: copy subdirectories, including Empty ones.
/MIR :: MIRror a directory tree (equivalent to /E plus /PURGE).

Warning: /MIR can be dangerous as it can delete content in the destination very quickly.

The filter will apply even if you traverse subfolders. So make sure to adjust or remove it. If you want to copy everything in a selected folder simply remove the filter, though technically an asterisk works too.

I recommend really studying the help file. There are different file selection & copy options that are worth knowing.

answered on Stack Overflow Mar 1, 2021 by Steven • edited Mar 1, 2021 by Steven

User contributions licensed under CC BY-SA 3.0