Discussion Locked

FOR statement "unexpected i" in batch?

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I wrote this little backup script

-------
@ECHO on
:start
::Will check day of the week and perform backup routine only if matches day specified in IF statement
FOR /F "usebackq delims==" %i IN (`date /t`) DO set date = %i
FOR /F "usebackq delims==" %i IN (`date /t`) DO set dobackup = %i
if "%dobackup%" == "*mon*" goto dobackup
else goto end

:dobackup
::moves weekly backup to weekly archive and dates one week after actual backup
move %homedrive%\weeklybackup "%homedrive%\Weekly Archive\%date%"
::copies email, favorites, cookies, my documents and desktop objects to users home directory excluding specified files.
xcopy /e /c /i /q /h /k "%userprofile%\LOCALS~1\APPLIC~1\Microsoft\Outlook" %homedrive%\weeklybackup\
xcopy /e /c /i /q /h /k "%appdata%\Microsoft\Signatures" %homedrive%\weeklybackup\
xcopy /e /c /i /q /h /k "%userprofile%\Favorites" %homedrive%\weeklybackup
xcopy /e /c /i /q /h /k "%userprofile%\Cookies" %homedrive%\weeklybackup
xcopy /e /c /i /q /h /k /exclude .mp3 "%userprofile%\*document*" %homedrive%\weeklybackup
xcopy /e /c /i /q /h /k /exclude .mp3 "%userprofile%\Desktop" %homedrive%\weeklybackup
::Dustin
:end
--------

When its run it just says "i was unexpected at this time" but seems to work when I type it into cmd and press enter, what am i doing wrong and is there a better way to do this?

2 replies

by speeder-net

It would seem that the /i switch is not required in this case because the /e has already defined the path being copied as a directory. I would try your script without the /i and see it runs correctly and copies all of the files you want.

by abarnes70

This error is being caused by the FOR command. Your command is correct when typed at the console, but when using this command in a batch file you need to precede your % variables with a double %. Therefore they would become:

FOR /F "usebackq delims==" %%i IN (`date /t`) DO set date = %%i
FOR /F "usebackq delims==" %%i IN (`date /t`) DO set dobackup = %%i

This discussion is closed to new replies.