Batch Script: Validate Date Input

后端 未结 6 1316
予麋鹿
予麋鹿 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条回答
  •  庸人自扰
    2020-12-21 07:12

    I made a function :getdate who test the date try it; It will test if the separators are correct, the value range for thr month and the day and if the values are NUM.

    @ECHO OFF
    setlocal enabledelayedexpansion
    
    :GetDate
    set /p $D=Enter a date (MM-DD-YYYY) :
    
    set $separate=%$d:~2,1% %$d:~5,1%
    
    for %%a in (%$separate%) do (if "%%a" neq "-" (echo Wrong Separator : %%a
                                                   pause
                                                   goto:Getdate))
    
    
    set $D=%$D:-= %
    set $c=1
    
    for %%a in (%$d%) do (call:test !$c! %%a
                          set /a $c+=1)
    
    if !$c!==4 set $DateOK=%$month%-%$day%-%$Year%
    echo This DATE IS OK %$dateOK%
    exit /b
    
    :test
    if %1 equ 1 (echo %2 | findstr [0-9][0-9]
                 if errorlevel 1 (echo Unvalid value for Month [NOT NUM]: %2
                                  pause
                                  goto:getdate)
    
                 if %2 GTR 12 (echo Unvalid value for Month [VALUR RANGE +]: %2
                                    pause
                                    goto:getdate)
                  if %2 LSS 1 (echo Unvalid value for Month [VALUR RANGE -]: %2
                                    pause
                                    goto:getdate)
                  set $month=%2) 
    
    
    
    if %1==2  (echo %2 | findstr [0-9][0-9]
               if errorlevel 1 (echo Unvalid value for Day [NOT NUM]: %2
                                pause
                                goto:getdate)
               if %2 GTR 31 (echo Unvalid value for Day [VALUR RANGE +] : %2
                                  pause
                                  goto:getdate)
               if %2 LSS 01 (echo Unvalid value for Day [VALUE RANGE -]: %2
                                  pause
                                  goto:getdate)
               set $day=%2)
    
    
    if %1==3  (echo %2 | findstr [0-9][0-9][0-9][0-9]
               if errorlevel 1 (echo Unvalid value for Year [NOT NUM] : %2
                                pause
                                goto:getdate)
             set $Year=%2)
    

提交回复
热议问题