Batch File code

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Batch File code

Post by VegasNath »

I am using the following:

Code: Select all

@echo off
set VarYear=2010
set VarMnth=02 Feb 10
echo VarYear is: %VarYear%
echo VarMnth is: %VarMnth%
echo Exit now if this is incorrect, or press enter to continue... & pause > nul
echo Please enter the VarDate (formatted to ddmmyy) and press enter.
set /p VarDate=
echo VarDate is: %VarDate%
echo Exit now if this is incorrect, or press enter to continue... & pause > nul
echo Processing.......
I would like to somehow check that the VarDate is formatted to ddmmyy, and refuse to continue if not. How can I do this?

TIA
:wales: Nathan :uk:
There's no place like home.....

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Batch File code

Post by HansV »

Processing dates in a batch file is difficult, but if anyone, John Gray will be able to tell you how.

It would be easier if you used VBScript in a .vbs file.
Best wishes,
Hans

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: Batch File code

Post by VegasNath »

Thanks Hans, I'll wait and see if John or any other lounger pops in.

In the above example, VarDate should equal **0210 where ** will always be numeric, so I guess that would be the desired verification check.

The code above does produce the desired result, I'm just looking to add an extra layer of protection.
:wales: Nathan :uk:
There's no place like home.....

User avatar
Goshute
3StarLounger
Posts: 397
Joined: 24 Jan 2010, 19:43
Location: Salt Lake City, Utah, USA

Re: Batch File code

Post by Goshute »

If you are trying to get the system date in mmddyyyy (not mmddyy):
--------------------------------
@echo off
%comspec% /e:2048/c for %%v in (1 2) do prompt set TempDate$q$d$_ | find/v "$" >{t}.bat
for %%v in (call del) do %%v {t}.bat
set TempDate=%TempDate:/=%
set TempDate=%TempDate:Sun =%
set TempDate=%TempDate:Mon =%
set TempDate=%TempDate:Tue =%
set TempDate=%TempDate:Wed =%
set TempDate=%TempDate:Thu =%
set TempDate=%TempDate:Fri =%
set TempDate=%TempDate:Sat =%
echo %TempDate%
--------------------------------
...but I don't think that's what you are trying to do. :sad: Verifying the date is mmddyy is going to require extensive batch coding, and I agree with Hans that VBS, which has some date verification capabilities available, is the way to go. (And just to annoy you I think since we are only 10 years from the turn of the century, and you may want your work longer than just this year, yyyy is safer that yy.)
Goshute
I float in liquid gardens

User avatar
John Gray
PlatinumLounger
Posts: 5405
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: Batch File code

Post by John Gray »

It really depends on how extensive are the checks you want!
You can check quite easily that:
  1. each character is numeric, and by implication, that you have at least 6 characters
  2. dd is in the range 01-31
  3. mm is in the range 01-12
  4. yy is 10 or 11 (or more, depending how long you think your script will last!)
Just do the substringing, remembering it starts from zero, so checking for dd might be
if "%vardate:~0,2%" LSS "01" goto invalid
if "%vardate:~0,2%" GTR "31" goto invalid


I can do the whole thing if you want, but that should give you a start
John Gray

"(or one of the team)" - how your appointment letter indicates you won't be seeing the Consultant...

User avatar
John Gray
PlatinumLounger
Posts: 5405
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: Batch File code

Post by John Gray »

Ok, here's my stab at it. It can be made shorter at the expense of intelligibility.
For ease of testing I have made vardate the parameter, as in
tester 1234567
or
tester 280210

Code: Select all

:: BATch file "tester"
setlocal
:: first and only parameter should be a date in form  ddmmyy
set vardate=%1

:: ensure there are no more than 6 characters
if "%vardate:~6,1%" NEQ "" goto invalid
:: ensure there are 6 characters
if "%vardate:~5,1%" EQU "" goto invalid
:: ensure the variable is all numeric
set nonnum=
for /f "delims=0123456789" %%a in ("%vardate%") do set nonnum=%%a
if defined nonnum goto invalid
:: check on the value of dd
if "%vardate:~0,2%" LSS "01" goto invalid
if "%vardate:~0,2%" GTR "31" goto invalid
:: check on the value of mm
if "%vardate:~2,2%" LSS "01" goto invalid
if "%vardate:~2,2%" GTR "12" goto invalid
:: check on the value of yy
if "%vardate:~4,2%" LSS "10" goto invalid
if "%vardate:~4,2%" GTR "12" goto invalid
:: if we get this far, we can be pretty happy with the ddymmyy date
goto valid

:: the remaining code can do what you want...
:invalid
echo %vardate% is not in the required ddmmyy format
goto :eof

:valid
echo %vardate% appears to be valid!
goto :eof

endlocal
John Gray

"(or one of the team)" - how your appointment letter indicates you won't be seeing the Consultant...

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: Batch File code

Post by VegasNath »

John Gray wrote:It really depends on how extensive are the checks you want!
You can check quite easily that:
  1. each character is numeric, and by implication, that you have at least 6 characters
  2. dd is in the range 01-31
  3. mm is in the range 01-12
  4. yy is 10 or 11 (or more, depending how long you think your script will last!)
Just do the substringing, remembering it starts from zero, so checking for dd might be
if "%vardate:~0,2%" LSS "01" goto invalid
if "%vardate:~0,2%" GTR "31" goto invalid


I can do the whole thing if you want, but that should give you a start
Thankyou very much John.
  1. Yes, definately worth checking that.
  2. Yes.
  3. Ideally, this should equal the first two digits of the previously 'set' VarMnth
  4. Ideally, this should equal the last two digits of the previously 'set' VarYear
I'm now off to try and understand your next post, so will return in a while..... :cheers:
:wales: Nathan :uk:
There's no place like home.....