I am currently working on a batch file to delete the previous file, copy any file created "Today" and rename to a generic name. This is for Restoring a DB file on a daily basis from the backup file that has been created today from another instance.
@echo off
echo deleting previous file.....
del E:\DataRestore\DBRestore.bak /f
FOR /F %%I IN ('dir /B /O:D *.bak') DO SET filename=%%I
FOR %%f IN (%filename%) DO SET temp=%%~tf
set mydate=%temp:~6,4%%temp:~0,2%%temp:~3,2%
FOR /F "tokens=1-4 delims=/ " %%I IN ('DATE /t') DO SET mydate2=%%L%%J%%K
ECHO last modified file date is %mydate%
if %mydate2%==%mydate%
RENAME %%I DBRestore.bak
DO COPY %%I E:\DataRestore & exit /b
I am getting a syntax error.
if %mydate2%==%mydate%
RENAME %%I DBRestore.bak
DO COPY %%I E:\DataRestore & exit /b
The above lines are not valid batch syntax.
if
with a condition, but no action.%%I
) outside a loop.do
is only a valid keyword in for
loops.Also, you don't need to determine the current date via date /t
. There's a variable %DATE%
providing that information.
Your script should probably look somewhat like this:
@echo off
setlocal EnableDelayedExpansion
echo deleting previous file.....
del E:\DataRestore\DBRestore.bak /f
set mydate2=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%
for /F %%I in ('dir /B /O:D *.bak') do (
set "filename=%%~fI"
set "temp=%%~tI"
set "mydate=!temp:~6,4!!temp:~0,2!!temp:~3,2!"
echo last modified file date is !mydate!
if "%mydate2%"=="!mydate!" (
copy "!filename!" E:\DataRestore\DBRestore.bak
exit /b
)
)