1iJaCk

bulk_rename.bat

Aug 19th, 2016
281
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 22.93 KB | None | 0 0
  1. :: BY Alex V | Release 08/02/2017 | https://pastebin.com/raw/dhzkny3h | bulk_rename.bat  ---  Renames Files in a Folder
  2. :: USE AT YOUR OWN RISK!!!!! NO WARRANTY.  NO SUPPORT.  NO GUARANTEE
  3. @echo off && setlocal EnableDelayedExpansion EnableExtensions
  4.  
  5. :: go to the start of the function calls
  6. goto START_SCRIPT %*
  7. goto :eof
  8.  
  9.  
  10.  
  11. :HARDCODED_INPUT
  12. :: Hardcoded Variables  :::::::::::::::
  13. ::  Location where you'd like to rename the files.
  14. if not defined bulkr_dir_with_files_to_rename ^
  15. set "bulkr_dir_with_files_to_rename="        
  16.  
  17.  
  18. ::  String that you would like replaced.
  19. if not defined bulkr_unwanted_string ^
  20. set "bulkr_unwanted_string="
  21.  
  22.  
  23. ::  New string to replace the old one with.
  24. if not defined bulkr_new_string ^
  25. set "bulkr_new_string="
  26.  
  27. :: Oh Noes, I am unable to recall which fix is for what fix.
  28. :: Remember folks:
  29. ::   String.txt ~> PrefixStringSuffix.txt
  30. ::
  31. :: Don't forget to pad with spaces, underscores, dashes, etc
  32. :: Example:
  33. ::   String.txt ~> Prefix_String_Suffix.txt
  34. ::
  35.  
  36. ::  Enter the new Prefix, Example: [awesomesause]_
  37. if not defined bulkr_new_prefix ^
  38. set "bulkr_new_prefix="
  39.  
  40.  
  41. ::  Enter the new Suffix, Example: _[awesomesause]
  42. if not defined bulkr_new_suffix ^
  43. set "bulkr_new_suffix="
  44.  
  45.  
  46. ::  If you have provided all desired information via variables or parameters and wish to bypass prompts.
  47. ::    ANY VALUE makes this true.  NO VALUE makes this false
  48. if not defined bulkr_bypass_all_prompts ^
  49. set "bulkr_bypass_all_prompts="
  50. :::::::::::::::::::::::::::::::::::::::
  51. goto :eof
  52.  
  53.  
  54.  
  55. :SCRIPT_VARS
  56. :: Script Variables :::::::::::::::::::
  57. set "bulkr_auto_bypass_all_prompts="          rem :: Default: Not Set :: If set, will ALWAYS bypass all prompts
  58. set "bulkr_default_timeout_value=3"           rem :: Default: 3       :: How long to wait before processing when all prompts are bypassed
  59. set "bulkr_allow_prompted_vars=true"          rem :: Default: true    :: Does not unset the vars assosiated to params.
  60. set "bulkr_rename_bin=move"                   rem :: Default: move    :: Binary used to perform the rename, 'rename' is an alternative although its not functional at the moment
  61. set "bulkr_current_directory=%cd%"            rem :: Default: %cd%    :: Enter the bulkr_current_directory path(examples: %cd% or '.\' or '%~dp0' )
  62. :::::::::::::::::::::::::::::::::::::::
  63. goto :eof
  64.  
  65.  
  66.  
  67. :::::::::::::::::::::::::::::::::::::::
  68. ::                                   ::
  69. :::::     Do Not Modify Below     :::::
  70. ::                                   ::
  71. :::::::::::::::::::::::::::::::::::::::
  72.  
  73. :START_SCRIPT
  74. call :SCRIPT_VARS
  75. call :CLEAR_VARS
  76. call :encode_utf8
  77. call :SCRIPT_VARS
  78. call :help_check %~1
  79. if defined bulkr_shown_help_flag goto :eof
  80.  
  81. call :HARDCODED_INPUT
  82. call :PARAM_INPUT %*
  83. call :func_reset_counters
  84. call :PROMPT_INPUT
  85. call :ERROR_CHECK_1
  86. call :INITIALIZE
  87. call :RENAME_LOOP
  88. call :SUMMARY
  89. call :CLEANUP
  90. call :CLEAR_VARS
  91. goto :eof
  92. :::::::::::::::::::::::::::::::::::::::
  93.  
  94.  
  95.  
  96. :: Clearing Variables :::::::::::::::::
  97. ::    Clears all bulkr_ variables    ::
  98. ::    only param vars bypassed       ::
  99. ::                                   ::
  100. ::
  101. :CLEAR_VARS
  102. for /f "tokens=1 delims==" %%x in ('set bulkr_') do (
  103.     if defined bulkr_allow_prompted_vars (
  104.         echo %%~x | findstr /i "bulkr_dir_with_files_to_rename bulkr_unwanted_string bulkr_new_string bulkr_new_prefix bulkr_new_suffix" 2>nul 1>nul
  105.         if !errorlevel! neq 0 (
  106.             set "%%~x="
  107.         )
  108.     ) else (
  109.         set "%%~x="
  110.     )
  111. )
  112.  
  113. goto :eof
  114. :::::::::::::::::::::::::::::::::::::::
  115.  
  116.  
  117.  
  118. :PARAM_INPUT
  119. :: Parameters  ::::::::::::::::::::::::
  120. ::  All Quotes will be removed       ::
  121. ::  Wrap the parameters in quotes    ::
  122. ::                                   ::
  123. ::  Example:                        
  124. ::      %0               %1                               %2           %3          %4            %5             %6
  125. ::    script          "Folder"                         "UnWanted"  "New Text"  "Add Preffix"  "Add Suffix"  "No Prompts"
  126. ::    bulk_rename.bat "%USERPROFILE%\Downloads\Videos"  "[Divx]"       ""          ""          "[JXE]"     "Trick Preeze"  
  127. ::
  128. ::                                  
  129. if not "%~1" equ "" set "bulkr_dir_with_files_to_rename=%~1"  rem :: First Param  = location where you'd like to rename the files.
  130. if not "%~2" equ "" set "bulkr_unwanted_string=%~2"           rem :: Second Param = the string that you would like replaced.
  131. if not "%~3" equ "" set "bulkr_new_string=%~3"                rem :: Third Param  = new string to replace the old one with.
  132. if not "%~4" equ "" set "bulkr_new_prefix=%~4"                rem :: Fourth Param = new Prefix, Example: [awesomesause]_
  133. if not "%~5" equ "" set "bulkr_new_suffix=%~5"                rem :: Fifth Param  = new Suffix, Example: _[awesomesause]
  134. if not "%~6" equ "" set "bulkr_bypass_all_prompts=%~6"        rem :: Sixth Param  = Prompted input? I don't even know her.
  135. goto :eof
  136. :::::::::::::::::::::::::::::::::::::::
  137.  
  138.  
  139.  
  140. :PROMPT_INPUT
  141. :: Prompted Variables :::::::::::::::::
  142. ::    Will Only be prompted          ::
  143. ::    if they're not already set     ::
  144. ::                                   ::
  145. ::
  146.  
  147. rem  Superceeded Bypass support
  148. if defined bulkr_auto_bypass_all_prompts (
  149.     set "bulkr_bypass_all_prompts=%bulkr_auto_bypass_all_prompts%"
  150. )
  151.  
  152. rem Bypass support
  153. if defined bulkr_bypass_all_prompts (
  154.     echo INFO: Prompts bypassed
  155.     call :func_set_file_totals
  156.     call :DUMP_PARAMETERS
  157. ) else (
  158.    rem Prompting for any unset variables
  159.     if not defined bulkr_dir_with_files_to_rename (
  160.         echo/
  161.         echo PROMPT: Enter the location where you'd like to rename the files:
  162.         echo            If left blank, defaults to [%bulkr_current_directory%]
  163.         set /p "bulkr_dir_with_files_to_rename=>"
  164.     )
  165.    
  166.     call :func_set_file_totals
  167.     call :func_set_sample_files
  168.    
  169.     if defined bulkr_sample_file1 (
  170.         echo/ Info: Cherry-Picked sample files for Copy-Paste ease
  171.    
  172.         for /l %%a in (1,1,5) do (
  173.             if defined bulkr_sample_file%%a echo/  !bulkr_sample_file%%a!
  174.         )
  175.     )
  176.  
  177.     if not defined bulkr_unwanted_string (
  178.         echo/
  179.         echo PROMPT: Enter the string that you would like replaced:
  180.         set /p "bulkr_unwanted_string=>"
  181.     )
  182.  
  183.     if not defined bulkr_new_string (
  184.         echo/
  185.         echo PROMPT: Enter the new string to replace the old one with:
  186.         set /p "bulkr_new_string=>"
  187.     )
  188.  
  189.     if not defined bulkr_new_prefix (
  190.         echo/
  191.         echo PROMPT: Enter the new Prefix, Example [or leave blank for none]:
  192.         set /p "bulkr_new_prefix=>"
  193.     )
  194.  
  195.     if not defined bulkr_new_suffix (
  196.         echo/
  197.         echo PROMPT: Enter the new Suffix, Example [or leave blank for none]:
  198.         set /p "bulkr_new_suffix=>"
  199.     )
  200.     echo/
  201.     echo -------------------------------------------------------
  202. )
  203. goto :eof
  204. :::::::::::::::::::::::::::::::::::::::
  205.  
  206.  
  207.  
  208. :ERROR_CHECK_1
  209. :: Basic Error Checking :::::::::::::::
  210. if not defined bulkr_dir_with_files_to_rename (
  211.     if defined bulkr_current_directory (
  212.         set "bulkr_dir_with_files_to_rename=%bulkr_current_directory%"
  213.     ) else (
  214.         set "bulkr_dir_with_files_to_rename=.\"
  215.     )
  216. )
  217.  
  218. rem Default Pause Timer Value
  219. if not defined bulkr_default_timeout_value (
  220.     set "bulkr_default_timeout_value=5"
  221. )
  222.  
  223. rem [bulkr_dir_with_files_to_rename] Cannot be empty
  224. if not defined bulkr_dir_with_files_to_rename (
  225.     echo Error: Fatal: Check-Variable: [^%bulkr_dir_with_files_to_rename^% is empty] Exiting...
  226.     goto :eof
  227. )
  228. goto :eof
  229. :::::::::::::::::::::::::::::::::::::::
  230.  
  231.  
  232. rem Scrubbing Var Counts
  233. rem for /f "tokens=1 delims==" %%a in ('set bulkr_count_file_') do set "%%a=0"
  234. :INITIALIZE
  235. rem pushd to the dir to read attribs
  236. if exist "!bulkr_dir_with_files_to_rename!" (
  237.     pushd "!bulkr_dir_with_files_to_rename!"
  238.     set /a "pushme_bulkr+=1"
  239. )
  240.  
  241.  
  242. rem Normal totals - Calc string length for prepadded zeros-- Using str length to "UnLazy" the "Lazy Zeroes"
  243. if defined bulkr_count_file_total (
  244.     if !bulkr_count_file_total! geq 10 (
  245.         set "bulkr_Pvar=!bulkr_count_file_total!#"
  246.         set "bulkr_padz_total=0"
  247.        
  248.         for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
  249.             if "!bulkr_Pvar:~%%P,1!" NEQ "" (
  250.                 set /a "bulkr_padz_total+=%%P"
  251.                 set "bulkr_Pvar=!bulkr_Pvar:~%%P!"
  252.             )
  253.         )
  254.     )
  255. )
  256.  
  257. rem Normal totals - Creating a working set of prepadded zeros from total -- deemed "Almost Lazy Zeros"
  258. if defined bulkr_padz_total (
  259.     for /l %%a in (1,1,!bulkr_padz_total!) do (
  260.         set "bulkr_padz=0!bulkr_padz!"
  261.     )
  262. ) else (
  263.     set "bulkr_padz="
  264. )
  265.  
  266. goto :eof
  267. :::::::::::::::::::::::::::::::::::::::
  268.  
  269. :: rem Hidden totals - Calc string length for prepadded zeros-- Using str length to "UnLazy" the "Lazy Zeroes"
  270. :: if defined bulkr_count_file_hidden_total (
  271. ::     if !bulkr_count_file_hidden_total! geq 10 (
  272. ::         set "bulkr_Pvar=!bulkr_count_file_hidden_total!#"
  273. ::         set "bulkr_hpadz_total=0"
  274. ::        
  275. ::         for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
  276. ::             if "!bulkr_Pvar:~%%P,1!" NEQ "" (
  277. ::                 set /a "bulkr_hpadz_total+=%%P"
  278. ::                 set "bulkr_Pvar=!bulkr_Pvar:~%%P!"
  279. ::             )
  280. ::         )
  281. ::     )
  282. :: )
  283. ::
  284. :: rem Hidden totals - Creating a working set of prepadded zeros from total -- deemed "Almost Lazy Zeros"
  285. :: if defined bulkr_hpadz_total (
  286. ::     for /l %%a in (1,1,!bulkr_padz_total!) do (
  287. ::         set "bulkr_hpadz=0!bulkr_padz!"
  288. ::     )
  289. :: ) else (
  290. ::     set "bulkr_hpadz="
  291. :: )
  292. ::
  293.  
  294. :: Renaming and erroring checking :::::
  295. ::   Allowing only Script Msgs       ::
  296. ::   All other outpout suppressed    ::
  297. rem working string :: set "bulkr_fname_new=%bulkr_new_prefix%!bulkr_fname_only:%bulkr_unwanted_string%=%bulkr_new_string%!%bulkr_new_suffix%!bulkr_fname_extention!"
  298. rem Expansion attributes
  299. rem   d ~0,1
  300. rem   - ~1,1
  301. rem   a ~2,1
  302. rem   h ~3,1
  303.  
  304. rem if wildcard is found process as wildcard - cmd limitations...
  305. rem this command is the key.  Needs to be in a onliner. call echo %string:%string:*[=[%=%
  306. :RENAME_LOOP
  307. for /f "tokens=1* delims=" %%A in ('dir /b /o:-n /a:-d "!bulkr_dir_with_files_to_rename!"') do (
  308.     set "bulkr_fname_only=%%~nA"
  309.     set "bulkr_fname_attrib=%%~aA"
  310.     (set "bulkr_fname_new=!bulkr_new_prefix!!bulkr_fname_only:%bulkr_unwanted_string%=%bulkr_new_string%!!bulkr_new_suffix!%%~xA")
  311.    
  312.     rem Making sure Replacement strings are removed if not defined
  313.     if not defined bulkr_unwanted_string (
  314.         if not defined bulkr_new_string (
  315.             set "bulkr_fname_new=%bulkr_new_prefix%!bulkr_fname_only!!bulkr_new_suffix!%%~xA"
  316.         )    
  317.     )
  318.  
  319.     if "[%%~nxA]" equ "[!bulkr_fname_new!]" (
  320.         set /a "bulkr_count_file_same+=1"
  321.         set /a "bulkr_count_file_current+=1"
  322.        
  323.         if defined bulkr_padz (
  324.             rem  Pre-pad value with zeros and strip un-needed extras
  325.             set "bulkr_pz_cntr=!bulkr_padz!!bulkr_count_file_current!"
  326.             set "bulkr_pz_cntr=!bulkr_pz_cntr:~-%bulkr_padz_total%!"
  327.            
  328.             echo Info: SameName [!bulkr_pz_cntr!/%bulkr_count_file_total%]: [!bulkr_fname_new!] - [Same]
  329.         ) else (
  330.             echo Info: SameName [!bulkr_count_file_current!/%bulkr_count_file_total%]: [!bulkr_fname_new!] - [Same]
  331.         )
  332.     ) else (
  333.         rem unhide hidden files
  334.         if /i "!bulkr_fname_attrib:~3,1!" equ "h" (
  335.             REM echo Info: Unhiding Hidden File: [%%~nxA]
  336.            
  337.             2>nul 1>nul attrib -H "%%~nxA"
  338.             if !errorlevel! equ 0 (
  339.                 set /a "bulkr_count_file_nhidden+=1"
  340.                 set "hiddenfile_bulkr!bulkr_count_file_nhidden!=%%~nxA"
  341.             )
  342.         )
  343.        
  344.         rem Making sure the old filename exists
  345.         if exist "%%~nxA" (
  346.             set /a "bulkr_count_file_current+=1"
  347.            
  348.             rem Making sure that the new filename does not exist
  349.             if not exist "!bulkr_fname_new!" (
  350.                 rem Moves OLD fname to NEW fname
  351.                 call %bulkr_rename_bin% "%%~nxA" "!bulkr_fname_new!" 2>nul 1>nul
  352.                
  353.                 rem Making sure that the new filename was created
  354.                 if exist "!bulkr_fname_new!" (
  355.                     if defined bulkr_padz (
  356.                         rem  Pre-pad value with zeros and strip un-needed extras
  357.                         set "bulkr_pz_cntr=!bulkr_padz!!bulkr_count_file_current!"
  358.                         set "bulkr_pz_cntr=!bulkr_pz_cntr:~-%bulkr_padz_total%!"
  359.                        
  360.                         echo Info: Success  [!bulkr_pz_cntr!/%bulkr_count_file_total%]: [!bulkr_fname_new!]
  361.                     ) else (
  362.                         echo Info: Success  [!bulkr_count_file_current!/%bulkr_count_file_total%]: [!bulkr_fname_new!]
  363.                     )
  364.                 ) else (
  365.                     echo/
  366.                     set /a "bulkr_count_file_error+=1"
  367.                     echo Error: Unable to find new filename - Path:[!bulkr_dir_with_files_to_rename!] File:[!bulkr_fname_new!]
  368.                 )
  369.             ) else (
  370.                 echo/
  371.                 set /a "bulkr_count_file_error+=1"
  372.                 echo Error: Destination File already exists ["!bulkr_fname_new!"]
  373.             )
  374.         ) else (
  375.             echo/
  376.             set /a "bulkr_count_file_error+=1"
  377.             echo Error: Internal Error. Unable to locate ["%%~nxA"]
  378.         )
  379.        
  380.         if defined hiddenfile_bulkr!bulkr_count_file_nhidden! (
  381.            
  382.             2>nul 1>nul attrib +H "!bulkr_fname_new!"
  383.             if !errorlevel! equ 0 (
  384.                 set "hiddenfile_bulkr!bulkr_count_file_nhidden!="
  385.             ) else (
  386.                 echo/
  387.                 set /a "bulkr_count_file_error+=1"
  388.                 echo Error: Internal Error. Unable to hide [attrib +H "!bulkr_fname_new!"]
  389.             )
  390.         )
  391.     )
  392. )
  393. goto :eof
  394. :::::::::::::::::::::::::::::::::::::::
  395.  
  396.  
  397. :SUMMARY
  398. :: Print Summary ::::::::::::::::::::::
  399. echo.
  400. echo -------------------------------------------------------
  401. call :DUMP_PARAMETERS
  402. echo -------------------------------------------------------
  403. echo.
  404. if !bulkr_count_file_error! gtr 0 (
  405.     echo Warning: Errors Found [%bulkr_count_file_error%]
  406. )
  407. echo SUMMARY:  Found:[!bulkr_count_file_total!]  Same:[!bulkr_count_file_same!]  Processed:[!bulkr_count_file_current!]  Hidden:[!bulkr_count_file_nhidden!]  Errors:[!bulkr_count_file_error!]
  408. goto :eof
  409. :::::::::::::::::::::::::::::::::::::::
  410.  
  411.  
  412.  
  413. :CLEANUP
  414. :: popd out of the pushd ::::::::::::::
  415. if defined pushme_bulkr (
  416.     for /l %%P in (1,1,!pushme_bulkr!) do (
  417.         popd "!bulkr_dir_with_files_to_rename!"
  418.     )
  419. ) else (
  420.     popd "!bulkr_dir_with_files_to_rename!"
  421. )
  422. :::::::::::::::::::::::::::::::::::::::
  423.  
  424. :: Changing typeset back to original ::
  425. call :encode_orig
  426. goto :eof
  427. :::::::::::::::::::::::::::::::::::::::
  428.  
  429.  
  430.  
  431. :DUMP_PARAMETERS
  432. :: Display User prompted variables ::::
  433. echo/INFO: Dumping all set parameters:
  434. echo/          Directory     :[!bulkr_dir_with_files_to_rename!]
  435. echo/          UnWanted Text :[%bulkr_unwanted_string%]
  436. echo/          Replace  Text :[%bulkr_new_string%]
  437. echo/          Add Prefix    :[%bulkr_new_prefix%]
  438. echo/          Add Suffix    :[%bulkr_new_suffix%]
  439. goto :eof
  440. :::::::::::::::::::::::::::::::::::::::
  441.  
  442.  
  443.  
  444. :::::::::::::::::::::::::::::::::::::::
  445. ::                                   ::
  446. ::    Changing typeset to utf8       ::
  447. ::    because we love everyone       ::
  448. ::     that's utf8 supported         ::
  449. ::       T.T, Everyone else          ::
  450. ::                                   ::
  451. :::::::::::::::::::::::::::::::::::::::
  452. :: Storing the original value
  453. ::  If not utf8, making it so
  454. :encode_utf8
  455. if not defined bulkr_orig_chcp call :set_bulkr_orig_chcp
  456. if !bulkr_orig_chcp! neq 437 (
  457.     chcp 437
  458.     if !errorlevel! neq 0 (
  459.         echo Error: Not Fatal: Unable to set UTF8 typeset...
  460.     )
  461. ) 2>nul 1>nul
  462.  
  463. goto :eof
  464. :::::::::::::::::::::::::::::::::::::::
  465.  
  466.  
  467.  
  468. :set_bulkr_orig_chcp
  469. if not defined bulkr_orig_chcp (
  470.     for /f "tokens=4 delims=: " %%a in ('chcp') do set "bulkr_orig_chcp=%%a"
  471. )
  472. goto :eof
  473. :::::::::::::::::::::::::::::::::::::::
  474.  
  475.  
  476.  
  477. :: If not orig, mskinh it so
  478. ::   if orig is not set, changing to oem (us default)
  479. ::   OneLiner - remind future self: Seemed like a good idea at the time
  480. :encode_orig
  481. if not defined bulkr_orig_chcp goto :eof
  482.  
  483. for /f "tokens=4 delims=: " %%a in ('chcp') do (
  484.     if not %%~a geq 0 (
  485.         set "bulkr_current_chcp="
  486.         goto :eof
  487.     ) else (
  488.         set "bulkr_current_chcp=%%a"
  489.     )
  490. )
  491.  
  492. if !bulkr_current_chcp! neq !bulkr_orig_chcp! (
  493.     chcp !bulkr_orig_chcp! 2>nul 1>nul
  494.     if !errorlevel! neq 0 (
  495.         echo Error: Not Fatal: Unable to set !bulkr_orig_chcp! typeset...
  496.  
  497.         chcp 65001
  498.         if !errorlevel! neq 0 (
  499.             echo Error: Not Fatal: Unable to set 65001 typeset...
  500.         )
  501.     )
  502. )
  503.  
  504. goto :eof
  505. :::::::::::::::::::::::::::::::::::::::
  506.  
  507.  
  508.  
  509. rem unused atm -- bulkr_count_file_hidden_total
  510. rem Scrubbing Var Counts -- sets all counts to zero
  511. :func_reset_counters
  512. for %%v in (
  513.     bulkr_count_file_nhidden
  514.     bulkr_count_file_current
  515.     bulkr_count_file_total
  516.     bulkr_count_file_error
  517.     bulkr_count_file_same
  518. ) do (
  519.     set "%%v=0"
  520. )
  521. goto :eof
  522.  
  523.  
  524.  
  525. REM for /f "tokens=1* delims=" %%A in ('dir /b /o:-n /a:h /a:-d "!bulkr_dir_with_files_to_rename!" 2^>nul') do (
  526.     REM set /a "bulkr_count_file_hidden_total+=1"
  527. REM )
  528. rem Getting total number of hidden files
  529. rem Getting total number of normal files
  530. :func_set_file_totals
  531.  
  532. for /f "tokens=1* delims=" %%A in ('dir /b /o:-n /a:-d "!bulkr_dir_with_files_to_rename!"') do (
  533.     set /a "bulkr_count_file_total+=1"
  534. )
  535. goto :eof
  536.  
  537.  
  538.  
  539. rem Getting Sample file name set for easy copy-pasta
  540. :func_set_sample_files
  541. if not defined bulkr_count_file_total goto :eof
  542.  
  543. set /a "bulkr_s1=(!bulkr_count_file_total! / 6)"
  544. for /l %%a in (2,1,5) do (
  545.     set /a "bulkr_s%%a=!bulkr_s1! * %%a"
  546. )
  547.  
  548. set "bulkr_cntr=0"
  549. if !bulkr_count_file_total! geq 6 (
  550.    
  551.     for /f "tokens=1* delims=" %%A in ('dir /b /o:-n /a:-d "!bulkr_dir_with_files_to_rename!"') do (
  552.         set /a "bulkr_cntr+=1"
  553.  
  554.                if !bulkr_s1! equ !bulkr_cntr! ( set "bulkr_sample_file1=%%~nxA"
  555.         ) else if !bulkr_s2! equ !bulkr_cntr! ( set "bulkr_sample_file2=%%~nxA"
  556.         ) else if !bulkr_s3! equ !bulkr_cntr! ( set "bulkr_sample_file3=%%~nxA"
  557.         ) else if !bulkr_s4! equ !bulkr_cntr! ( set "bulkr_sample_file4=%%~nxA"
  558.         ) else if !bulkr_s5! equ !bulkr_cntr! ( set "bulkr_sample_file5=%%~nxA"
  559.         )
  560.     )
  561. ) 2>nul else (
  562.     for /f "tokens=1* delims=" %%A in ('dir /b /o:-n /a:-d "!bulkr_dir_with_files_to_rename!" 2^>nul') do (
  563.         set /a "bulkr_cntr+=1"
  564.         set "bulkr_sample_file!bulkr_cntr!=%%~nA"
  565.     )
  566. )
  567.  
  568. set "bulkr_cntr="
  569. goto :eof
  570.  
  571.  
  572.  
  573. :: Calling For User Input :::::::::::::
  574. ::   Ignored if:                     ::
  575. ::     "bulkr_bypass_all_prompts=1"  ::
  576. ::     Present Hardcoded Values      ::
  577. ::     Passed Parameters             ::
  578. ::                                   ::
  579. :: Checking for the help parameter   ::
  580. ::   Dumping Help Menu If needed     ::
  581. ::
  582. :help_check
  583. if "%~1" equ "" goto :eof
  584.  
  585.      if /i "%~1" equ "/h"    ( call :helpMe
  586. else if /i "%~1" equ "/help" ( call :helpMe
  587. else if /i "%~1" equ "-h"    ( call :helpMe
  588. else if /i "%~1" equ "-help" ( call :helpMe
  589. else if ["%~1"]  equ ["/?"]  ( call :helpMe
  590. else if ["%~1"]  equ ["-?"]  ( call :helpMe
  591. )
  592.  
  593. echo/
  594. goto :eof
  595. :::::::::::::::::::::::::::::::::::::::
  596.  
  597.  
  598.  
  599. :helpMe
  600. :: Help Menu ::::::::::::::::::::::::::
  601. set "bulkr_shown_help_flag=true"
  602. echo/
  603. echo USAGE: bulk_rename.bat ["Folder" "UnWanted" "New Text" "Add Preffix" "Add Suffix" "No Prompts"]
  604. echo           bulk_rename.bat [/^?^|/h^|-h^|-^?] for help
  605. echo/
  606. echo       {^*} Add/Remove/Replace Bulk file names.  Directories not supported at this point
  607. echo/
  608. echo       {^*} Mix and Match Four Input Meathods
  609. echo            In weighted order from TOP to BOTTOM, where top superceeds/overwrites bottom:
  610. echo             - Parameters: Pass Parameters when calling script; overwrites all below
  611. echo             - Hardcoded:  Bake defaults into the script; overwrites all below  
  612. echo             - Predefined: Predefined variables in the shell; overwrites below
  613. echo             - Prompted:   Prompts user for input, unless bypassed
  614. echo/
  615. echo/
  616. echo       {^*} Parameter Input Meathod:
  617. echo            - First Param  = location where you'd like to rename the file(s).
  618. echo            - Second Param = the string that you would like replaced.
  619. echo            - Third Param  = New string to replace the old one with.
  620. echo            - Fourth Param = new Prefix, Example: [awesomesause]_
  621. echo            - Fifth Param  = new Suffix, Example: _[awesomesause]
  622. echo            - Sixth Param  = Prompted input? I don't even know her.
  623. echo/
  624. echo       {^*} Usage Example: Remove [Divx] tag;  Append Suffix " [JXE]"  and bypass all prompts
  625. echo            - bulk_rename.bat "^%USERPROFILE^%\Downloads\Videos"  "[Divx]"  ""  ""  " [JXE]"  "Trick Preeze"
  626. echo/
  627. echo       {^*} Usage Example: Replace all Double Spaces with Single Spaces and bypass all prompts
  628. echo            - bulk_rename.bat "^%USERPROFILE^%\Downloads\Videos"  "  "  " "  ""  ""  "Trick Preeze"
  629. echo/
  630. echo       {^*} Usage Example: Prompt for all fields
  631. echo            - bulk_rename.bat
  632. echo/
  633. echo       {^*} Parameter Input Meathod:
  634. echo            - Batch/cmd - Use (please note the quotes): set "[variable below]=[value]"
  635. echo               # bulkr_dir_with_files_to_rename
  636. echo               # bulkr_unwanted_string
  637. echo               # bulkr_new_string
  638. echo               # bulkr_new_prefix
  639. echo               # bulkr_new_suffix
  640. echo               # bulkr_bypass_all_prompts
  641. echo/
  642. goto :eof
  643. :::::::::::::::::::::::::::::::::::::::
  644.  
  645. :::::::::::::::::::::::::::::::::::::::
  646. ::
  647. ::  ----------------------------------------
  648. ::   Thanks, Sources, and/or [Sourced-]Contributions
  649. ::  ----------------------------------------
  650. ::   + Feature: Prepadded Zeros
  651. ::    ~> Credited Author: jeb         - https://stackoverflow.com/users/463115/jeb
  652. ::     * Determine string length     -- https://stackoverflow.com/a/19101275
  653. ::     * "Lazy Zeros"                -- https://stackoverflow.com/a/9430912
  654. ::  ----------------------------------------
  655. ::
  656. ::
  657. ::  ----------------------------------------
  658. ::   Version History
  659. ::  ----------------------------------------
  660. ::   * 09/12/2016: #Fixed Param Process order; #Added More info in 'help'; #Fixed Possible issue of blank params overwriting all other input methods
  661. ::   * 07/12/2017: #Added bulkr_ prefix to all params to unset all vars easily; #Added setting to allow param pre-sets so certain vars are not cleared
  662. ::   * 07/23/2017: #Changed default var [bulkr_current_directory] to %cd%; #Fixed Issue where script vars were being cleared #Added Padded current count.
  663. ::   * 08/02/2017: #Fixed the weird bug when files contained certain special characters; #Rewrote chcp functions and changed folder detection to increase script speed.
  664. ::  ----------------------------------------
  665. ::
  666. ::
  667. ::  ----------------------------------------
  668. ::   Blah Blah Blah -- Notes -- Bleh  *hic*
  669. ::  ----------------------------------------
  670. ::   @ 07/23/2017 ~ I finally admit it, this code is ugly
  671. ::   After modifying this script these past couple of weeks, it seemed that there were too many useless blocks
  672. ::   And trying to follow the execution order was a confusing mess.
  673. ::   I've come to the conclusion that this script will need to be completely rewritten.
  674. ::   Focus would be on:
  675. ::    - Expanding the feature set - rename dirs, recurse via tree, hidden/system/special files, etc
  676. ::       - Nothing too M$Crazy, but keeping "unix-style apps" in mind
  677. ::       - Args parse pl9x
  678. ::    - Faster - (I'm no expert, so bare with me)
  679. ::    - Overall Structure Flow - (I won't dare say "better"/"improved"/etc)
  680. ::  
  681. :::::::::::::::::::::::::::::::::::::::
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment