Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

Windows batch: robocopy based on a csv file

Hello experts,
I am looking for a .bat that covers the following actions:
1.      execute “robocopy /e Source folder Destination folder” based on a csv file.
2.      csv file should be located in the same folder of .bat
3.      csv delimiter:“;”
4.      log file to generate with the various success and fail robocopy actions
I attached dummy csv file.
If you have questions, please contact me. Thank you very much for your help.
Avatar of Bill Prew
Bill Prew

How does the CSV file fit in?

You didn't attach a file...


»bp
As Bill put it, we need to know what info is comming from the CSV to be used in the robocopy.

Please attach when you have a moment, as it isn't currently on the Q
Avatar of Luis Diaz

ASKER

Hello,
Sorry for the the delay.
Please find attached dummy csv file.
config-robocopy.csv
ld16,

juts a quick question.
I assume there not really is a blank between drive and rest of path?

leflon
How about this:

@echo off
setlocal

set ConfigFile=%~dp0config-robocopy.csv
set LogFile=%~dp0log-robocopy.csv

for /f "tokens=1-2 delims=;" %%A in ('type "%ConfigFile%"') do (
    robocopy /e "%%~A" "%%~B" && (
        echo Copy from "%%~A" to "%%~B" succeeded.
    ) || (
        echo Copy from "%%~A" to "%%~B" failed.
    )
)

Open in new window


»bp
@leflon: There is not blank. I made a typo mistake.
@Bill: thank you very much for  this proposal. Unable to test it right know. I will keep you informed.
Above script would report success only if source and destination content are identical (up to date). Because of the combined values returned by RoboCopy you cannot assume "0 is good". Indeed, you need to decode the return value, see https://ss64.com/nt/robocopy-exit.html.
Depending on what you  define as "error", values 8 and 16 are important, and 4 migh be.

So, if you want to have some logging:
@echo off
setlocal EnableDelayedExpansion

set ConfigFile=%~dp0config-robocopy.csv
set LogFile=%~dp0log-robocopy.csv

for /f "tokens=1-2 delims=;" %%A in ('type "%ConfigFile%"') do (
    robocopy /e "%%~A" "%%~B"
    set /a result="%errorlevel% & 0x18"
    if !result! equ 0 (
        echo Copy from "%%~A" to "%%~B" succeeded.
    ) else (
        echo Copy from "%%~A" to "%%~B" failed.
    )
)

Open in new window

Thank you very much I will test it this week and keep you informed.
Good info Qlemo.  So until LD16 refines what conditions they might want to flag as "bad" versus "good" I'd at least propose something like this.

As it stands all ROBOCOPY output will also go to the log file as well...

@echo off
setlocal

set ConfigFile=%~dp0config-robocopy.csv
set LogFile=%~dp0log-robocopy.csv

(
    for /f "tokens=1-2 delims=;" %%A in ('type "%ConfigFile%"') do (
        echo Copying from "%%~A" to "%%~B"
        robocopy /e "%%~A" "%%~B"
        echo Copy completed with status:%ERRORLEVEL%
    )
) > "%LogFile%"

Open in new window


»bp
In that case  - if not using the log options of RoboCopy itself -  we should add /NP to its options, so the progress stuff does not get logged unnecessarily.
Thank you for your comments and sorry for the delay I tested last proposal of Bill and it works but we have a lot of information in log file. How should I proceed to add /NP option as recommended by Qlemo?
Thank you in advance for your help.
RoboCopy has a lot of options, a good choice is too use
robocopy "%%~A" "%%~B" /e /np /njh /njs /r:1 /w:1

Open in new window

How should I proceed to add /NP option

Uh, just add it to the ROBOCOPY command line in the script...

You might want to review the full list of options and command line format to make sure you have the right set of options for your particular needs...



»bp
True. Sorry for that.
@Bill: possible to add /np option  to your last proposal so I can assign solution?
Thank you again for your help.
@ LD16

take this:

robocopy /e "%%~A" "%%~B"

Open in new window


and Write it as this:

robocopy "%%~A" "%%~B" /e /np

Open in new window


I did not report the full code as I donlt think it's needed.

Please Mark Bill's full script as the solution, I'm sure he will add it later, and if not it should be easy for a future answer seeker to find this next bit.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much Bill.