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: %/E%[31m
- @CD "%~dp0"
- @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 using white-space to pad the string, and Substring Modification to trim it to the desired length, creating columns.
- Set "File_N=%%~nxA"
- Set "File_N=!File_N! "
- REM trim the padded string to a defined maximum length
- 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/%/E%[90m!File_N! !Diff_Display! %/E%[33mModified:%/E%[90m !Modified! %/E%[33mSize: %/E%[90m!File_S! %/E%[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/%/E%[37m!File_N! !Diff_Display! %/E%[33mModified:%/E%[37m !Modified! %/E%[33mSize: %/E%[37m!File_S! %/E%[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/%/E%[32m!File_N! !Diff_Display! %/E%[33mModified:%/E%[32m !Modified! %/E%[33mSize: %/E%[32m!File_S! %/E%[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=%/E%[37mEnter a value between %/E%[33m%2 %/E%[37mand %/E%[33m%3 %/E%[37mfor %/E%[36m%1 %/E%[37m}<%/E%[33m "
- Set /P "%1=!C_Prompt!"
- For /L %%i in (%2,1,%3) DO (IF "%%i"=="!%1!" GOTO :%4)
- Echo/%/E%[31mInvalid value for %/E%[33m%1
- Exit /B
- :::
- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: End File Age Comparison Function
- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: *** Commence Main ***
- :Main
- @Echo Off & Setlocal EnableExtensions DisableDelayedExpansion
- ::: ************************************************************** || Macro Definitions
- rem *** Delayed expansion must be enabled AFTER definition of macros, and BEFORE their use.
- Set "TAB= "
- rem *** Usage: %Menu%Options as list "Quoting Strings with spaces"
- rem ::: Returns the selected string in the variable OPTION
- Set "Menu=Echo/!DIV!&For %%n in (1 2)Do if %%n==2 ((Set "CHCS="&For %%G in (!Options!)Do (Set "Opt=%%~G"& Set "CHCS=!CHCS!!Opt:~0,1!"&Set "Opt[!Opt:~0,1!]=%%~G"& Set "Opt=[!Opt:~0,1!]!Opt:~1!"& Echo/!Opt!))&Echo/!DIV!& For /F "Delims=" %%o in ('Choice /N /C:!CHCS!')Do (For %%C in ("!Opt[%%o]!")Do (Set "OPTION=%%~C")))Else Set Options="
- rem *** Usage:%DEF/array:$E=varname%"Quoted Element Value"
- Set "DEF/array=(If "!#$E!"=="" (Set "#$E=-1"))&For %%n in (1 2)Do if %%n==2 (Set /A "#$E+=1"&For %%G in (!$E!)Do (Set "$E[!#$E!]=%%~G"))Else Set $E="
- rem *** extension type provided using Substring modification to replace $E
- rem ::: selected path returned in Path[!Ext!]
- rem *** Usage: %SeletFile:$E=Ext%
- Set "SelectFolder=(If Not !#$E! GTR 0 (Echo/No $E Files found. & Exit /B 0))& (For /L %%i in (0 1 !#$E!) Do If Not "!$E[%%i]!"=="" Echo/%%i:!TAB!!$E[%%i]!)&Echo/!DIV!&Echo/Select a $E file number [0-!#$E!]:&Echo/!DIV!&Set /P "FN=FN: "&For %%v in ("!FN!")do (If /I "!FN!"=="Exit" (Exit /B 0)Else If not "!$E[%%~v]!"=="" (Set "$E=!$E[%%~v]!"&Echo/!Div!&Echo/!$E[%%~v]! Selected)Else (Echo/invalid file index&Echo/!Div!& Timeout 1 /Nobreak > Nul & Goto :$E))"
- Set "Do=If /I "!Option!"=="Exit" (Exit)Else (For %%i in ("!Path[$E]!")Do !Option! "%%~i")"
- rem *** Usage: %GetFiles:$E=Ext%
- rem ::: substitute $E with the extension type selection array is to be defined with Via DEF/Array macro.
- Set "GetFolders=Set "#$E="&For /F "Delims=" %%O in ('Dir /B /S /A:D')Do %DEF/array%"%%~fO""
- ::: ************************************************************** || End Disabled DE Macro Definitions
- @ECHO OFF & mode 1000 & Title File Age Checking Function
- For /F %%a in ('echo prompt $E ^| cmd')do (Set "/E=%%a")
- 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/%/E%[32m Date Format Detected as %/E%[37mDDMMYYYY
- Set "FormatID=true"
- )
- IF "%FormatID%"=="false" (
- Set "format=MMDD"
- Call :ProcessDate %LogDate%
- IF %FD_D% GTR 12 (
- Echo/%/E%[32m Date Format Detected as %/E%[37mMMDDYYYY
- Set "FormatID=true"
- )
- )
- IF "%FormatID%"=="false" (
- Echo/ %/E%[32m Select the%/E%[33m Date %/E%[32mFormat of your device: %/E%[36m[%/E%[33mM%/E%[36m]%/E%[37m MMDDYYYY %/E%[36m[%/E%[33mD%/E%[36m] %/E%[37mDDMMYYYY%/E%[35m
- CHOICE /N /C dm /M "" >nul
- IF %ERRORLEVEL%==2 (
- Set "format=MMDD"
- ) else (
- Set "format=DDMM"
- )
- )
- Set "LogDate=%Modified: =%
- Echo/
- Echo/%/E%[33m Select Search Type: %/E%[36m(%/E%[37mR%/E%[36m)%/E%[90mecursive %/E%[36m(%/E%[37mF%/E%[36m)%/E%[90molder Only
- CHOICE /N /C rf /M "" >nul
- IF ERRORLEVEL 2 (
- Set "Search=FolderOnly"
- ) else (
- Set "Search=Recursive"
- )
- Echo/%/E%[33m Select Output Type: %/E%[36m(%/E%[37mD%/E%[36m)%/E%[90misplay Only %/E%[36m(%/E%[37mL%/E%[36m)%/E%[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/ %/E%[31mA Log Already Exists For Today.
- Echo/ %/E%[37mOverwrite? %/E%[33mY%/E%[37m/%/E%[33mN%/E%[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/ %/E%[31mLog Removed.
- )))
- %GetFolders:$E=DirPath%
- :DirPath
- %SelectFolder:$E=DirPath%
- IF Not Exist "!DirPath!" (
- Cls
- Echo/%/E%[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% "%DirPath%"
- ECHO & REM The preceeding Character is the BEL character, Not the ANSI ESC character used for Color Codes.
- Echo/ %/E%[36m Comparison Complete. %/E%[33m Next...
- pause >nul
- ENDLOCAL & GOTO :Main
- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: *** End Main ***
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement