Advertisement
T3RRYT3RR0R

Batch File Macro development

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