1.

Solve : Validation yyyymmdd.txt?

Answer»

Hi all,
I've created a small executable file that searches for a file which would have the format:yyyymmdd.txt..
The user must enter SOMETHING like "20090930.txt" first and then program will search to see if the file actually exists.
However, I'd like to perform some kind of validation that checks if the user has entered this in the correct format.

Does anyone have any ideas about how to go about this?

Thanks,
LauraYour planning to write this in what langauge(Assumming Batch if you POSTED it here)?you can use vbscript to check the date validity
Code: [Select]Set objArgs = WScript.Arguments
strDate = objArgs(0)
s = Split(strDate,".")
yr = Mid(s(0),1,4)
mth = Mid(s(0),5,2)
DY = Mid(s(0),7,2)
If IsDate( mth&"/"&dy&"/"&yr ) <> 0 Then
WScript.Echo "Valid date"
Else
WScript.Echo "Invalid date"
End If

usage:
Code: [Select]C:\test>cscript /nologo checkdate.vbs 20090228.txt
Valid date

Hey there
That's exactly what I'm after.
Thanks a million. Quote from: newuserlh on November 26, 2009, 07:15:17 AM

Hey there
That's exactly what I'm after.
Thanks a million.

Hi thanks for your reply, but I've tested it out and it doesn't seem to validate the date properly.
Is there any way of doing the validation in the MS-DOS batch program itself??

Yes heres the code:
Code: [Select]@echo off
:enterdate
cls
echo Enter date to check(YYYYMMDD format)
set /p datein=?
set yearin=%datein:~4%
set monthin=%datein:~4,2%
set dayin=%datein:~6,2%

set curdate=%DATE:~4,10%

set fulldatein=%yearin%/%monthin%/%dayin%

if %fulldatein% EQU %curdate% (
echo Valid date..Begin search
pause
goto Search
) else (
echo Invalid date, please re-enter date
pause
goto enterdate
)

:Search

You can add your file search code after the Search label
Hi there
Thanks for your post.

I have tried your program.. and tried to enter a date like this:20091126 for today but it doesn't seem to work??
What is a valid date for this?when you say it doesn't validate properly, what is the error? show what you have done. Don't just say it doesn't work!!!!
You can't really validate a date like that in batch. You have to take care certain things like whether there is a february 29 or not etc... USING vbscript's date check is the simplest way to go.



Discussion

No Comment Found