Computing.Net > Forums > Programming > Batch file for renaming folders

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file for renaming folders

Reply to Message Icon

Name: rbsterli
Date: June 7, 2007 at 20:05:36 Pacific
OS: XP
CPU/Ram: NA
Product: Dell
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: rbsterli
Date: June 7, 2007 at 20:07:28 Pacific
Reply:

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

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


0

Response Number 2
Name: rbsterli
Date: June 7, 2007 at 20:35:07 Pacific
Reply:

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


0

Response Number 3
Name: ghostdog
Date: June 7, 2007 at 22:58:28 Pacific
Reply:

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


0

Response Number 4
Name: Razor2.3
Date: June 7, 2007 at 23:39:15 Pacific
Reply:

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.


0

Response Number 5
Name: ghostdog
Date: June 7, 2007 at 23:43:33 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: June 8, 2007 at 02:27:01 Pacific
Reply:

" 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



0

Response Number 7
Name: rbsterli
Date: June 9, 2007 at 10:48:52 Pacific
Reply:

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


0

Response Number 8
Name: Razor2.3
Date: June 9, 2007 at 15:25:13 Pacific
Reply:

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.


0

Response Number 9
Name: rbsterli
Date: June 9, 2007 at 15:33:26 Pacific
Reply:

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!!


0

Response Number 10
Name: Mechanix2Go
Date: June 9, 2007 at 19:43:22 Pacific
Reply:

::== 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



0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file for renaming folders

DOS batch file for deleting folders www.computing.net/answers/programming/dos-batch-file-for-deleting-folders/9005.html

Batch file for renaming files www.computing.net/answers/programming/batch-file-for-renaming-files/15423.html

batch for renaming www.computing.net/answers/programming/batch-for-renaming/14941.html