Batch Script: Validate Date Input

后端 未结 6 1314
予麋鹿
予麋鹿 2020-12-21 06:39

I have a batch file that I use to create new project folders for clients that walks a user through the creation process and adds the appropriate files and folders to a centr

6条回答
  •  Happy的楠姐
    2020-12-21 07:04

    You could present the user with three prompts - year, month, day.

    set /p y="Please enter year (YYYY): "
    set /p m="Please enter month (MM): "
    set /p d="Please enter day (DD): "
    set date=%y%-%m%-%d%
    

    If you would like to verify the length of the input something like:

    if [%y:~4%] NEQ [] echo year entered incorrectly & goto :getDate
    

    You can assume if %y% is greater than four characters - i.e. if %y:~4% is not null - that it has been entered incorrectly (see Dos Tips on string manipulation). The same principal applies for day and month, except they should be two characters.
    Obviously for that example you would need to add the label :getDate before the user input.

提交回复
热议问题