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.
LVL 1
LD16Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
How does the CSV file fit in?

You didn't attach a file...


»bp
Ben Personick (Previously QCubed)Lead SaaS Infrastructure EngineerCommented:
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
LD16Author Commented:
Hello,
Sorry for the the delay.
Please find attached dummy csv file.
config-robocopy.csv
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

leflonCommented:
ld16,

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

leflon
Bill PrewIT / Software Engineering ConsultantCommented:
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
LD16Author Commented:
@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.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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

LD16Author Commented:
Thank you very much I will test it this week and keep you informed.
Bill PrewIT / Software Engineering ConsultantCommented:
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
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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.
LD16Author Commented:
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.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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

Bill PrewIT / Software Engineering ConsultantCommented:
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
LD16Author Commented:
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.
Ben Personick (Previously QCubed)Lead SaaS Infrastructure EngineerCommented:
@ 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.
Bill PrewIT / Software Engineering ConsultantCommented:
@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 /np "%%~A" "%%~B"
        echo Copy completed with status:%ERRORLEVEL%
    )
) > "%LogFile%"

Open in new window


»bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LD16Author Commented:
Thank you very much Bill.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.