Batch file for renaming folders

June 7, 2007 at 20:05:36
Specs: XP, NA

I am looking to rename folders at a specified location but only want to rename them if a text string exists in the folder name. I have another app that creates a set of randomly generated folder names based on current %COMPUTERNAME%. These could all be located in C:\MyFolder

I would like to rename the following example to the examples below:

C:\MyFolder\OldCompNmTEST
C:\MyFolder\HHOldCompNmT
C:\MyFolder\TESTOldCompNm
C:\MyFolder\OldCompNmTEST123

would be renamed to:

C:\MyFolder\NEWCompNmTEST
C:\MyFolder\HHNEWCompNmT
C:\MyFolder\TESTNEWCompNm
C:\MyFolder\NEWCompNmTEST123

Any ideas? Thanks in advance

I found this thread from a while back which is on the right track but only replaces the first three characters of each folder.

@Echo Off

:: Fix.cmd Syntax: Fix Folder_Name String
:: Fix C:\MyFolder 180

Set Fld=%1
Set Str=%2

For /F %%a in ('Dir /B /AD-H %1') Do Call :REN %%a
GoTo :EOF

:REN
Set RDir=%*
Set RDir=%Str%%RDir:~3%
Echo Move "%Fld%\%*" "%Fld%\%RDir%"
GoTo :EOF


See More: Batch file for renaming folders

Report •


#1
June 7, 2007 at 20:07:28

Sorry, forgot to clearly note that the old computer name in the example above = OldCompName

New, current %COMPUTERNAME% (Computer Name) = NEWCompName


Report •

#2
June 7, 2007 at 20:35:07

And once again, sorry to be a bug but this is basically a batch level "find and replace" for FOLDER NAMES rather than file contents. It will search for a text string in any set of folder names and if the text string exists, it will rename that folder by substituting the old text string with the new text string, resulting in the new folder name.

C:\11111OLDNAME11111
C:\11111NEWNAME11111

Thanks again


Report •

#3
June 7, 2007 at 22:58:28

Set FSO = CreateObject("Scripting.FileSystemObject")
Set Folder = FSO.GetFolder("c:\myFolder")
For Each efile in Folder.Files
'Wscript.Echo efile
If InStr(efile,"Old") Then
newName = Replace(efile,"Old","NEW")
FSO.MoveFile efile, newName
End If
Next

Report •

Related Solutions

#4
June 7, 2007 at 23:39:15

Well, I took that script you provided, kept the GoTo's and :REN, and pretty much rewrote the rest. Here's what I ended up with:


@Echo Off
GoTo Begin
Obviously, this takes 3 (1 optional) arguments. If you don't specify
the first (the search directory), it'll use the current directory. If
you don't have at least two arguments, the script gracefully crashes.

Mod'ed by: Razor2.3
Created by: Donno, but gotta give credit where credit's due.

Arguments:
%1 = Directory to search (Optional)
%2 = Find what
%3 = Replace with

:Begin
SetLocal EnableDelayedExpansion
If (%2)==() exit /b 1
If (%3)==() %0 . %*

For /D %%a in (%1\*.*) Do Call :REN "%%~Fa" %2 %3
GoTo :EOF

:REN
Set RDir=%1
Move %RDir% !RDir:%2=%3!
GoTo :EOF

ghostdog:
You can simplify your For Each loop by doing the following:


For Each efile in Folder.Files
efile.Name = Replace(efile,"Old","NEW")
Next

I only bring it up because I assume my fellow scripters are as lazy as me.


Report •

#5
June 7, 2007 at 23:43:33

i also just realize OP wants to rename folders and not files.

Report •

#6
June 8, 2007 at 02:27:01

" I only bring it up because I assume my fellow scripters are as lazy as me."

LOL


=====================================
If at first you don't succeed, you're about average.

M2



Report •

#7
June 9, 2007 at 10:48:52

Actually, I beleive both of these solutions are for renaming files, not folders....

Report •

#8
June 9, 2007 at 15:25:13

Well, you could at least try them before dismissing them.

For /D %%a in

The command script file only looks at directories, and the VBScript can be modified to work by changing .Files to .Subfolders.


Report •

#9
June 9, 2007 at 15:33:26

I did try them. Thank you. Also, I really need a bacth file for this request - purpose (per the title).
I created several test scenarios. C:\test\folder1, C:\test\folder2, C:\test\folder3. I ran the batch file above with the following syntax and parameters: rename.bat c:\test olde %computername%.

The end result should have been renaming the three folders and replacing the text string of "olde" with the current computer name.

The folder names were not changed.

I will go make the modifications that you suggested and try again.

EDIT: I figured out what happened (or must have happened) I first ran the WScript and it didn't do the trick. Well, it must have left a lock on the folders or something so when I then tested the batch file it also failed. I removed the echo off and I see that the return message is that "The process cannot access the file because it is in use by another application".

When doing another test scenario, the batch file does now work properly. Thank you!!


Report •

#10
June 9, 2007 at 19:43:22

::== rendir4.bat
:: rendir

@echo off
setLocal EnableDelayedExpansion

pushd C:\MyFolder
for /f "tokens=* delims= " %%a in ('dir /b/ad') do (
set x=%%a
set y=!x:OldComp=NEWComp!
ren !x! !y!
)
::==

=============================
result:
C:\temp\->tree \myfolder
Folder PATH listing for volume 2G_40
Volume serial number is 0006FE80 3318:1605
C:\MYFOLDER
├───OldCompNmTEST
├───HHOldCompNmT
├───TESTOldCompNm
└───OldCompNmTEST123

C:\temp\->tree \myfolder
Folder PATH listing for volume 2G_40
Volume serial number is 0006FE80 3318:1605
C:\MYFOLDER
├───NEWCompNmTEST
├───HHNEWCompNmT
├───TESTNEWCompNm
└───NEWCompNmTEST123


=====================================
If at first you don't succeed, you're about average.

M2



Report •


Ask Question