Advertisement
T3RRYT3RR0R

Batch Input Filter For Safe Setting of Variables

Dec 10th, 2019
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 14.12 KB | None | 0 0
  1. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Date Comparison and File Log Function
  2. :::
  3.     @ECHO OFF & mode 1000 & Title File Age Checking Function
  4. :::
  5. ::: For use on Windows 10. To use on Older Systems, remove all instances of ANSI Color Codes IE: 
  6.  
  7. :DropDir
  8.  
  9.     Setlocal EnableDelayedExpansion
  10.     cls & Color 02
  11.  
  12.     Set "LogLoc=%~dp0"
  13.     Set "LogLoc=%LogLoc%logs\"
  14.     IF NOT EXIST "%LogLoc%" md "%LogLoc%"
  15.  
  16.     Set "format=DDMM"
  17.     Set "LogDate=%DATE%"
  18.     Set "LogDate=%LogDate:~4,10%"
  19.     Set "FormatID=false"
  20.     Call :ProcessDate %LogDate%
  21.  
  22.     IF %FD_D% GTR 12 (
  23.         ECHO( Date Format Detected as DDMMYYYY
  24.         Set "FormatID=true"
  25.     )
  26.  
  27.     IF "%FormatID%"=="false" (
  28.         Set "format=MMDD"
  29.         Call :ProcessDate %LogDate%
  30.         IF %FD_D% GTR 12 (
  31.             ECHO( Date Format Detected as MMDDYYYY
  32.             Set "FormatID=true"
  33.         )
  34.     )
  35.  
  36.     IF "%FormatID%"=="false" (
  37.         ECHO(  Select the Date Format of your device: [M] MMDDYYYY [D] DDMMYYYY
  38.         CHOICE /N /C dm /M "" >nul
  39.         IF %ERRORLEVEL%==2 (
  40.             Set "format=MMDD"
  41.         ) else (
  42.             Set "format=DDMM"
  43.         )
  44.     )
  45.  
  46.     Set "LogDate=%Modified: =%
  47.  
  48.     ECHO(
  49.     ECHO( Select Search Type: (R)ecursive (F)older Only
  50.     CHOICE /N /C rf /M "" >nul
  51.     IF ERRORLEVEL 2 (
  52.         Set "Search=FolderOnly"
  53.     ) else (
  54.         Set "Search=Recursive"
  55.     )
  56.    
  57.     ECHO( Select Output Type: (D)isplay Only (L)og and Display
  58.     CHOICE /N /C ld /M "" >nul
  59.     IF ERRORLEVEL 2 (
  60.         Set "output=Display"
  61.     ) else (
  62.         Set "output=Log"
  63.         IF Exist "!LogLoc!fileage!LogDate!.log" (
  64.             ECHO( A Log Already Exists For Today.
  65.             ECHO( Overwrite? Y/N
  66.             CHOICE /N /C YN /M "" >nul
  67.             IF ERRORLEVEL 2 (
  68.                 ECHO(Log Will Not be Stored.
  69.                 Set "output=Display"
  70.             ) else (
  71.                 DEL /Q "!LogLoc!fileage!LogDate!.log" && ECHO( Log Removed.
  72.             )
  73.         )
  74.     )
  75.    
  76.     ECHO(
  77.  
  78.     Set "C_Prompt=Drag and Drop or Enter Directory }"
  79.     (Set /p "AgeDir=!C_Prompt!<")
  80.  
  81.     IF Not Exist "!AgeDir!" (
  82.         cls & Color 04
  83.         ECHO Invalid Directory Entered.
  84.         Timeout 1 > nul
  85.         Endlocal & GOTO :DropDir
  86.     )
  87.    
  88.     ECHO(
  89.  
  90. REM Get safe user Input for Date Comparison
  91.  
  92. :Day_Select
  93.  
  94.     Call :InputVar_Type DD number
  95. REM Remove leading Zeros
  96.     Set /A DD = 100!DD! %% 100
  97.  
  98.     For /L %%i in (1,1,31) DO (
  99.         IF "%%i"=="!DD!" GOTO :Month_Select
  100.     )
  101.    
  102.     ECHO(Invalid Day.
  103.     GOTO :Day_Select
  104.  
  105. :Month_Select
  106.  
  107.     Call :InputVar_Type MM number
  108. REM Remove leading Zeros
  109.     Set /A MM = 100!MM! %% 100
  110.  
  111.     For /L %%i in (1,1,12) DO (
  112.         IF "%%i"=="!MM!" GOTO :Year_Select
  113.     )
  114.        
  115.     ECHO(Invalid Month
  116.     GOTO :Month_Select
  117.  
  118. :Year_Select
  119.  
  120.     Call :InputVar_Type YYYY number
  121.     Call :ConvertJulian %DD% %MM% %YYYY% CompDate
  122.  
  123.     For /L %%i in (1985,1,2500) DO (
  124.         IF %%i==!YYYY! GOTO :Fetch_Data
  125.     )
  126.    
  127.     ECHO(Invalid Year
  128.     GOTO :Year_Select
  129.  
  130. :Fetch_Data
  131.  
  132.     cls
  133.     Color 06
  134.     CALL :%search% %DD% %MM% %YYYY% "%AgeDir%"
  135.     ECHO  & REM The preceeding Character is the BEL character, Not the ANSI ESC character used for Color Codes.
  136.     ECHO(  Comparison Complete.  Next...
  137.     pause >nul
  138.     ENDLOCAL & GOTO :DropDir
  139.  
  140. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: *** End Main and Commence Subroutines ***
  141.  
  142. :FolderOnly
  143.  
  144.     PUSHD "%~4"
  145.  
  146.     For %%A in (*.*) do (
  147.         Set "extType=%%~xA"
  148.         IF NOT "!extType!"=="" (
  149.             CALL :FolderCheck "%%~dpA"
  150.             CALL :ProcessDate "%%~tA"
  151. REM Format File information into Columns using Set Substring Modification and white-space
  152.             Set "File_N=%%~nxA"
  153.             Set "File_N=!File_N!                                             "
  154.             Set "File_N=!File_N:~,45!"
  155. REM Set "Modified=!Modified:~,10!"
  156.             Set "File_S=%%~zA"
  157.             Set "File_S=!File_S!               "
  158.             Set "File_S=!File_S:~,15!"
  159.             CALL :ConvertJulian !FD_D! !FD_M! !FD_Y! FileDate
  160.             CALL :DateDifference !FileDate! !CompDate! Difference
  161.  
  162.             IF !Difference!==0 (
  163.                 CALL :MatchFile
  164.             )
  165.  
  166.             IF !Difference! LSS 0 (
  167.                 CALL :NewerFile
  168.             )
  169.  
  170.             IF !Difference! GTR 0 (
  171.                 CALL :OlderFile
  172.             )      
  173.         Set "InFolder=%%~dpA"
  174.         )
  175.     )
  176.    
  177.     POPD
  178.     exit /b
  179.  
  180. :Recursive
  181.  
  182.     PUSHD "%~4"
  183.  
  184.     For /R %%A in (*.*) do (
  185.         Set "extType=%%~xA"
  186.         IF NOT "!extType!"=="" (
  187.             CALL :FolderCheck "%%~dpA"             
  188.             CALL :ProcessDate "%%~tA"
  189. REM Format File information into Columns using Set Substring Modification and white-space
  190.             Set "File_N=%%~nxA"
  191.             Set "File_N=!File_N!                                             "
  192.             Set "File_N=!File_N:~,45!"
  193. REM Set "Modified=!Modified:~,10!"
  194.             Set "File_S=%%~zA"
  195.             Set "File_S=!File_S!               "
  196.             Set "File_S=!File_S:~,15!"
  197.             CALL :ConvertJulian !FD_D! !FD_M! !FD_Y! FileDate
  198.             CALL :DateDifference !FileDate! !CompDate! Difference
  199.  
  200.             IF !Difference!==0 (
  201.                 CALL :MatchFile "%%A"
  202.             )
  203.  
  204.             IF !Difference! LSS 0 (
  205.                 CALL :NewerFile "%%A"
  206.             )
  207.  
  208.             IF !Difference! GTR 0 (
  209.                 CALL :OlderFile "%%A"
  210.             )      
  211.         Set "InFolder=%%~dpA"
  212.         )
  213.     )
  214.    
  215.     POPD
  216.     exit /b
  217.  
  218. :FolderCheck
  219.    
  220. REM Doubleqouting Of Directory Path Is Required
  221.  
  222.     IF Defined InFolder (
  223.         IF NOT "!InFolder!"=="%~1" (
  224.             ECHO(
  225.             ECHO("%~1"
  226.             ECHO(
  227.             IF "!output!"=="Log" (
  228.                 ECHO.>>!LogLoc!fileage!LogDate!.log
  229.                 ECHO("%~1">>!LogLoc!fileage!LogDate!.log
  230.                 ECHO.>>!LogLoc!fileage!LogDate!.log
  231.             )
  232.         )
  233.     ) Else (
  234.         ECHO(
  235.         ECHO("%~1"
  236.         ECHO(
  237.         IF "!output!"=="log" (
  238.             ECHO.>>!LogLoc!fileage!LogDate!.log
  239.             ECHO("%~1">>!LogLoc!fileage!LogDate!.log
  240.             ECHO.>>!LogLoc!fileage!LogDate!.log
  241.         )
  242.     )              
  243.  
  244.     Exit /b
  245.  
  246. :OlderFile
  247.  
  248.     Set "Diff_Display=!Difference!               "
  249.     Set "Diff_Display=!Diff_Display:~,15! Days Older.                              "
  250.     Set Diff_Display=!Diff_Display:~,35!
  251.     ECHO(!File_N! !Diff_Display! Modified: !Modified! Size: !File_S! Bytes
  252.  
  253. REM Insert Command/s below here if desired to act on files with an Older date. Filepath passed with Parameter 1
  254.  
  255.     IF "!output!"=="Log" (
  256.         ECHO(!File_N! !Diff_Display! Modified: !Modified! Size: !File_S! Bytes>>!LogLoc!fileage!LogDate!.log
  257.     )
  258.     Exit /b
  259.  
  260. :NewerFile
  261.  
  262.     Set "Diff_Display=!Difference:~1,20!               "
  263.     Set "Diff_Display=!Diff_Display:~,15! Days Newer.                              "
  264.     Set Diff_Display=!Diff_Display:~,35!
  265.     ECHO(!File_N! !Diff_Display! Modified: !Modified! Size: !File_S! Bytes
  266.  
  267. REM Insert Command/s below here if desired to act on Newer files. Filepath passed with Parameter 1
  268.  
  269.     IF "!output!"=="Log" (
  270.     ECHO(!File_N! !Diff_Display! Modified: !Modified! Size: !File_S! Bytes>>!LogLoc!fileage!LogDate!.log
  271.     )
  272.     Exit /b
  273.  
  274. :MatchFile
  275.  
  276.     Set "Diff_Display=!Difference!               "
  277.     Set "Diff_Display=!Diff_Display:~,15! Days. Date Match.                              "
  278.     Set Diff_Display=!Diff_Display:~,35!
  279.     ECHO(!File_N! !Diff_Display! Modified: !Modified! Size: !File_S! Bytes
  280.  
  281. REM Insert Command/s below here if desired to act on files that match the Date. Filepath passed with Parameter 1
  282.  
  283.     IF "!output!"=="Log" (
  284.         ECHO(!File_N! !Diff_Display! Modified: !Modified! Size: !File_S! Bytes>>!LogLoc!fileage!LogDate!.log
  285.     )
  286.     Exit /b
  287.  
  288. :ProcessDate
  289.  
  290.     Set "F_D=%~1"
  291.     Set "F_D=!F_D:/=!"
  292.     Set "F_D=!F_D:~0,8!"
  293.     Set "FD_Y=!F_D:~4,4!"
  294.  
  295.     IF "%format%"=="DDMM" (
  296.     REM DDMM Format:
  297.         Set "FD_D=!F_D:~0,2!"
  298.         Set "FD_M=!F_D:~2,2!"
  299.         Set "Modified=!FD_D! !FD_M! !FD_Y!"
  300.  
  301. REM Remove leading Zeros to use for LSS LEQ EQU GTR GEQ comparisons
  302.         Set /A FD_D = 100!FD_D! %% 100
  303.         Set /A FD_M = 100!FD_M! %% 100
  304.     ) else (
  305. REM MMDD Format:
  306.         Set "FD_M=!F_D:~0,2!"
  307.         Set "FD_D=!F_D:~2,2!"
  308.         Set "Modified=!FD_M! !FD_D! !FD_Y!"
  309. REM Remove leading Zeros to use for LSS LEQ EQU GTR GEQ comparisons
  310.         Set /A FD_M = 100!FD_M! %% 100
  311.         Set /A FD_D = 100!FD_D! %% 100
  312.     )
  313.  
  314. exit /b
  315.  
  316. :ConvertJulian
  317.  
  318.     Setlocal
  319.     Set "Day=%1"
  320.     Set "Month=%2"
  321.     Set /a "MonthCv=( %Month% - 14 ) / 12"
  322.     Set /a "YearCv=%3 + 4800"
  323.     Set /a "Julian=1461 * ( %YearCv% + %MonthCv% ) / 4 + 367 * ( %Month% - 2 -12 * %MonthCv% ) / 12 - ( 3 * ( ( %YearCv% + %MonthCv% + 100 ) / 100 ) ) / 4 + %Day% - 32075"
  324.  
  325.     (
  326.     Endlocal
  327.     Set "%~4=%Julian%"
  328.     exit /b
  329.     )
  330.  
  331. :DateDifference
  332.  
  333.     SetLocal
  334.     Set /a "tmp=%~2 - %~1"
  335.     (
  336.     Endlocal
  337.     Set "%3=%tmp%"
  338.     exit /b
  339.     )
  340.  
  341. :::
  342. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: End File Age Comparison Function
  343.  
  344. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Input Validation Function by T3RRY
  345. ::: Usage: Parameter 1 is Mandatory. Parameters 2 and 3 are optional.
  346. ::: When Needing User Input:
  347. ::: For General Input - CALL :InputVar_Type <VarName>
  348. ::: To Define Input permitted Character types - CALL :InputVar_Type <VarName> <PermitType>
  349. ::: To Restrict length of Input - CALL :InputVar_Type <VarName> <PermitType> <MaxLength>
  350. ::: Where <VarName> is replaced with the Variable name to be Set
  351. ::: And <PermitType> is replaced with letter, number or alphanumerical
  352. ::: <MaxLength> is The Maximum Allowed String length.
  353.  
  354. :InputVar_Type
  355.  
  356.     (
  357.     SETLOCAL EnableDelayedExpansion
  358.  
  359. REM Assign the variable (Passed by call Argument) to be set and how to test it
  360.  
  361.     SET "testinput=%~1"
  362.     SET "permitted=%~2"
  363.     SET "MaxLength=%~3"
  364.     )
  365.  
  366. ::: SYNTAX ::: Tests Parameter Usage is Correct
  367.  
  368.     IF NOT DEFINED testinput (
  369.         ECHO(Error. CALL :InputVar_Type Function Not used correctly. Parameter 1 for Variable Name is Required.
  370.         ECHO( Parameter 2 for Character Types is Optional, though strongly Recommended.
  371.         ECHO( Parameter 3 for String Length is Optional.
  372.         Pause
  373.         Exit
  374.     )
  375.    
  376.     IF NOT "!testinput!"=="!testinput: =!" (
  377.         ECHO(Error. CALL :InputVar_Type Parameter 1 : %testinput% is Not an acceptable Variable Name. Variable names Cannot Contain Spaces.
  378.         Pause
  379.         EXIT
  380.     )
  381.  
  382.     REM Parameter 2 for Permitted Character types is Optional, However risks unwanted characters being input.
  383.     IF DEFINED permitted Set "P_True=0"
  384.     For %%V IN (number,letter,alphanumerical) Do (IF /I "!permitted!"=="%%V" Set /a P_True+=1)
  385.    
  386.     IF "!P_True!"=="0" (
  387.         ECHO(Error. Incorrect Parameter used for CALL :InputVar_Type 2nd Parameter. %permitted% Is Not acceptable.
  388.         ECHO(Correct Parameters are: "number" OR "letter" OR "alphanumerical"
  389.         Pause
  390.         Exit
  391.     )
  392.  
  393.     REM Parameter 3 for MaxLength is Optional. The below Line ensures a value still exists for Later Usage.
  394.     IF NOT DEFINED MaxLength Set "MaxLength=9999"
  395.    
  396.     echo.!MaxLength!| findstr /R "[^0-9]" >nul 2>nul
  397.     IF NOT errorlevel 1 (
  398.         ECHO(Error. CALL :InputVar_Type Paramater 3 is Not Defined correctly. %MaxLength% needs to be a Number
  399.         Pause
  400.         Exit
  401.     )
  402.  
  403. ::: Expand testinput to the Name of the Variable to be Assigned, Ensures Undefined
  404.  
  405.     SET %testinput%=
  406.  
  407. ::: Ensure Undefined Value for Input
  408.  
  409.     SET input=
  410.  
  411. :Get_Input
  412.  
  413. ::: Exit Filter once Acceptable Variable is Set
  414.  
  415.     IF DEFINED input GOTO return
  416.  
  417. ::: Custom Prompt regarding input required.
  418.  
  419.     Set "C_Prompt= Enter Value for %testinput% }> "
  420.    
  421.     SET /P "input=!C_Prompt!"
  422.  
  423. ::: Test to ensure Variable defined. Needed here to prevent environment variable not defined message ahead of next test Should the variable not be defined.
  424.  
  425.     IF NOT DEFINED input GOTO invInput
  426.  
  427. ::: Test for Doublequotes and reset variable if present
  428.  
  429.     SET Input | FIND """" >NUL
  430.     IF NOT ERRORLEVEL 1 SET Input=
  431.  
  432. IF NOT DEFINED input GOTO invInput
  433.  
  434. :: REM Block Tilde
  435.  
  436.     SET Input | FIND "~" >NUL
  437.     IF NOT ERRORLEVEL 1 SET Input=
  438.  
  439.     IF NOT DEFINED input GOTO invInput
  440.  
  441. ::: Test for Spaces
  442.  
  443.     IF NOT "%input%"=="%input: =%" GOTO invInput
  444.  
  445. ::: To permit symbols REM out permitted Symbol comparisons and Call Filter without Second Parameter.
  446. ::: Test for all other standard Symbols.
  447.  
  448.     IF NOT "%input%"=="%input:&=%" GOTO invInput
  449.     IF NOT "%input%"=="%input:(=%" GOTO invInput
  450.     IF NOT "%input%"=="%input:)=%" GOTO invInput
  451.     IF NOT "%input%"=="%input:<=%" GOTO invInput
  452.     IF NOT "%input%"=="%input:>=%" GOTO invInput
  453.     IF NOT "%input%"=="%input:^=%" GOTO invInput
  454.     IF NOT "%input%"=="%!!|%" GOTO invInput
  455.     IF NOT "%input%"=="%input:{=%" GOTO invInput
  456.     IF NOT "%input%"=="%input:}=%" GOTO invInput
  457.     IF NOT "%input%"=="%input:?=%" GOTO invInput
  458.     IF NOT "%input%"=="%input:!=%" GOTO invInput
  459.     IF NOT "%input%"=="%input:`=%" GOTO invInput
  460.     IF NOT "%input%"=="%input:'=%" GOTO invInput
  461.  
  462.     IF NOT "%input%"=="%input:]=%" GOTO invInput
  463.     IF NOT "%input%"=="%input:[=%" GOTO invInput
  464.     IF NOT "%input%"=="%input:#=%" GOTO invInput
  465.     IF NOT "%input%"=="%input:+=%" GOTO invInput
  466.     IF NOT "%input%"=="%input:-=%" GOTO invInput
  467.     IF NOT "%input%"=="%input:/=%" GOTO invInput
  468.     IF NOT "%input%"=="%input:\=%" GOTO invInput
  469.     IF NOT "%input%"=="%input:$=%" GOTO invInput
  470.     IF NOT "%input%"=="%input:@=%" GOTO invInput
  471.     IF NOT "%input%"=="%input:,=%" GOTO invInput
  472.     IF NOT "%input%"=="%input:.=%" GOTO invInput
  473.     IF NOT "%input%"=="%!!%" GOTO invInput
  474.  
  475. :: REM Length restriction. Optional 3rd parameter. If not used, MaxLength is set to 9999, effectively removing the Limit.
  476.  
  477.     IF NOT "!input:~%MaxLength%!"=="" (
  478.         ECHO(limit of %MaxLength% Characters allowed for %testinput%
  479.         Set input=
  480.         GOTO :Get_Input
  481.     )
  482.  
  483.  
  484.     IF /I "%permitted%"=="number" (
  485.         echo.!input!| findstr /R "[^0-9]" >nul 2>nul
  486.         IF NOT errorlevel 1 (
  487.             GOTO invInput
  488.         ) else (
  489.             GOTO :Get_Input
  490.         )
  491.     )
  492.    
  493.     IF /I "%permitted%"=="letter" (
  494.         echo.!input!| findstr /R "[^a-zA-Z]" >nul 2>nul
  495.         IF NOT errorlevel 1 (
  496.             GOTO invInput
  497.         ) else (
  498.             GOTO :Get_Input
  499.         )
  500.     )
  501.    
  502.     IF /I "%permitted%"=="alphanumerical" (
  503.         echo.!input!| findstr /R "[^a-zA-Z0-9]" >nul 2>nul
  504.         IF NOT errorlevel 1 (
  505.             GOTO invInput
  506.         ) else (
  507.             GOTO :Get_Input
  508.         )
  509.     )
  510.    
  511.     GOTO :Get_Input
  512.    
  513. :invInput
  514.  
  515.     SET input=
  516.  
  517. ::: REM Specifies the permitted input types.
  518.  
  519. ECHO(Input of %permitted% characters are allowed. Other Symbols and Characters are Not Permitted.
  520.  
  521.     GOTO :Get_Input
  522.  
  523. :return
  524.  
  525. ::: assigns the input value to the variable name being validated.
  526.  
  527.     (
  528.     ENDLOCAL
  529.     Set "%testinput%=%input%"
  530.     exit /b
  531.     )
  532.  
  533. :::
  534. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: End Input Validation Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement