Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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\OldCompNmTEST123would be renamed to:
C:\MyFolder\NEWCompNmTEST
C:\MyFolder\HHNEWCompNmT
C:\MyFolder\TESTNEWCompNm
C:\MyFolder\NEWCompNmTEST123Any 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 180Set Fld=%1
Set Str=%2For /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

Sorry, forgot to clearly note that the old computer name in the example above = OldCompName
New, current %COMPUTERNAME% (Computer Name) = NEWCompName

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:\11111NEWNAME11111Thanks again

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

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 :EOFghostdog:
You can simplify your For Each loop by doing the following:
For Each efile in Folder.Files
efile.Name = Replace(efile,"Old","NEW")
NextI only bring it up because I assume my fellow scripters are as lazy as me.

" 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

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.

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

::== rendir4.bat
:: rendir@echo off
setLocal EnableDelayedExpansionpushd 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
└───OldCompNmTEST123C:\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

![]() |
![]() |
![]() |

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