Advertisement
T3RRYT3RR0R

Batch File Age Compare and Log

Jan 30th, 2020
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 8.68 KB | None | 0 0
  1. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Date Comparison and File Log Function
  2. :::
  3. ::: For use on Windows 10. To use on Older Systems, remove all instances of ANSI Color Codes IE: 
  4.  
  5. @GOTO :Main & REM Main Script Positioned After Functions.
  6.  
  7. :FolderOnly
  8.  
  9.     PUSHD "%~4"
  10.  
  11.     For %%A in (*.*) do (
  12.         Set "extType=%%~xA"
  13.         IF NOT "!extType!"=="" (
  14.             CALL :FolderCheck "%%~dpA"
  15.             CALL :ProcessDate "%%~tA"
  16. REM Format File information into Columns using Set Substring Modification and white-space
  17.             Set "File_N=%%~nxA"
  18.             Set "File_N=!File_N!                                             "
  19.             Set "File_N=!File_N:~,45!"
  20. REM Set "Modified=!Modified:~,10!"
  21.             Set "File_S=%%~zA"
  22.             Set "File_S=!File_S!               "
  23.             Set "File_S=!File_S:~,15!"
  24.             CALL :ConvertJulian !FD_D! !FD_M! !FD_Y! FileDate
  25.             CALL :DateDifference !FileDate! !CompDate! Difference
  26.  
  27.             IF !Difference!==0 (
  28.                 CALL :MatchFile
  29.             )
  30.  
  31.             IF !Difference! LSS 0 (
  32.                 CALL :NewerFile
  33.             )
  34.  
  35.             IF !Difference! GTR 0 (
  36.                 CALL :OlderFile
  37.             )      
  38.         Set "InFolder=%%~dpA"
  39.         )
  40.     )
  41.    
  42.     POPD
  43.     exit /b
  44.  
  45. :Recursive
  46.  
  47.     PUSHD "%~4"
  48.  
  49.     For /R %%A in (*.*) do (
  50.         Set "extType=%%~xA"
  51.         IF NOT "!extType!"=="" (
  52.             CALL :FolderCheck "%%~dpA"             
  53.             CALL :ProcessDate "%%~tA"
  54. REM Format File information into Columns using Set Substring Modification and white-space
  55.             Set "File_N=%%~nxA"
  56.             Set "File_N=!File_N!                                             "
  57.             Set "File_N=!File_N:~,45!"
  58. REM Set "Modified=!Modified:~,10!"
  59.             Set "File_S=%%~zA"
  60.             Set "File_S=!File_S!               "
  61.             Set "File_S=!File_S:~,15!"
  62.             CALL :ConvertJulian !FD_D! !FD_M! !FD_Y! FileDate
  63.             CALL :DateDifference !FileDate! !CompDate! Difference
  64.  
  65.             IF !Difference!==0 (
  66.                 CALL :MatchFile "%%A"
  67.             )
  68.  
  69.             IF !Difference! LSS 0 (
  70.                 CALL :NewerFile "%%A"
  71.             )
  72.  
  73.             IF !Difference! GTR 0 (
  74.                 CALL :OlderFile "%%A"
  75.             )      
  76.         Set "InFolder=%%~dpA"
  77.         )
  78.     )
  79.    
  80.     POPD
  81.     exit /b
  82.  
  83. :FolderCheck <%%~dpI> REM displays or logs change in folder location for each Sub Directory being processed
  84.    
  85. REM Doubleqouting Of Directory Path Is Required
  86.  
  87.     IF Defined InFolder (
  88.         IF NOT "!InFolder!"=="%~1" (
  89.             ECHO(
  90.             ECHO("%~1"
  91.             ECHO(
  92.             IF /I "!output!"=="Log" (
  93.                 ECHO.>>!LogLoc!fileage!LogDate!.log
  94.                 ECHO("%~1">>!LogLoc!fileage!LogDate!.log
  95.                 ECHO.>>!LogLoc!fileage!LogDate!.log
  96.             )
  97.         )
  98.     )
  99.     IF Not Defined Infolder (
  100.         ECHO(
  101.         ECHO("%~1"
  102.         ECHO(
  103.         IF /I "!output!"=="Log" (
  104.             ECHO.>>!LogLoc!fileage!LogDate!.log
  105.             ECHO("%~1">>!LogLoc!fileage!LogDate!.log
  106.             ECHO.>>!LogLoc!fileage!LogDate!.log
  107.         )
  108.     )              
  109.  
  110.     Exit /b
  111.  
  112. :OlderFile
  113.  
  114.     Set "Diff_Display=!Difference!               "
  115.     Set "Diff_Display=!Diff_Display:~,15! Days Older.                              "
  116.     Set Diff_Display=!Diff_Display:~,35!
  117.     ECHO(!File_N! !Diff_Display! Modified: !Modified! Size: !File_S! Bytes
  118.  
  119. REM Insert Command/s below here if desired to act on files with an Older date. Filepath passed with Parameter 1
  120.  
  121.     IF "!output!"=="Log" (
  122.         ECHO(!File_N! !Diff_Display! Modified: !Modified! Size: !File_S! Bytes>>!LogLoc!fileage!LogDate!.log
  123.     )
  124.     Exit /b
  125.  
  126. :NewerFile
  127.  
  128.     Set "Diff_Display=!Difference:~1,20!               "
  129.     Set "Diff_Display=!Diff_Display:~,15! Days Newer.                              "
  130.     Set Diff_Display=!Diff_Display:~,35!
  131.     ECHO(!File_N! !Diff_Display! Modified: !Modified! Size: !File_S! Bytes
  132.  
  133. REM Insert Command/s below here if desired to act on Newer files. Filepath passed with Parameter 1
  134.  
  135.     IF "!output!"=="Log" (
  136.     ECHO(!File_N! !Diff_Display! Modified: !Modified! Size: !File_S! Bytes>>!LogLoc!fileage!LogDate!.log
  137.     )
  138.     Exit /b
  139.  
  140. :MatchFile
  141.  
  142.     Set "Diff_Display=!Difference!               "
  143.     Set "Diff_Display=!Diff_Display:~,15! Days. Date Match.                              "
  144.     Set Diff_Display=!Diff_Display:~,35!
  145.     ECHO(!File_N! !Diff_Display! Modified: !Modified! Size: !File_S! Bytes
  146.  
  147. REM Insert Command/s below here if desired to act on files that match the Date. Filepath passed with Parameter 1
  148.  
  149.     IF "!output!"=="Log" (
  150.         ECHO(!File_N! !Diff_Display! Modified: !Modified! Size: !File_S! Bytes>>!LogLoc!fileage!LogDate!.log
  151.     )
  152.     Exit /b
  153.  
  154. :ProcessDate <%%~tI>
  155.  
  156.     Set "F_D=%~1"
  157.     Set "F_D=!F_D:/=!"
  158.     Set "F_D=!F_D:~0,8!"
  159.     Set "FD_Y=!F_D:~4,4!"
  160.  
  161.     IF "%format%"=="DDMM" (
  162.     REM DDMM Format:
  163.         Set "FD_D=!F_D:~0,2!"
  164.         Set "FD_M=!F_D:~2,2!"
  165.         Set "Modified=!FD_D! !FD_M! !FD_Y!"
  166.  
  167. REM Remove leading Zeros to use for LSS LEQ EQU GTR GEQ comparisons
  168.         Set /A FD_D = 100!FD_D! %% 100
  169.         Set /A FD_M = 100!FD_M! %% 100
  170.     ) else (
  171. REM MMDD Format:
  172.         Set "FD_M=!F_D:~0,2!"
  173.         Set "FD_D=!F_D:~2,2!"
  174.         Set "Modified=!FD_M! !FD_D! !FD_Y!"
  175. REM Remove leading Zeros to use for LSS LEQ EQU GTR GEQ comparisons
  176.         Set /A FD_M = 100!FD_M! %% 100
  177.         Set /A FD_D = 100!FD_D! %% 100
  178.     )
  179.  
  180.     Exit /b
  181.  
  182. :ConvertJulian REM Leading Zero's Must be removed. <DD> <MM> <YYYY> <ResultVar>
  183.  
  184.     Setlocal
  185.     Set "Day=%1"
  186.     Set "Month=%2"
  187.     Set /a "MonthCv=( %Month% - 14 ) / 12"
  188.     Set /a "YearCv=%3 + 4800"
  189.     Set /a "Julian=1461 * ( %YearCv% + %MonthCv% ) / 4 + 367 * ( %Month% - 2 -12 * %MonthCv% ) / 12 - ( 3 * ( ( %YearCv% + %MonthCv% + 100 ) / 100 ) ) / 4 + %Day% - 32075"
  190.  
  191.     (
  192.     Endlocal
  193.     Set "%~4=%Julian%"
  194.     exit /b
  195.     )
  196.  
  197. :DateDifference <Comparison Date Julian Value> <File Date Julian Value> <ResultVar>
  198.  
  199.     SetLocal
  200.     Set /a "tmp=%~2 - %~1"
  201.     (
  202.     Endlocal
  203.     Set "%3=%tmp%"
  204.     exit /b
  205.     )
  206.  
  207. :Input <VarName> <StartRange> <EndRange> <Next Label>
  208.     Set "C_Prompt=Enter a value between %2 and %3 for %1 }< "
  209.     Set /P "%1=!C_Prompt!"
  210.     For /L %%i in (%2,1,%3) DO (IF "%%i"=="!%1!" GOTO :%4)
  211.     ECHO(Invalid value for %1
  212.     Exit /B
  213.  
  214. :::
  215. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: End File Age Comparison Function
  216.  
  217. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: *** Commence Main ***
  218.  
  219. :Main
  220. @ECHO OFF & mode 1000 & Title File Age Checking Function
  221.  
  222.     Setlocal EnableDelayedExpansion
  223.     cls & Color 02
  224.  
  225.     Set "LogLoc=%~dp0"
  226.     Set "LogLoc=%LogLoc%logs\"
  227.     IF NOT EXIST "%LogLoc%" md "%LogLoc%"
  228.  
  229.     Set "format=DDMM"
  230.     Set "LogDate=%DATE%"
  231.     Set "LogDate=%LogDate:~4,10%"
  232.     Set "FormatID=false"
  233.     Call :ProcessDate %LogDate%
  234.  
  235.     IF %FD_D% GTR 12 (
  236.         ECHO( Date Format Detected as DDMMYYYY
  237.         Set "FormatID=true"
  238.     )
  239.  
  240.     IF "%FormatID%"=="false" (
  241.         Set "format=MMDD"
  242.         Call :ProcessDate %LogDate%
  243.         IF %FD_D% GTR 12 (
  244.             ECHO( Date Format Detected as MMDDYYYY
  245.             Set "FormatID=true"
  246.         )
  247.     )
  248.  
  249.     IF "%FormatID%"=="false" (
  250.         ECHO(  Select the Date Format of your device: [M] MMDDYYYY [D] DDMMYYYY
  251.         CHOICE /N /C dm /M "" >nul
  252.         IF %ERRORLEVEL%==2 (
  253.             Set "format=MMDD"
  254.         ) else (
  255.             Set "format=DDMM"
  256.         )
  257.     )
  258.  
  259.     Set "LogDate=%Modified: =%
  260.  
  261.     ECHO(
  262.     ECHO( Select Search Type: (R)ecursive (F)older Only
  263.     CHOICE /N /C rf /M "" >nul
  264.     IF ERRORLEVEL 2 (
  265.         Set "Search=FolderOnly"
  266.     ) else (
  267.         Set "Search=Recursive"
  268.     )
  269.    
  270.     ECHO( Select Output Type: (D)isplay Only (L)og and Display
  271.     CHOICE /N /C ld /M "" >nul
  272.     IF ERRORLEVEL 2 (
  273.         Set "output=Display"
  274.     ) else (
  275.         Set "output=Log"
  276.         IF Exist "!LogLoc!fileage!LogDate!.log" (
  277.             ECHO( A Log Already Exists For Today.
  278.             ECHO( Overwrite? Y/N
  279.             CHOICE /N /C YN /M "" >nul
  280.             IF ERRORLEVEL 2 (
  281.                 ECHO(Log Will Not be Stored.
  282.                 Set "output=Display"
  283.             ) else (
  284.                 DEL /Q "!LogLoc!fileage!LogDate!.log" && ECHO( Log Removed.
  285.             )
  286.         )
  287.     )
  288.    
  289.     ECHO(
  290.  
  291.     Set "C_Prompt=Drag and Drop or Enter Directory  }< "
  292.     (Set /p "AgeDir=!C_Prompt!")
  293.  
  294.     IF Not Exist "!AgeDir!" (
  295.         Cls
  296.         ECHO( Invalid Directory Entered.
  297.         Timeout 1 > nul
  298.         Endlocal & GOTO :Main
  299.     )
  300.    
  301.     ECHO(
  302.  
  303. REM Get safe user Input for Date Comparison
  304.  
  305. :Day_Select
  306.  
  307.     Call :Input DD 1 31 Month_Select
  308.     GOTO :Day_Select
  309.  
  310. :Month_Select
  311.  
  312.     Call :Input MM 1 12 Year_Select
  313.     GOTO :Month_Select
  314.  
  315. :Year_Select
  316.  
  317. REM Date substring modification `%Date:~10,4%` suitable for DDMMYYYY or MMDDYYYY Formats only
  318.     Call :Input YYYY 1985 %Date:~10,4% Fetch_Data
  319.     GOTO :Year_Select
  320.  
  321. :Fetch_Data
  322.     Call :ConvertJulian %DD% %MM% %YYYY% CompDate
  323.     Cls
  324.     CALL :%search% %DD% %MM% %YYYY% "%AgeDir%"
  325.     ECHO  & REM The preceeding Character is the BEL character, Not the ANSI ESC character used for Color Codes.
  326.     ECHO(  Comparison Complete.  Next...
  327.     pause >nul
  328.     ENDLOCAL & GOTO :Main
  329.  
  330. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: *** End Main ***
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement