Reputation: 11
So I have this batch file to backup some programs and also restore them if needed. I have it automated to create folders with the current date each time these programs are backed up. So far I have it set so you can enter a variable as one of the current folder dates that are backed up and when entered correctly it will display "CMS restore completed ! ! !" however, if nothing is entered or the wrong date is entered it will still display "CMS restore completed ! ! !"
I would to be able to use the dates in a txt file to validate this variable so only the dates shown in the txt file will allow the rest of the batch file to run. I would like it to be able to tell me that an invalid date not shown in the txt file will not allow the batch file to keep running.
Any help would be greatly appreciated. Thanks
BATCH FILE BELOW
$:CMS
@ECHO OFF
cd\1\mybackup\
CLS
ECHO.
ECHO CURRENT BACKUP DATES ON DISK
ECHO.
type list.txt
ECHO.
ECHO Input date to restore CMS and press Enter.
ECHO i.e. YYYY-MM-DD
SET /p VARIABLE=
xcopy /e /y c:\1\mybackup\%VARIABLE%\CMS c:\CMS
CLS
ECHO CMS Restored ! ! !
PAUSE
GOTO 2
VARIABLES FROM TEXT FILE BELOW
2013-08-05
2013-08-06
2013-08-07
New Folder
New Folder (2)
New Folder (3)
New Folder (4)
New Folder (5)
Upvotes: 1
Views: 460
Reputation: 79982
@ECHO OFF
cd\1\mybackup\
CLS
:again
ECHO.
ECHO CURRENT BACKUP DATES ON DISK
ECHO.
type list.txt | find "-"
ECHO.
ECHO Input date to restore CMS and press Enter.
ECHO i.e. YYYY-MM-DD
SET "VARIABLE="
SET /p VARIABLE=
if not defined variable echo Nothing entered?&goto again
type list.txt|findstr /i /b /e "%variable%" >nul
if errorlevel 1 echo Invalid date entered&goto again
xcopy /e /y c:\1\mybackup\%VARIABLE%\CMS c:\CMS
CLS
ECHO CMS Restored to %variable% ! ! !
...
Notes:
:again
to repeat question SET/P
with just enter will leave the variable
UNCHANGED, not clear it.list.txt
to string entered - full match /b
begins /e
and ends /i
but case-insensitive.
FIND
again to match against date-type line if required.CMS Restored
message.Upvotes: 1
Reputation: 4750
Try something like this to catch no date entered. I edited to add code to catch invalid date (untested). Of course it will fail in the year 2100 and you need to decide if you need to be more discretionary. Corrected!
@ECHO OFF
cd\1\mybackup\
:Retry
CLS
ECHO.
ECHO CURRENT BACKUP DATES ON DISK
ECHO.
type list.txt
ECHO.
ECHO Input date to restore CMS and press Enter.
ECHO i.e. YYYY-MM-DD
SET /p VARIABLE=
IF "%VARIABLE%"=="" ECHO.Please enter a date & PAUSE & GOTO :Retry
FOR /D %%A if (20??-*) DO IF EXIST "%%A" GOTO :DoCopy
ECHO.Please enter a valid date & PAUSE & GOTO :Retry
:DoCopy
xcopy /e /y c:\1\mybackup\%VARIABLE%\CMS c:\CMS
CLS
ECHO CMS Restored ! ! !
PAUSE
GOTO 2
Upvotes: 0