jcunews

FileNameRandomizer.bat

May 30th, 2020 (edited)
3,332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 1.49 KB | None | 0 0
  1. @echo off
  2. rem FileNameRandomizer v1.0.1.
  3. rem Randomize file names in the specified directory using optional file mask.
  4. rem https://pastebin.com/u/jcunews
  5. rem https://www.reddit.com/user/jcunews1/
  6.  
  7. setlocal enabledelayedexpansion
  8. if "%~1" == "" (
  9.   echo Usage: FileNameRandomizer {directory} [file mask]
  10.   echo.
  11.   echo Examples:
  12.   echo   FileNameRandomizer "e:\data\test files"
  13.   echo   FileNameRandomizer ..\testdir *.txt
  14.   echo   FileNameRandomizer testdir "the file*.*"
  15.   goto :eof
  16. )
  17. pushd %1
  18. if errorlevel 1 goto :eof
  19. set mask=%~2
  20. if "%mask%" == "" set mask=*
  21.  
  22. rem *** get file list
  23. echo Retrieving file list...
  24. set namecount=0
  25. for %%A in (%mask%) do (
  26.   set/a namecount+=1
  27.   set name!namecount!=%%A
  28. )
  29. if %namecount% == 0 (
  30.   echo No file found.
  31.   goto :eof
  32. )
  33.  
  34. rem *** generate random file list
  35. Generating randomized file list...
  36. :mkrnd
  37. for /l %%A in (1,1,%namecount%) do (
  38.   set tmp%%A=!name%%A!
  39.   set rnd%%A=
  40. )
  41. set i=1
  42. :getrnd
  43. set/a r=%random%%%namecount+1
  44. if %r% == %i% (
  45.  rem *** restart list randomizer if last item end up having itself to choose
  46.   if %r% == %namecount% goto mkrnd
  47.   goto getrnd
  48. )
  49. if "!tmp%r%!" == "" goto getrnd
  50. set rnd%i%=!tmp%r%!
  51. set tmp%r%=
  52. set/a i+=1
  53. if %i% leq %namecount% goto getrnd
  54.  
  55. rem *** rename files to random names
  56. echo Renaming files...
  57. for /l %%A in (1,1,%namecount%) do (
  58.   set tmp%%A=file%%A.tmp
  59.   ren "!name%%A!" !tmp%%A!
  60. )
  61. for /l %%A in (1,1,%namecount%) do (
  62.   echo !name%%A! ^>^> !rnd%%A!
  63.   ren !tmp%%A! "!rnd%%A!"
  64. )
Add Comment
Please, Sign In to add comment