Reputation: 31
I need a little help with one batch script and xcopy
. I have a folder with a lot of different folders inside. For example: "Test" is the primary directory and inside I have New folder 1
, New folder 2
, New folder 3
... Every week these folders have to copy to different servers.
My script is:
if exist "%dir%\New folder 1" (
xcopy %dir%\New folder 1 C:\Users\user1\New folder1 /i /q /s /y /z
echo New folder 1 copied successfully
)
if exist "%dir%\New folder 2" (
xcopy %dir%\New folder 2 C:\Users\user1\New folder2 /i /q /s /y /z
echo New folder 2 copied successfully
)
I need to update my script to show me the errors on cmd
when xcopy
cannot copy some files.
Upvotes: 1
Views: 11865
Reputation: 37569
Xcopy returns an error code for the last action, you can check this with the &&
and ||
operator:
@echo off &setlocal
if exist "%dir%\New folder 1" xcopy "%dir%\New folder 1" "C:\Users\user1\New folder1" /i /q /s /y /z && echo New folder 1 copied successfully || echo copied NOT successfully
if exist "%dir%\New folder 2" xcopy "%dir%\New folder 2" "C:\Users\user1\New folder2" /i /q /s /y /z && echo New folder 2 copied successfully || echo copied NOT successfully
Note: always put double quotes around path names with space(s).
Upvotes: 0
Reputation: 4629
I hope I'm understanding this correctly, let me know if I'm wrong.
XCopy should throw errors itself if there's an issue to do with permissions etc. If it's just that something doesn't exist, just add an ELSE
to your IF
statements and echo whatever error you want.
Sidenote: You have a typo in your script, you're checking the existence of New Folder 1
twice, not New Folder 1
and New Folder 2
. Not sure if that's just on SO, but I thought I'd tell you.
Upvotes: 0