Question

I'm copying files to an unreliable samba share using Robocopy

The script below appears to work but I can't test it fully at this stage.

I really have two questions:

  1. Can't replicate at share level in robocopy

    I've found that robocopy must have at least \\host\share as it's target. If I just use \\host it throws ERROR 161. I basically want to replicate a bunch of folders accross into their identically named share. For example my source folders look like this:

    E:\Root\Folder1\File1
    E:\Root\Folder2\File2 
    E:\Root\Folder3\File3 
    

    and I want to copy all of that to

    \\asambashare\Folder1\File1
    \\asambashare\Folder2\File1
    \\asambashare\Folder3\File1
    

    In my source, Folder1 is simply a folder, but in my samba target, Folder1 is actually a share.

    I'm getting around it now by running a robocopy for each share.

    What I would rather do is loop through folders or find a way to convince robocopy to do it this way rather than explicitly naming all of the folders.

  2. Reliable return codes against robocopy

    For my purposes errorlevel 7 and below from robocopy is fine and the batch script should return 0

    Firstly is my current code doing that?

    Secondly if I call robocopy multiple times can I simply repeat the IF ERRORLEVEL... code after each call? I want it to try each copt regardless and if at least one failes, the script should return 1

    I believe what I have is pretty close but if someone could answer with certainty it would save me a lot of testing.

    REM target must be of the format \\host\share. 
    REM Simplest way to do this is list out each combination manually
    REM Afer calling ROBOCOPY, set Exitcode. 
    REM If at least one copy returns 8 or higher, this batch file returns 1. 
    REM Otherwise it returns 0
    
    
    SET LOGFILE=E:\SambaReplication.LOG
    SET SRC=E:\Root\asambashare
    SET TGT=\\asambashare
    SET EXITCODE=0
    
    
    ROBOCOPY %SRC%\Folder1 %TGT%\Folder1 /S /MOV /R:20 /W:600 /V /FP /LOG+:%LOGFILE%
    IF ERRORLEVEL 8 SET EXITCODE=1
    
    ROBOCOPY %SRC%\Folder2 %TGT%\Folder2 /S /MOV /R:20 /W:600 /V /FP /LOG+:%LOGFILE%
    IF ERRORLEVEL 8 SET EXITCODE=1
    
    ROBOCOPY %SRC%\Folder3 %TGT%\Folder3 /S /MOV /R:20 /W:600 /V /FP /LOG+:%LOGFILE%
    IF ERRORLEVEL 8 SET EXITCODE=1
    
    
    
    EXIT %EXITCODE%
    
Was it helpful?

Solution

Use for command to iterate over the indicated list of folders.

SETLOCAL ENABLEEXTENSIONS

SET "LOGFILE=E:\SambaReplication.LOG"
SET "SRC=E:\Root\asambashare"
SET "TGT=\\asambashare"
SET "EXITCODE=0"

FOR %%F IN (Folder1 Folder2 Folder3) DO (
    ROBOCOPY "%SRC%\%%F" "%TGT%\%%F" /S /MOV /R:20 /W:600 /V /FP /LOG+:"%LOGFILE%"
    IF ERRORLEVEL 8 SET "EXITCODE=1"
)

ENDLOCAL & EXIT /B %EXITCODE%

And if all the directories under %SRC% are to be copied, instead of enumerating them in a list, use the for /d to just retrieve the list of folders

FOR /D %%F IN ("%SRC%\*") DO (
    ROBOCOPY "%%~fF" "%TGT%\%%~nxF" /S /MOV /R:20 /W:600 /V /FP /LOG+:"%LOGFILE%"
    IF ERRORLEVEL 8 SET "EXITCODE=1"
)

Where

`%%~fF` is the full path of the folder referenced by `%%F`, and 
`%%~nxF` is the name and extension (if it has) of the folder referenced by `%%F`
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top