Search code examples
batch-filecmdxcopy

XCopy command not working in batch file


I am trying to run a simple xcopy command at machine shutdown. When I execute the command via cmd , everything works as expected. However when I paste it in a batch file and try to run it , I encounter errors. Here is the command:

FOR /D %d in (*) DO xcopy /S /I /y /exclude:exclude.txt %d V:\SUBFOLDER\%d

The error I get is : "unexpected exclude.txt " I tried surrounding the file name with quote marks but it is didn't solve the issue. What am I doing wrong? Thank you for your help!


Solution

  • In a batch file, you need to use %%d rather than %d:

    FOR /D %%d in (*) DO xcopy /S /I /y /exclude:exclude.txt %%d V:\SUBFOLDER\%%d
    

    If you're serious about learning various Windows scripting tools, you can't go past Rob van der Woude's website. This covers a very wide range of subjects. You may also want to consider switching to Powershell since it's a great step up from the cmd.exe scripting language.