Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vbscript to rename subfolders

Status
Not open for further replies.

foxfire76

Technical User
Apr 5, 2011
5
US
I'm looking for help with a rename script. I know enough to tweak ones that are close, but I'm not having any luck finding one close to what I need. I have a text file with a column for a number x and a column with a folder name. Could be any number or any name. For example:
5 abc1234
6 abc4321

The number in the first column is not important. I need a script that will use my text file output to look for all folder names listed in a path, like c:\test, and rename the folders to the original name + old. For example:
abc1234 would be renamed abc1234old and
abc4321 would be renamed abc4321old.

The script doesn't have to check if the folder exists, just rename all the names from the text file. Any help would be greatly appreciated.


 
1. open the txt file
2. readin data line by line
3. parse data to get path
4. get folder object
5. rename folder

Code:
set objFSO = WScript.CreateObject("Wscript.FileSystemObject")
set objStream = objFSO.OpenTextFile("C:\paths.txt", 1, true, 0)

do while noot (objStream.AtEndOfStream)
   strLine = objStream.ReadLine
   strPath = right(strLine, len(strLine) - inStrRev(strLine, " ") - 1)

   set objFolder = objFSO.GetFolder(strPath)
   objFolder.Name = objFolder.Name & "old"
loop

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
Thanks so much for the reply. It works, but I am getting Line 8 Char 4 Path not found Code: 800A004C. I did change the text file output to 5 : abc1234. Here's the code I'm using now. It does change the folder name, but I get the above error. Thanks again for any help.

set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
set objStream = objFSO.OpenTextFile("C:\TEST\result.txt", 1, true, 0)

do while not (objStream.AtEndOfStream)
strLine = objStream.ReadLine
strPath = Right(strLine, Len(strLine) - inStrRev(strLine, ":") -1)

set objFolder = objFSO.GetFolder(strPath)
objFolder.Name = objFolder.Name & "old"
loop
 
my guess is that strPath = Right(strLine, Len(strLine) - inStrRev(strLine, ":") -1) is producing an invalid path. Throw a msgbox after it to see what strPath is.

Keep in mind, that after you run it once, it will not work again as all folders suggested in the result.txt file are already renamed. This could also be the cause of the error.

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
I guess you should replace this:
set objFolder = objFSO.GetFolder(strPath)
with this:
set objFolder = objFSO.GetFolder("C:\TEST\" & strPath)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I guess I didn't think this one through. I do need to run this script more than once. What would it take to verify the files existence before renaming?
 
Use the FolderExists method.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Code:
if (objFSO.FolderExists(strPath)) then
   msgbox "yah!"
else
   msgbox "boo :("
end if

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
OK. I'm still working on this. The first run everything works. The second run is not working correctly. Here's what happens. The first run of the script all folders rename correctly. If I add a foldername to my text file it does not get renamed the next run. Here's the code I'm using now. The way I read it is if the folder name exists in the text file perform the rename. If not do nothing. What am I doing wrong?

dim strPath
strPath = "c:\Test"
set filesys = CreateObject("Scripting.FileSystemObject")

set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
set objStream = objFSO.OpenTextFile("C:\TEST\result.txt", 1, true, 0)
If (objFSO.FolderExists(strPath)) Then

do while not (objStream.AtEndOfStream)
strLine = objStream.ReadLine
strPath = Right(strLine, Len(strLine) - inStrRev(strLine, ":") -1)

set objFolder = objFSO.GetFolder(strPath & "\")
objFolder.Name = objFolder.Name & "old"
loop
Else
End If
 
Put the test inside the loop

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV is right. If the test is on the outside of the loop, it will only test the one time. If the test is inside the loop, it will test everytime.

Code:
    do while not (objStream.AtEndOfStream)
        strLine = objStream.ReadLine
        strPath = Right(strLine, Len(strLine) - inStrRev(strLine, ":") - 1)
        [red]If (objFSO.FolderExists(strPath)) Then[/red]
            set objFolder = objFSO.GetFolder(strPath & "\")
            objFolder.Name = objFolder.Name & "old"
        [red]end if[/red] 
    loop

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
Thanks so much to both of you. Everything works as requested.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top