9
@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.

Brandon
  • 456
  • 2
  • 6
  • 19

2 Answers2

8

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^"
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • 1
    "Copying Worlds:" ERROR: Invalid argument/option - '@file'. Type "FORFILES /?" for usage. Invalid switch - /q" @file backup\2014-04-28D\worlds\@file" "Copying Plugins" That does not seem to work, I tried your method before going to the method described by the For loop, they both seem to not work. – Brandon Apr 28 '14 at 23:09
  • @MegaSniperB - Whoops - I talked about the needed CMD /C in my text, then forgot to use it in my code. It should be fixed now. – dbenham Nov 18 '14 at 14:49
5

I realize this is an old post, but another way of doing it is to call a batch file and pass it the parameters that it needs. And I do not believe that there is any limitations in what can be in that batch file. For example:

forfiles /M "*.img" /C "cmd /c ProcessFile.bat @file @fdate @ftime"

joe P
  • 51
  • 1
  • 1
  • Assuming your initial batch file doesn't have any parameters, you can call itself and use "IF EXIST %1 GOTO LABEL" – Kelly Bang Oct 15 '18 at 03:54