SOLVED Batch renaming folders with names containing spaces (using CMD)


Joined
Apr 16, 2017
Messages
49
Reaction score
10
Hi all.

I have a large number of folders which I want to batch rename if possible. The folders all end in "318_PANA", and I want them to end in "108_PANA". Example of full (present) folder name: "2020.02.16 318_PANA".

Many years ago, when I got my first windows computer, I batch renamed huge numbers of files (not folders), using the command prompt. However, that was files with 8-digit DOS-names, so there is really no comparison :).

Hope you can help.

Best regards
Jeppe
 
Ad

Advertisements

Joined
Nov 28, 2015
Messages
93
Reaction score
11
I don't know of any simple command line method but if you try a search for "batch rename folders utility", several free solutions appear, including Bulk Rename Utility.
 
Joined
Apr 16, 2017
Messages
49
Reaction score
10
Thanks to both of you for the imput.

I used Bulk Rename Utility for the job, weegmand, and it went smoothly, when I had got the part which was to be replaced written correctly in the Replace field. Thank you.

Best regards
Jeppe
 
Joined
Apr 22, 2017
Messages
3,464
Reaction score
431
Hi Jeppe,

please mark the thread as solved so it helps others. :)
 
Ad

Advertisements

Joined
Feb 22, 2014
Messages
1,442
Reaction score
291
This has peaked my curiosity. I'm trying to script something now. It is one of my home hobbies.

But it seems I would be better off scripting in another language. So simple getting the string length in other languages, yet it is near impossible in cmd. Need to be able to pull string length. I can loop through the folder names, but have no control of which characters to change. I'm currently trying to loop through a string char by char till I reach an error and counting each loop for a length.
 
Ad

Advertisements

Joined
Feb 22, 2014
Messages
1,442
Reaction score
291
After running into a few walls I finally managed to get a script to working. The script will prompt for user input. After the user inputs the current and new text. The script will echo a single line to VBS which is needed for finding file length. Then I ran into a issue calling variables within variables. I saw no other option than to build my final variables in CMD.

The script will find and isolate the text given. And then rename regardless whether it is to the left, middle, or right.

For the record special characters can be used but the script will not run correctly using "&" in the text. The "&" sign is a special end of line character for CMD.

Code:
@Echo off&COLOR F0&SET sp=      
REM _________________________________Initial Variables____________________________________
  SET /P Current=.%sp%Enter Current Text : &  SET /P     New=.%sp%Enter New Text     :
  Call :SubStrLen %Current%
  SET OldLen=%StrLen%
  Call :SubStrLen %new%    
  SET NewLen=%StrLen%

REM _____________________________Main Loop for all Folders________________________________
  FOR /F "tokens=1,2* delims==" %%a in ('DIR /B/A:D "*%Current%*.*"') DO (
    Set RenString=%%~a&  Call :SubStrLen "%%~a"&  Call :SubStrPos "%%~a")
  If Exist TempRunRenameScript.* Del /Q TempRunRenameScript.*
  Goto :EOF

:SubStrLen
REM ______________________________Sub Find String Length__________________________________
  Echo.WScript.Echo Len("%~1")>TempRunRenameScript.vbs
  FOR /F "tokens=1,2* delims==" %%a in ('cscript //Nologo "TempRunRenameScript.vbs"') DO (
    SET StrLen=%%~a)
  Goto :EOF

:SubStrPos
REM _____________________________Sub Find String Position_________________________________
  Echo.WScript.Echo InStr("%~1","%Current%")>TempRunRenameScript.vbs
  FOR /F "tokens=1,2* delims==" %%a in ('cscript //Nologo "TempRunRenameScript.vbs"') DO (
    SET StrStart=%%~a)
  Set /A LStart=0&                  Set /A LEnd=%StrStart%-1
  Set /A MStart=%StrStart%-1&       Set /A MEnd=%OldLen%
  Set /A RStart=%MStart%+%OldLen%&  Set /A REnd=%StrLen% - %MEnd%
REM ______________________________Build Renaming Command__________________________________
  Echo.Set LString=%%RenString:~%LStart%,%LEnd%%%>TempRunRenameScript.cmd
  Echo.Set MString=%%RenString:~%MStart%,%MEnd%%%>>TempRunRenameScript.cmd
  Echo.Set RString=%%RenString:~%RStart%,%REnd%%%>>TempRunRenameScript.cmd
  Call TempRunRenameScript.cmd
  Ren "%RenString%" "%LString%%New%%RString%"
  Goto :EOF

REM ____________________________________End of File_______________________________________
:EOF
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top