T3RRYT3RR0R

Batch 'Where' upgrade For finding files

May 8th, 2020
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 2.36 KB | None | 0 0
  1. @REM ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  2. ::: Batch program to Search for files.                                                  :::
  3. ::: Results stored in a .log file using a 3rd parameter                                 :::
  4. ::: A fourth parameter VarName returns the number of files to that Variable.            :::
  5. :::  - When called from a batch file, the results may be retrieved from an array using: :::
  6. :::    For /L %%i in (1,1,!Param4!) Do Echo !File.Loc[%%i]!                             :::
  7. @Echo off & CD "%~dp0" & Title Filefinder by T3RRY & REM                                :::
  8. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  9.  
  10. ::: { Creates variable Æ = Ascii-27 Escape code, Used to Display output in multiple Colors.
  11. ::: - http://www.dostips.com/forum/viewtopic.php?t=1733
  12. ::: - https://stackoverflow.com/a/34923514/12343998
  13. :::
  14. ::: - Æ (Alt 146) can be used  with and without DelayedExpansion.
  15.     Setlocal
  16.     For /F "tokens=2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  17.         Endlocal
  18.         Set "Æ=%%a"
  19.     )
  20. ::: }
  21.  
  22. Echo.%Æ%[31m & REM
  23.  
  24. IF Not "%~1" == "" IF Not Exist "%~1" (
  25.     Echo.Folder %Æ%[33m"%~1" %Æ%[31mDoes Not Exist%Æ%[0m
  26.     Echo.
  27.     Echo.%Æ%[31mWhereis Usage:
  28.     Echo.%Æ%[33m^<Root Directory^> ^<*.*^> ^<Optional - Output File^> ^<Optional - IndexVar^>%Æ%[0m
  29.     Pause > Nul
  30.     Exit /B 1
  31. )
  32. IF "%~2" == "" (
  33.     Echo.Whereis Usage:
  34.     Echo.%Æ%[33m^<Root Directory^> ^<*.*^> ^<Optional - Output File^> ^<Optional - IndexVar^>%Æ%[0m
  35.     Pause > Nul
  36.     Exit /B 1
  37. )
  38.  
  39. :FileFinder
  40. ::: - Search Recursively through the declared directory for all copies of files matching the search term
  41. ::: - Assigns the results to an array
  42. ::: - Parameters 1 and 2 should be quoted. Results will be more accurate if an extension is Provided for Param 2.
  43.     SetLocal EnableDelayedExpansion
  44.     Set "R#="
  45.     For /F "UsebackQ Tokens=* delims=" %%W in (`WHERE /R "%~1" /F "%~2"`) Do (
  46.         Set /A "R#+=1"
  47.         Set "File.Loc[!R#!]=%%~W"
  48.     )
  49.     IF Not "%~3" == "" (
  50.         Echo %Date% %Time: =0%
  51.     )>>"%~3.log"
  52.  
  53.     For /L %%i in (1,1,!R#!) Do (
  54.         For %%A in ("!File.Loc[%%i]!") Do (
  55.             Echo %Æ%[33m%%~nxA %Æ%[90mis located in:
  56.             Echo %Æ%[36m%%~dpA%Æ%[0m
  57.             IF Not "%~3" == "" (>>"%~3.log" Echo %%~dpnxA)
  58.         )
  59.     )
  60.     If Not "%~4" == "" (Set "%~4=!R#!")
  61. Echo.%Æ%[0m & Rem
  62. Exit /B 0
Add Comment
Please, Sign In to add comment