Click here for important information about

Validate DATE format from SET /P with FINDSTR

Microsoft Windows xp professional w/serv...
January 5, 2011 at 12:20:48
Specs: Windows XP
I am trying to check for 8 numerical characters (DATE format of CCYYMMDD) using FINDSTR /R /C: and if NOT numerical format (would be nice if it could check date format also) then GOTO :EOF

REM -- Requesting parameters from user needing to be entered to find file
SET /P fdate=Position 1 of file name - What is the file date with no spaces[ie-20100101]?:
ECHO %fdate% | findstr /R /C:[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] > nul
IF NOT ErrorLevel 1 (
ECHO ERROR:"%fdate%" Contains Letters
goto :EOF

IF "%fdate%" NEQ "ECHO %fdate% | findstr /R /C:[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]> nul" goto :dateerror

Neither of the above have worked....any help would leave me very grateful!

See More: Validate DATE format from SET /P with FINDSTR

January 5, 2011 at 13:19:41
Not sure what you are doing but try this code and see if it gives you what you want...

for /f "tokens=*" %%a in ('dir C:\ /b') do (
  set T=%%~Ta
  set FileDay=!T:~3,2!
  set FileMonth=!T:~0,2!
  set FileYear=!T:~6,4!
  echo %%a Date: !FileYear! !FileMonth! !FileDay!

Not sure what you mean here...

"and if NOT numerical format"

I don't know how to get the dates to display as Characters. The only thing that could be would be the month.

Report •

January 5, 2011 at 13:24:34
Probably won't hurt to chk length too.

@echo off & setLocal EnableDELAYedeXpansion

SET /P D=no spaces[ie-20100101]?:

if "!D:~8,1!" neq "" echo too long && goto :eof
if "!D:~7,1!" equ "" echo too short && goto :eof

ECHO %D% | findstr /R /C:[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] > nul
IF ErrorLevel 1 (
ECHO ERROR:"%D%" Contains Letters && goto :eof
echo OK

Life is too important to be taken seriously.


Report •

January 6, 2011 at 02:43:45
I am sorry if I was unclear in my original post....however, I will try both of the above and let you know how it fits my needs if at all. They both look true and accurate. Let me rephrase my request.

There are thousands of files that have file names with dates ranging from 20060101 - current date (formatted CCYYMMDD) in the first position (token) of a file name delimited by (.) - [i.e 20090129.NAME2.NAME3.NAME4.TXT] and this needs to capture a user prompt asking for the specific date they want. I just want to ensure that when someone enters any of the following it fails

OR said in the positive
Only allow the format of the date parameter listed above, parsed from the date of the machine, WHICH does look like ace_omega provided that which I will test and confirm.

Now Mechanix2Go you have always been a amazement and huge help on this site and I cannot thank you enough.....however, IF in fact the syntax for checking the date range using the OS date in the manner ace_omega provided works, then wouldnt it ONLY allow for 8 characters or are you saying that it could miss that and your syntax would be required?

Report •

Related Solutions

January 6, 2011 at 03:06:52
As to ace's code, no point in guessing at date layout. There are dozens of variations, if not hundreds.

Mine checks to see if the string entered is exactly 8 chars and that they are all nums.

You could go further and chk first 2 for 20. You could chk MM for 01 throufg 12 etc.

You can start the real work where my code says echo OK.


Life is too important to be taken seriously.


Report •

January 6, 2011 at 13:37:40
Mechanix2Go -- Again impressed and amazed at the skill you bring to these forums which help a lot of us get through the day.


Report •

January 7, 2011 at 00:17:25
Thanks for the encouragement.

Life is too important to be taken seriously.


Report •

Ask Question