Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Date Comparison and File Log Function
- :::
- ::: For use on Windows 10. To use on Older Systems, remove all instances of ANSI Color Codes IE: [31m
- @GOTO :Main & REM Main Script Positioned After Functions.
- :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 <%%~dpI> REM displays or logs change in folder location for each Sub Directory being processed
- REM Doubleqouting Of Directory Path Is Required
- IF Defined InFolder (
- IF NOT "!InFolder!"=="%~1" (
- ECHO(
- ECHO("%~1"
- ECHO(
- IF /I "!output!"=="Log" (
- ECHO.>>!LogLoc!fileage!LogDate!.log
- ECHO("%~1">>!LogLoc!fileage!LogDate!.log
- ECHO.>>!LogLoc!fileage!LogDate!.log
- )
- )
- )
- IF Not Defined Infolder (
- ECHO(
- ECHO("%~1"
- ECHO(
- IF /I "!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 <%%~tI>
- 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 REM Leading Zero's Must be removed. <DD> <MM> <YYYY> <ResultVar>
- 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 <Comparison Date Julian Value> <File Date Julian Value> <ResultVar>
- SetLocal
- Set /a "tmp=%~2 - %~1"
- (
- Endlocal
- Set "%3=%tmp%"
- exit /b
- )
- :Input <VarName> <StartRange> <EndRange> <Next Label>
- Set "C_Prompt=[37mEnter a value between [33m%2 [37mand [33m%3 [37mfor [36m%1 [37m}<[33m "
- Set /P "%1=!C_Prompt!"
- For /L %%i in (%2,1,%3) DO (IF "%%i"=="!%1!" GOTO :%4)
- ECHO([31mInvalid value for [33m%1
- Exit /B
- :::
- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: End File Age Comparison Function
- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: *** Commence Main ***
- :Main
- @ECHO OFF & mode 1000 & Title File Age Checking Function
- 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=[37mDrag and Drop [90mor [37mEnter [36mDirectory [37m }< [33m"
- (Set /p "AgeDir=!C_Prompt!")
- IF Not Exist "!AgeDir!" (
- Cls
- ECHO([31m Invalid Directory Entered.
- Timeout 1 > nul
- Endlocal & GOTO :Main
- )
- ECHO(
- REM Get safe user Input for Date Comparison
- :Day_Select
- Call :Input DD 1 31 Month_Select
- GOTO :Day_Select
- :Month_Select
- Call :Input MM 1 12 Year_Select
- GOTO :Month_Select
- :Year_Select
- REM Date substring modification `%Date:~10,4%` suitable for DDMMYYYY or MMDDYYYY Formats only
- Call :Input YYYY 1985 %Date:~10,4% Fetch_Data
- GOTO :Year_Select
- :Fetch_Data
- Call :ConvertJulian %DD% %MM% %YYYY% CompDate
- Cls
- 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 :Main
- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: *** End Main ***
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement