Search code examples
for-loopbatch-fileforfiles

How to use multiple commands in batch using forfiles command?


@ECHO OFF

SET backdir=backup
SET snapshotdir=snapshots
SET worldprefix=world_
SET itdate=%date:~10,4%-%date:~4,2%-%date:~7,2%
SET hour=%time:~0,2%

IF "%hour:~0,1%" == " " SET hour=0%hour:~1,1%

echo Current date: %itdate%. Current hour: %hour%. Current Minute:Second: %time:~3,2%:%time:~6,2%

    forfiles /m "%worldprefix%*" /c (
        echo Copying World: @path
        cmd /c xcopy /e /c /h /i /v /r /y /q @file %snapshotdir%\@file\%itdate%-%hour%-%time:~3,2%-%time:~6,2%
        cmd /c xcopy /e /c /h /i /v /r /y /q @file %backdir%\%itdate%D\worlds\@file
     )

echo Copying Plugins
xcopy /e /c /h /i /v /r /y /q plugins %backdir%\%itdate%D\plugins\
xcopy /e /c /h /i /v /r /y /q %backdir%\%itdate%D %backdir%\%itdate%-%hour%H\

echo Backup Complete (assuming no errors above).  Attempting to remove old files..
forfiles /p "%snapshotdir%" /c "cmd /c rmdir /s /q @path" /d -7
forfiles /p "%backdir%" /m "*H" /c "cmd /c rmdir /s /q @path" /d -2
forfiles /p "%backdir%" /m "*D" /c "cmd /c rmdir /s /q @path" /d -14

PAUSE

I am trying to copy all files with "world_" as a prefix. I run into a problem when I try to use multiple commands in a loop. I have attempted to write the batch script I want above.


Solution

  • The two commands have absolutely nothing in common, so no, you cannot use parentheses like that.

    You must execute all the commands within a single CMD /C. You can concatenate commands on one line using &. I've defined a simple XCOPY "macro" to save a bit of typing.

    set XCOPY=xcopy /e /c /h /i /v /r /y /q"
    forfiles /m "%worldprefix%*" /c "cmd /c echo Copying World: @path&%XCOPY% @file %snapshotdir%\@file\%itdate%-%hour%-%time:~3,2%-%time:~6,2%&%XCOPY% @file %backdir%\%itdate%D\worlds\@file"
    

    If you escape the quotes, then you can use line continuation to split the logical line accross multiple lines. But then you must also escape the &.

    set XCOPY=xcopy /e /c /h /i /v /r /y /q"
    forfiles /m "%worldprefix%*" /c ^"cmd /c ^
    echo Copying World: @path ^&^
    %XCOPY% @file %snapshotdir%\@file\%itdate%-%hour%-%time:~3,2%-%time:~6,2% ^&^
    %XCOPY% @file %backdir%\%itdate%D\worlds\@file^"
    

    Or you could put the & in the front of the line so that you don't need to escape it. The line continuation also escapes the first character of the next line:

    set XCOPY=xcopy /e /c /h /i /v /r /y /q"
    forfiles /m "%worldprefix%*" /c ^"cmd /c ^
    echo Copying World: @path^
    &%XCOPY% @file %snapshotdir%\@file\%itdate%-%hour%-%time:~3,2%-%time:~6,2%^
    &%XCOPY% @file %backdir%\%itdate%D\worlds\@file^"