Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Date Comparison and File Log Function
- :::
- @ECHO OFF & mode 1000 & Title File Age Checking Function
- :::
- ::: For use on Windows 10. To use on Older Systems, remove all instances of ANSI Color Codes IE: [31m
- :DropDir
- Setlocal EnableDelayedExpansion
- cls & Color 02
- Set "LogLoc=%~dp0"
- Set "LogLoc=%LogLoc%logs\"
- IF NOT EXIST "%LogLoc%" md "%LogLoc%"
- Set "format=DDMM"
- Set "LogDate=%DATE%"
- Set "LogDate=%LogDate:~4,10%"
- Set "FormatID=false"
- Call :ProcessDate %LogDate%
- IF %FD_D% GTR 12 (
- ECHO([32m Date Format Detected as [37mDDMMYYYY
- Set "FormatID=true"
- )
- IF "%FormatID%"=="false" (
- Set "format=MMDD"
- Call :ProcessDate %LogDate%
- IF %FD_D% GTR 12 (
- ECHO([32m Date Format Detected as [37mMMDDYYYY
- Set "FormatID=true"
- )
- )
- IF "%FormatID%"=="false" (
- ECHO( [32m Select the[33m Date [32mFormat of your device: [36m[[33mM[36m][37m MMDDYYYY [36m[[33mD[36m] [37mDDMMYYYY[35m
- CHOICE /N /C dm /M "" >nul
- IF %ERRORLEVEL%==2 (
- Set "format=MMDD"
- ) else (
- Set "format=DDMM"
- )
- )
- Set "LogDate=%Modified: =%
- ECHO(
- ECHO([33m Select Search Type: [36m([37mR[36m)[90mecursive [36m([37mF[36m)[90molder Only
- CHOICE /N /C rf /M "" >nul
- IF ERRORLEVEL 2 (
- Set "Search=FolderOnly"
- ) else (
- Set "Search=Recursive"
- )
- ECHO([33m Select Output Type: [36m([37mD[36m)[90misplay Only [36m([37mL[36m)[90mog and Display
- CHOICE /N /C ld /M "" >nul
- IF ERRORLEVEL 2 (
- Set "output=Display"
- ) else (
- Set "output=Log"
- IF Exist "!LogLoc!fileage!LogDate!.log" (
- ECHO( [31mA Log Already Exists For Today.
- ECHO( [37mOverwrite? [33mY[37m/[33mN[90m
- CHOICE /N /C YN /M "" >nul
- IF ERRORLEVEL 2 (
- ECHO(Log Will Not be Stored.
- Set "output=Display"
- ) else (
- DEL /Q "!LogLoc!fileage!LogDate!.log" && ECHO( [31mLog Removed.
- )
- )
- )
- ECHO(
- Set "C_Prompt=[33mDrag and Drop [90mor [33mEnter [37mDirectory [36m}"
- (Set /p "AgeDir=!C_Prompt!<")
- IF Not Exist "!AgeDir!" (
- cls & Color 04
- ECHO Invalid Directory Entered.
- Timeout 1 > nul
- Endlocal & GOTO :DropDir
- )
- ECHO(
- REM Get safe user Input for Date Comparison
- :Day_Select
- Call :InputVar_Type DD number
- REM Remove leading Zeros
- Set /A DD = 100!DD! %% 100
- For /L %%i in (1,1,31) DO (
- IF "%%i"=="!DD!" GOTO :Month_Select
- )
- ECHO([31mInvalid Day.
- GOTO :Day_Select
- :Month_Select
- Call :InputVar_Type MM number
- REM Remove leading Zeros
- Set /A MM = 100!MM! %% 100
- For /L %%i in (1,1,12) DO (
- IF "%%i"=="!MM!" GOTO :Year_Select
- )
- ECHO([31mInvalid Month[32m
- GOTO :Month_Select
- :Year_Select
- Call :InputVar_Type YYYY number
- Call :ConvertJulian %DD% %MM% %YYYY% CompDate
- For /L %%i in (1985,1,2500) DO (
- IF %%i==!YYYY! GOTO :Fetch_Data
- )
- ECHO([31mInvalid Year[32m
- GOTO :Year_Select
- :Fetch_Data
- cls
- Color 06
- CALL :%search% %DD% %MM% %YYYY% "%AgeDir%"
- ECHO & REM The preceeding Character is the BEL character, Not the ANSI ESC character used for Color Codes.
- ECHO( [36m Comparison Complete. [33m Next...
- pause >nul
- ENDLOCAL & GOTO :DropDir
- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: *** End Main and Commence Subroutines ***
- :FolderOnly
- PUSHD "%~4"
- For %%A in (*.*) do (
- Set "extType=%%~xA"
- IF NOT "!extType!"=="" (
- CALL :FolderCheck "%%~dpA"
- CALL :ProcessDate "%%~tA"
- REM Format File information into Columns using Set Substring Modification and white-space
- Set "File_N=%%~nxA"
- Set "File_N=!File_N! "
- Set "File_N=!File_N:~,45!"
- REM Set "Modified=!Modified:~,10!"
- Set "File_S=%%~zA"
- Set "File_S=!File_S! "
- Set "File_S=!File_S:~,15!"
- CALL :ConvertJulian !FD_D! !FD_M! !FD_Y! FileDate
- CALL :DateDifference !FileDate! !CompDate! Difference
- IF !Difference!==0 (
- CALL :MatchFile
- )
- IF !Difference! LSS 0 (
- CALL :NewerFile
- )
- IF !Difference! GTR 0 (
- CALL :OlderFile
- )
- Set "InFolder=%%~dpA"
- )
- )
- POPD
- exit /b
- :Recursive
- PUSHD "%~4"
- For /R %%A in (*.*) do (
- Set "extType=%%~xA"
- IF NOT "!extType!"=="" (
- CALL :FolderCheck "%%~dpA"
- CALL :ProcessDate "%%~tA"
- REM Format File information into Columns using Set Substring Modification and white-space
- Set "File_N=%%~nxA"
- Set "File_N=!File_N! "
- Set "File_N=!File_N:~,45!"
- REM Set "Modified=!Modified:~,10!"
- Set "File_S=%%~zA"
- Set "File_S=!File_S! "
- Set "File_S=!File_S:~,15!"
- CALL :ConvertJulian !FD_D! !FD_M! !FD_Y! FileDate
- CALL :DateDifference !FileDate! !CompDate! Difference
- IF !Difference!==0 (
- CALL :MatchFile "%%A"
- )
- IF !Difference! LSS 0 (
- CALL :NewerFile "%%A"
- )
- IF !Difference! GTR 0 (
- CALL :OlderFile "%%A"
- )
- Set "InFolder=%%~dpA"
- )
- )
- POPD
- exit /b
- :FolderCheck
- REM Doubleqouting Of Directory Path Is Required
- IF Defined InFolder (
- IF NOT "!InFolder!"=="%~1" (
- ECHO(
- ECHO("%~1"
- ECHO(
- IF "!output!"=="Log" (
- ECHO.>>!LogLoc!fileage!LogDate!.log
- ECHO("%~1">>!LogLoc!fileage!LogDate!.log
- ECHO.>>!LogLoc!fileage!LogDate!.log
- )
- )
- ) Else (
- ECHO(
- ECHO("%~1"
- ECHO(
- IF "!output!"=="log" (
- ECHO.>>!LogLoc!fileage!LogDate!.log
- ECHO("%~1">>!LogLoc!fileage!LogDate!.log
- ECHO.>>!LogLoc!fileage!LogDate!.log
- )
- )
- Exit /b
- :OlderFile
- Set "Diff_Display=!Difference! "
- Set "Diff_Display=!Diff_Display:~,15! Days Older. "
- Set Diff_Display=!Diff_Display:~,35!
- ECHO([90m!File_N! !Diff_Display! [33mModified:[90m !Modified! [33mSize: [90m!File_S! [33mBytes
- REM Insert Command/s below here if desired to act on files with an Older date. Filepath passed with Parameter 1
- IF "!output!"=="Log" (
- ECHO(!File_N! !Diff_Display! Modified: !Modified! Size: !File_S! Bytes>>!LogLoc!fileage!LogDate!.log
- )
- Exit /b
- :NewerFile
- Set "Diff_Display=!Difference:~1,20! "
- Set "Diff_Display=!Diff_Display:~,15! Days Newer. "
- Set Diff_Display=!Diff_Display:~,35!
- ECHO([37m!File_N! !Diff_Display! [33mModified:[37m !Modified! [33mSize: [37m!File_S! [33mBytes
- REM Insert Command/s below here if desired to act on Newer files. Filepath passed with Parameter 1
- IF "!output!"=="Log" (
- ECHO(!File_N! !Diff_Display! Modified: !Modified! Size: !File_S! Bytes>>!LogLoc!fileage!LogDate!.log
- )
- Exit /b
- :MatchFile
- Set "Diff_Display=!Difference! "
- Set "Diff_Display=!Diff_Display:~,15! Days. Date Match. "
- Set Diff_Display=!Diff_Display:~,35!
- ECHO([32m!File_N! !Diff_Display! [33mModified:[32m !Modified! [33mSize: [32m!File_S! [33mBytes
- REM Insert Command/s below here if desired to act on files that match the Date. Filepath passed with Parameter 1
- IF "!output!"=="Log" (
- ECHO(!File_N! !Diff_Display! Modified: !Modified! Size: !File_S! Bytes>>!LogLoc!fileage!LogDate!.log
- )
- Exit /b
- :ProcessDate
- Set "F_D=%~1"
- Set "F_D=!F_D:/=!"
- Set "F_D=!F_D:~0,8!"
- Set "FD_Y=!F_D:~4,4!"
- IF "%format%"=="DDMM" (
- REM DDMM Format:
- Set "FD_D=!F_D:~0,2!"
- Set "FD_M=!F_D:~2,2!"
- Set "Modified=!FD_D! !FD_M! !FD_Y!"
- REM Remove leading Zeros to use for LSS LEQ EQU GTR GEQ comparisons
- Set /A FD_D = 100!FD_D! %% 100
- Set /A FD_M = 100!FD_M! %% 100
- ) else (
- REM MMDD Format:
- Set "FD_M=!F_D:~0,2!"
- Set "FD_D=!F_D:~2,2!"
- Set "Modified=!FD_M! !FD_D! !FD_Y!"
- REM Remove leading Zeros to use for LSS LEQ EQU GTR GEQ comparisons
- Set /A FD_M = 100!FD_M! %% 100
- Set /A FD_D = 100!FD_D! %% 100
- )
- exit /b
- :ConvertJulian
- Setlocal
- Set "Day=%1"
- Set "Month=%2"
- Set /a "MonthCv=( %Month% - 14 ) / 12"
- Set /a "YearCv=%3 + 4800"
- Set /a "Julian=1461 * ( %YearCv% + %MonthCv% ) / 4 + 367 * ( %Month% - 2 -12 * %MonthCv% ) / 12 - ( 3 * ( ( %YearCv% + %MonthCv% + 100 ) / 100 ) ) / 4 + %Day% - 32075"
- (
- Endlocal
- Set "%~4=%Julian%"
- exit /b
- )
- :DateDifference
- SetLocal
- Set /a "tmp=%~2 - %~1"
- (
- Endlocal
- Set "%3=%tmp%"
- exit /b
- )
- :::
- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: End File Age Comparison Function
- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Input Validation Function by T3RRY
- ::: Usage: Parameter 1 is Mandatory. Parameters 2 and 3 are optional.
- ::: When Needing User Input:
- ::: For General Input - CALL :InputVar_Type <VarName>
- ::: To Define Input permitted Character types - CALL :InputVar_Type <VarName> <PermitType>
- ::: To Restrict length of Input - CALL :InputVar_Type <VarName> <PermitType> <MaxLength>
- ::: Where <VarName> is replaced with the Variable name to be Set
- ::: And <PermitType> is replaced with letter, number or alphanumerical
- ::: <MaxLength> is The Maximum Allowed String length.
- :InputVar_Type
- (
- SETLOCAL EnableDelayedExpansion
- REM Assign the variable (Passed by call Argument) to be set and how to test it
- SET "testinput=%~1"
- SET "permitted=%~2"
- SET "MaxLength=%~3"
- )
- ::: SYNTAX ::: Tests Parameter Usage is Correct
- IF NOT DEFINED testinput (
- ECHO([31mError. CALL :InputVar_Type Function Not used correctly. [36mParameter 1 for Variable Name is Required.
- ECHO([33m Parameter 2 for Character Types is Optional, though strongly [35mRecommended.
- ECHO([33m Parameter 3 for String Length is Optional.
- Pause
- Exit
- )
- IF NOT "!testinput!"=="!testinput: =!" (
- ECHO([31mError. CALL :InputVar_Type Parameter 1 : [33m%testinput% [31mis Not an acceptable Variable Name. Variable names Cannot Contain Spaces.
- Pause
- EXIT
- )
- REM Parameter 2 for Permitted Character types is Optional, However risks unwanted characters being input.
- IF DEFINED permitted Set "P_True=0"
- For %%V IN (number,letter,alphanumerical) Do (IF /I "!permitted!"=="%%V" Set /a P_True+=1)
- IF "!P_True!"=="0" (
- ECHO([31mError. Incorrect Parameter used for CALL :InputVar_Type 2nd Parameter. %permitted% Is Not acceptable.
- ECHO([31mCorrect Parameters are: "[32mnumber[31m" OR "[32mletter[31m" OR "[32malphanumerical[31m"
- Pause
- Exit
- )
- REM Parameter 3 for MaxLength is Optional. The below Line ensures a value still exists for Later Usage.
- IF NOT DEFINED MaxLength Set "MaxLength=9999"
- echo.!MaxLength!| findstr /R "[^0-9]" >nul 2>nul
- IF NOT errorlevel 1 (
- ECHO([31mError. CALL :InputVar_Type Paramater 3 is Not Defined correctly. [33m%MaxLength% [31mneeds to be a [32mNumber
- Pause
- Exit
- )
- ::: Expand testinput to the Name of the Variable to be Assigned, Ensures Undefined
- SET %testinput%=
- ::: Ensure Undefined Value for Input
- SET input=
- :Get_Input
- ::: Exit Filter once Acceptable Variable is Set
- IF DEFINED input GOTO return
- ::: Custom Prompt regarding input required.
- Set "C_Prompt=[33m Enter Value for [37m%testinput%[36m }> [36m"
- SET /P "input=!C_Prompt!"
- ::: Test to ensure Variable defined. Needed here to prevent environment variable not defined message ahead of next test Should the variable not be defined.
- IF NOT DEFINED input GOTO invInput
- ::: Test for Doublequotes and reset variable if present
- SET Input | FIND """" >NUL
- IF NOT ERRORLEVEL 1 SET Input=
- IF NOT DEFINED input GOTO invInput
- :: REM Block Tilde
- SET Input | FIND "~" >NUL
- IF NOT ERRORLEVEL 1 SET Input=
- IF NOT DEFINED input GOTO invInput
- ::: Test for Spaces
- IF NOT "%input%"=="%input: =%" GOTO invInput
- ::: To permit symbols REM out permitted Symbol comparisons and Call Filter without Second Parameter.
- ::: Test for all other standard Symbols.
- IF NOT "%input%"=="%input:&=%" GOTO invInput
- IF NOT "%input%"=="%input:(=%" GOTO invInput
- IF NOT "%input%"=="%input:)=%" GOTO invInput
- IF NOT "%input%"=="%input:<=%" GOTO invInput
- IF NOT "%input%"=="%input:>=%" GOTO invInput
- IF NOT "%input%"=="%input:^=%" GOTO invInput
- IF NOT "%input%"=="%!!|%" GOTO invInput
- IF NOT "%input%"=="%input:{=%" GOTO invInput
- IF NOT "%input%"=="%input:}=%" GOTO invInput
- IF NOT "%input%"=="%input:?=%" GOTO invInput
- IF NOT "%input%"=="%input:!=%" GOTO invInput
- IF NOT "%input%"=="%input:`=%" GOTO invInput
- IF NOT "%input%"=="%input:'=%" GOTO invInput
- IF NOT "%input%"=="%input:]=%" GOTO invInput
- IF NOT "%input%"=="%input:[=%" GOTO invInput
- IF NOT "%input%"=="%input:#=%" GOTO invInput
- IF NOT "%input%"=="%input:+=%" GOTO invInput
- IF NOT "%input%"=="%input:-=%" GOTO invInput
- IF NOT "%input%"=="%input:/=%" GOTO invInput
- IF NOT "%input%"=="%input:\=%" GOTO invInput
- IF NOT "%input%"=="%input:$=%" GOTO invInput
- IF NOT "%input%"=="%input:@=%" GOTO invInput
- IF NOT "%input%"=="%input:,=%" GOTO invInput
- IF NOT "%input%"=="%input:.=%" GOTO invInput
- IF NOT "%input%"=="%!!%" GOTO invInput
- :: REM Length restriction. Optional 3rd parameter. If not used, MaxLength is set to 9999, effectively removing the Limit.
- IF NOT "!input:~%MaxLength%!"=="" (
- ECHO([33mlimit of %MaxLength% Characters allowed for %testinput%[32m
- Set input=
- GOTO :Get_Input
- )
- IF /I "%permitted%"=="number" (
- echo.!input!| findstr /R "[^0-9]" >nul 2>nul
- IF NOT errorlevel 1 (
- GOTO invInput
- ) else (
- GOTO :Get_Input
- )
- )
- IF /I "%permitted%"=="letter" (
- echo.!input!| findstr /R "[^a-zA-Z]" >nul 2>nul
- IF NOT errorlevel 1 (
- GOTO invInput
- ) else (
- GOTO :Get_Input
- )
- )
- IF /I "%permitted%"=="alphanumerical" (
- echo.!input!| findstr /R "[^a-zA-Z0-9]" >nul 2>nul
- IF NOT errorlevel 1 (
- GOTO invInput
- ) else (
- GOTO :Get_Input
- )
- )
- GOTO :Get_Input
- :invInput
- SET input=
- ::: REM Specifies the permitted input types.
- ECHO([33mInput of %permitted% characters are allowed. [31mOther Symbols and Characters are Not Permitted.[32m
- GOTO :Get_Input
- :return
- ::: assigns the input value to the variable name being validated.
- (
- ENDLOCAL
- Set "%testinput%=%input%"
- exit /b
- )
- :::
- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: End Input Validation Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement