Dan
Dan

Reputation: 11

Batch file variables

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

Answers (2)

Magoo
Magoo

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:

  • added label :again to repeat question
  • piped LIST.TXT through FIND to show only lines containing "-" (ie. dates)
  • Set variable to NOTHING as SET/P with just enter will leave the variable UNCHANGED, not clear it.
  • Detect nothing-entered; respond and prompt again
  • match any line from list.txt to string entered - full match /b begins /e and ends /i but case-insensitive.
    • Could pipe through FIND again to match against date-type line if required.
  • If entered data not found in file, report and prompt again
  • Added restore-entry to CMS Restored message.

Upvotes: 1

RGuggisberg
RGuggisberg

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

Related Questions