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

2 Answers 2

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^"
Sign up to request clarification or add additional context in comments.

2 Comments

"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.
@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.
6

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"

1 Comment

Assuming your initial batch file doesn't have any parameters, you can call itself and use "IF EXIST %1 GOTO LABEL"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.