Guest User

batch random rename files

a guest
Oct 24th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 1.93 KB | None | 0 0
  1. @ECHO OFF
  2. ECHO Random Names
  3. ECHO Written By: Jason Faulkner
  4. ECHO HowToGeek.com
  5. ECHO.
  6. ECHO.
  7.  
  8. REM Randomly renames every file in a directory.
  9.  
  10. SETLOCAL EnableExtensions EnableDelayedExpansion
  11.  
  12. REM 0 = Rename the file randomly.
  13. REM 1 = Prepend the existing file name with randomly generated string.
  14. SET PrependOnly=0
  15.  
  16. REM 1 = Undo changes according to the translation file.
  17. REM This will only work if the file "__Translation.txt" is in the same folder.
  18. REM If you delete the translaction file, you will not be able to undo the changes!
  19. SET Undo=0
  20.  
  21.  
  22. REM --------------------------------------------------------------------------
  23. REM Do not modify anything below this line unless you know what you are doing.
  24. REM --------------------------------------------------------------------------
  25.  
  26. SET TranslationFile=__Translation.txt
  27.  
  28. IF NOT {%Undo%}=={1} (
  29.     REM Rename files
  30.     ECHO You are about to randomly rename every file in the following folder:
  31.     ECHO %~dp0
  32.     ECHO.
  33.     ECHO A file named %TranslationFile% will be created which allows you to undo this.
  34.     ECHO Warning: If %TranslationFile% is lost/deleted, this action cannot be undone.
  35.     ECHO Type "OK" to continue.
  36.     SET /P Confirm=
  37.     IF /I NOT {!Confirm!}=={OK} (
  38.         ECHO.
  39.         ECHO Aborting.
  40.         GOTO :EOF
  41.     )
  42.  
  43.     ECHO Original Name/Random Name > %TranslationFile%
  44.     ECHO ------------------------- >> %TranslationFile%
  45.  
  46.     FOR /F "tokens=*" %%A IN ('DIR /A:-D /B') DO (
  47.         IF NOT %%A==%~nx0 (
  48.             IF NOT %%A==%TranslationFile% (
  49.                 SET Use=%%~xA
  50.                 IF {%PrependOnly%}=={1} SET Use=_%%A
  51.                
  52.                 SET NewName=!RANDOM!-!RANDOM!-!RANDOM!!Use!
  53.                 ECHO %%A/!NewName!>> %TranslationFile%
  54.                
  55.                 RENAME "%%A" "!NewName!"
  56.             )
  57.         )
  58.     )
  59. ) ELSE (
  60.     ECHO Undo mode.
  61.     IF NOT EXIST %TranslationFile% (
  62.         ECHO Missing translation file: %TranslationFile%
  63.         PAUSE
  64.         GOTO :EOF
  65.     )
  66.     FOR /F "skip=2 tokens=1,2 delims=/" %%A IN (%TranslationFile%) DO RENAME "%%B" "%%A"
  67.     DEL /F /Q %TranslationFile%
  68. )
Add Comment
Please, Sign In to add comment