Advertisement
T3RRYT3RR0R

Batch File Logger

Jan 24th, 2020
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 4.26 KB | None | 0 0
  1. @ECHO OFF
  2. mode 1000
  3. SETLOCAL enableDelayedExpansion
  4. Color 06
  5.  
  6. Set "LogFilesDir=%~dp0"
  7.  
  8. IF NOT EXIST "!LogFilesDir!logs" MD "!LogFilesDir!logs"
  9. Set "LogFilesDir=!LogFilesDir!logs\"
  10.  
  11. :main
  12.     IF exist "!LogFilesDir!*.log" (
  13.         CHOICE /C ny /M "Remove previous Logs?"
  14.         IF !errorlevel!==2 (
  15.             DEL /Q !LogFilesDir!*.log 2>nul
  16.             ECHO Logs Removed.
  17.             TIMEOUT 1 >nul
  18.         )
  19.     )
  20.  
  21. :selectDir
  22. cls
  23. Color 03
  24. CHOICE /N /C 12345 /M "[Select Directory To Log] 1: Root. 2: User Profile. 3: System. 4: App_Data 5: Other"
  25. IF !errorlevel!==5 GOTO :Drop
  26. IF !errorlevel!==4 GOTO :App_Data
  27. IF !errorlevel!==3 GOTO :System
  28. IF !errorlevel!==2 GOTO :User
  29. IF !errorlevel!==1 GOTO :Root
  30. GOTO :selectDir
  31.  
  32. :System
  33. (Set "LogDir=!WinDir!")
  34. GOTO :Confirm
  35.  
  36. :User
  37. (Set "LogDir=!userprofile!")
  38. GOTO :Confirm
  39.  
  40. :Root
  41. (Set "LogDir=%~d0\")
  42. GOTO :Confirm
  43.  
  44. :App_Data
  45. (Set "LogDir=!LOCALAPPDATA!")
  46. GOTO :Confirm
  47.  
  48. :Drop
  49. (Set /p "LogDir=[Enter or Drop Directory to Log:]<")
  50. GOTO :Confirm
  51.  
  52. :Confirm
  53.     IF Not Exist "!LogDir!" (
  54.         cls
  55.         Color 04
  56.         ECHO Invalid Directory Entered.
  57.         Timeout 1 > nul
  58.         GOTO :selectDir
  59.     )
  60.  
  61. REM CHOICE /C ny /M "Confirm: !LogDir!"
  62. REM IF NOT !errorlevel!==2 GOTO :selectDir
  63.  
  64. :: REM - The Directory being searched & logged
  65. CD !LogDir!
  66.  
  67. :: REM - Variable used to display total number of files logged.
  68. Set totalFiles=0
  69. cls
  70.  
  71. CHOICE /N /C pa /M "[Log (A)ll Extensions OR (P)redefined Extensions:]"
  72. IF ERRORLEVEL==2 GOTO :log_all
  73.  
  74. :log_predefined
  75. cls
  76. :: REM - For loop calls a function that logs files according to extension type.
  77.  
  78.     FOR %%a in (vbs,exe,ps1,bat,txt,sav,docx,pdf,ini,wav,mp3,mp4,ico,png,jpg,gif,bmg,png) DO (
  79.         Set "ext=%%a"
  80.             CALL :Get_DateTime
  81.             CALL :process
  82.     )
  83.  
  84.     FOR /L %%a IN (5,1,40) DO (
  85.         ECHO [%%a;1H
  86.     )
  87.  
  88. ECHO Inventory Complete
  89. pause >nul
  90. exit
  91.  
  92. :process
  93. :: REM - assign path for log
  94.  
  95. Set "Storefile=!LogFilesDir!!ext!FILES!T_D!.log"
  96.  
  97. :: REM - Variable used for displaying filecount for current ext being logged
  98.  
  99. Set Found=0
  100.  
  101. :: REM - Variable used to in array to display groups of files logged
  102.  
  103. Set Dcount=0
  104.  
  105. :: REM - Increments counts used for display of chosen Information, Displays Status, Appends files found to log.
  106.  
  107.     For /R %%b IN (*.%ext%) DO (
  108.         Set /a Dcount+=1
  109.         Set /a totalFiles+=1
  110.         Set /a Found+=1
  111.         Set name=%%~nb
  112.         Set location=%%~dpb
  113.         ECHO  !Found! !EXT! files located !totalFiles! files logged in Total.
  114.         ECHO  Searching !location!...
  115.         Set Line[!Dcount!]=%%b
  116.         ECHO  !name! 
  117.         ECHO  On !T_D!
  118.         ECHO(%%~b>>"%Storefile%"
  119.         IF !Dcount!==35 (CALL :Display)
  120.     )
  121.  
  122. GOTO :eof
  123.  
  124. :: REM - Function to display array of logged files in groups of 35.
  125.  
  126. :Display
  127. Set Dcount=0
  128.  
  129.     For /L %%a IN (1,1,35) DO (
  130.         Set /a Offset=%%a
  131.         Set /a Offset=!Offset!+5
  132.         ECHO [!Offset!;1H !Line[%%a]!
  133.     )
  134.  
  135. GOTO :EOF
  136.  
  137.  
  138. :log_all
  139. CALL :Get_DateTime
  140. cls
  141. :: REM - For loop calls a function that logs files according to extension type.
  142. Set "N_E_F=0"
  143.  
  144.     FOR /R %%a in (*.*) DO (
  145.         Set "PathOfFile=%%a"
  146.         Set "ext=%%~xa"
  147.         IF EXIST "!PathOfFile!" (
  148.             IF DEFINED ext (CALL :process_all "!PathOfFile!")
  149.             Set ext=
  150.         ) else (
  151.         Set /a "N_E_F+=1"
  152.         ECHO  [!N_E_F!] Files with no Extension Encountered.
  153.         ECHO("%%~pa"
  154.         ECHO("%%~dpnxa"
  155. REM delay progress to allow reading of extensionless file info
  156.             For /L %%b IN (1,1,1000) DO (
  157.                 Set Delay=Delayed%%b
  158.             )
  159.         )
  160.     )
  161.  
  162. ECHO Inventory Complete
  163. pause >nul
  164. exit
  165.  
  166. :: REM - Function that finds and logs files.
  167.  
  168. :process_all
  169.  
  170. :: REM - Increments counts used for display of chosen Information, Displays Status, Appends files found to log.
  171.  
  172.     For %%b IN (%1) DO (
  173.         Set /a totalFiles+=1
  174.         Set "name=%%~nb"
  175.         Set "location=%%~dpb"
  176.         Set "ext=!ext:.=!"
  177.         Set "Storefile=!LogFilesDir!!ext!FILES!T_D!.log"
  178.         ECHO  !totalFiles! files logged in Total.
  179.         ECHO  Searching in !location!...
  180.         ECHO  !EXT! file: !name! 
  181.         ECHO(%%~b>>"!storefile!"
  182.     )
  183.  
  184. GOTO :eof
  185.  
  186. :Get_DateTime
  187. Set T_D=%DATE%
  188. Set T_D=%T_D: =%
  189. Set T_D=%T_D%%TIME%
  190. Set T_D=%T_D:/=%
  191. Set T_D=%T_D::=%
  192. Set T_D=%T_D:.=%
  193. Set T_D=%T_D: =0%
  194. Set T_D=%T_D:~3,12%
  195. GOTO :EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement