Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Script to rename lots of folders

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,34580
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Script to rename lots of folders
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 Script to rename lots of folders - 5/17/2006 3:51:48 AM   
  thinicer

 

Posts: 18
Score: 0
Joined: 5/17/2006
Status: offline
Hello all,

I cannot figure out how to rename a bunch of folders in a directory from one naming convention to another. It's not as simple as adding a 1 in the front, or at the end of its name. Certain things need to be removed and added.

Here is an example of a directory that needs to be renamed:

Existing Folder: 05-0009.P0 Alpha Project
New Folder: 050009.00.P0 Alpha Project
 
Here is another folder:

Existing Folder: 01-0015.26 Beta Agreement
New Folder: 010015.00.26 Beta Agreement

As you can see, the new naming convention removes the hyphen and adds a .00 in the middle of the folder name.

The existing folders all have the same naming convention, that is....##-####.## Project Name and they need to be renamed to ######.00.## Project Name.

I would really appreciate it if somebody could assist me with a VBS script. I've exhausted many hours of research online to figure out how to do this, and the only scripts I have come across are ones that make all the folders in a directory lowercase, or adding a character to the beginning or end of the folder name.

Thank you all in advance.

- Michael
 
 
Post #: 1
 
 RE: Script to rename lots of folders - 5/17/2006 4:14:46 AM   
  ehvbs

 

Posts: 2204
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi thinicer,

try this:


      

(in reply to thinicer)
 
 
Post #: 2
 
 RE: Script to rename lots of folders - 5/17/2006 4:29:48 AM   
  thinicer

 

Posts: 18
Score: 0
Joined: 5/17/2006
Status: offline
Hi ehvbs,

Thanks for the quick reply. I'm pretty green to the art of scripting.

Is it necessary for me to create an array of all the hundreds of folder names in the script, as well as their new names? This part has me a bit confused.

(in reply to ehvbs)
 
 
Post #: 3
 
 RE: Script to rename lots of folders - 5/17/2006 4:48:21 AM   
  ehvbs

 

Posts: 2204
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi thinicer,

The script test the method
  
   sMsg = Left( sMsg, 2 ) + Mid( sMsg, 4, 4 ) + ".00" + Mid( sMsg, 8 )

to change a name against the two examples you posted. You should add
some other test cases (old name - expected new name) to aTests and
see if the method still gives correct results.

(in reply to thinicer)
 
 
Post #: 4
 
 RE: Script to rename lots of folders - 5/17/2006 4:55:26 AM   
  thinicer

 

Posts: 18
Score: 0
Joined: 5/17/2006
Status: offline
The script came back with the following results:

05-0009.P0 Alpha Project        050009.00.P0 Alpha Project      True
01-0015.26 Beta Agreement       010015.00.26 Beta Agreement     True

(in reply to ehvbs)
 
 
Post #: 5
 
 RE: Script to rename lots of folders - 5/17/2006 5:03:28 AM   
  thinicer

 

Posts: 18
Score: 0
Joined: 5/17/2006
Status: offline
I tried two other folder names, and they both completed successfully.

(in reply to thinicer)
 
 
Post #: 6
 
 RE: Script to rename lots of folders - 5/17/2006 5:11:23 AM   
  ehvbs

 

Posts: 2204
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
yes - because these two sample inputs are renamed by the string manipulation method
to the expected results. If you are confident that these samples are representative
for (?) your hundreds  of folder names, integrate something like

  sOldName = <name of folder to rename>
  sNewName = Left( sOldName, 2 ) + Mid( sOldName, 4, 4 ) + ".00" + Mid( sOldName, 8 )

in your script.

(in reply to thinicer)
 
 
Post #: 7
 
 RE: Script to rename lots of folders - 5/17/2006 6:26:17 AM   
  thinicer

 

Posts: 18
Score: 0
Joined: 5/17/2006
Status: offline
Thanks for the help. It works great!

(in reply to ehvbs)
 
 
Post #: 8
 
 RE: Script to rename lots of folders - 5/17/2006 7:15:15 AM   
  thinicer

 

Posts: 18
Score: 0
Joined: 5/17/2006
Status: offline
Hey ehvbs,

I guess I spoke too soon. While the script works, I didn't count on something else.

All these folders are on a DFS. The script I made, instead of replicating just the simple file name change (2 KB), it is replicating ALL of the data in the folders, gigs and gigs worth.

Do you know if any way to write a script that will just do a simple name change but won't replicate all the data in the folders? If I were to do this manually, by highlighting the folder and manually changing the name using explorer, it would not force a complete replication.

Here is the script I'm working. Most of the code comes from another post on this forum:

Dim fso,f,fc
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("c:\test")
Set fc = f.SubFolders

For Each f1 in fc
      RenameFolder(f1.Name)  
Next

Sub RenameFolder(folder)
'Will rename directory to new naming convention
   strOldname = folder
   strNewFolder = Left( strOldName, 2 ) + Mid( strOldName, 4, 4 ) + ".00" + Mid( strOldName, 8 )
   f1.Name = strNewFolder
End Sub

Would be great if it didn't replicate all the data!

Thanks for all your help with this.

- Michael

(in reply to ehvbs)
 
 
Post #: 9
 
 RE: Script to rename lots of folders - 5/17/2006 8:15:21 AM   
  thinicer

 

Posts: 18
Score: 0
Joined: 5/17/2006
Status: offline
You know what? this script would probably work with DFS if it is run from the directory where it is stored, excluding all other areas.

I only want it to focus on one folder at a time, but I don't believe it is doing this.

So how do I set this script to focus on the current folder where the vbs file is?

(in reply to ehvbs)
 
 
Post #: 10
 
 RE: Script to rename lots of folders - 5/17/2006 8:19:09 AM   
  ebgreen


Posts: 5070
Score: 31
Joined: 7/12/2005
Status: online
Change this:

Set f = fso.GetFolder("c:\test")

to this:

Set oWSHl = WScript.CreateObject("WScript.Shell")
strPWD = oWSHl.
CurrentDirectory
Set f = fso.GetFolder(strPWD)

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to thinicer)
 
 
Post #: 11
 
 RE: Script to rename lots of folders - 5/17/2006 8:51:43 AM   
  ehvbs

 

Posts: 2204
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi thinicer,

I used

      
to verify that Folder.Name works as advertised in the VBScript Docs. I know
that rename and move are related and that to move to another drive means
copy. So what exactly is this DFS?
Your script shouldn't access anything outside c:\temp. Do you have links
in your folder tree?
Perhaps you should put some WScript.Echo Folder.Path in your code.
Sorry that I have no solution for your problem.

(in reply to ebgreen)
 
 
Post #: 12
 
 RE: Script to rename lots of folders - 5/17/2006 9:09:39 AM   
  thinicer

 

Posts: 18
Score: 0
Joined: 5/17/2006
Status: offline
DFS = Distributed File System

DFS is used on WANs so that data between LANs can be replicated to each other. When somebody makes a change on one network, that change is then reflected in the other. All these folders that need to be renamed are on a DFS. Renaming a folder with a very simple click with the mouse, plus a couple of keyboard strokes, using windows explorer only replicates 2 kb of data. However, using the script I had before is causing all the data from the root DFS directory to replicate, all gigs and gigs worth. Weird.

Thanks for all your help and effort guys. I'll give these scripts a shot. If they don't work, I'm going to declare defeat simply because Saturday is when we're supposed to rename all these folders.

This was definitely a great learning experience.

(in reply to ehvbs)
 
 
Post #: 13
 
 RE: Script to rename lots of folders - 5/17/2006 11:43:21 PM   
  dalemontgomery45177

 

Posts: 26
Score: 0
Joined: 5/11/2006
Status: offline
If the vbs 'rename and move are related ' then perhaps you can try using the REN command via command line prompt. Test it manually and if successful, the script you have can be adjusted to call the REN command do do the actual renaming.

(in reply to thinicer)
 
 
Post #: 14
 
 RE: Script to rename lots of folders - 5/18/2006 11:38:42 PM   
  thinicer

 

Posts: 18
Score: 0
Joined: 5/17/2006
Status: offline
ebgreen, thank you very much for your contribution. It solved my problem. The script now executes within the directory and it doesn't cause a massive replication of all data in DFS. It just changes the names of the folders like it is supposed to do.

Outstanding! Thanks again!


(in reply to ebgreen)
 
 
Post #: 15
 
 RE: Script to rename lots of folders - 5/18/2006 11:39:57 PM   
  thinicer

 

Posts: 18
Score: 0
Joined: 5/17/2006
Status: offline
ehvbs, thank you also for your code that helped me rename the folders to the convention I want. Both you and ebgreen helped save my department and our operations team lots and lots of time.

THANKS! :)

(in reply to ehvbs)
 
 
Post #: 16
 
 RE: Script to rename lots of folders - 5/19/2006 1:20:03 AM   
  ehvbs

 

Posts: 2204
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi thinicer,

thank you for your kind words. You are most welcome. Just because the
move/copy problem made me very nervous: Can you tell us something
about the reason/remedy for it?

(in reply to thinicer)
 
 
Post #: 17
 
 RE: Script to rename lots of folders - 5/19/2006 1:24:29 AM   
  thinicer

 

Posts: 18
Score: 0
Joined: 5/17/2006
Status: offline
Move/Copy problem, you mean with DFS replicating all the data?

(in reply to ehvbs)
 
 
Post #: 18
 
 RE: Script to rename lots of folders - 5/19/2006 1:31:20 AM   
  ehvbs

 

Posts: 2204
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
yes, please.

(in reply to thinicer)
 
 
Post #: 19
 
 RE: Script to rename lots of folders - 5/19/2006 1:50:07 AM   
  thinicer

 

Posts: 18
Score: 0
Joined: 5/17/2006
Status: offline
It's kind of hard to explain since I don't know DFS that well, but I will give it my best shot.

When you set up DFS you have to specify a root of all the data you want to replicate. For example, suppose I want to replicate data all data in the D:\DFSdata\  on my server. D:\DFSdata is therefore the root.

Now, the script I was using before specified a path, so if I wanted to change all the folder names in D:\DFSdata\Work\Projects I would have to put that path in the script and I could theoretically run from the script from another folder or another drive if I wanted. Because of the way DFS is configured, and because that script I was using before uses a path, it goes up the tree. It goes to DFSdata, and then goes to Work, and then goes to Projects and changes the path of the folders from the ROOT itself.

So, it changes D:\DFSdata\Work\Projects\Old Folder Name to D:\DFSdata\Work\Projects\New Folder Name.

So when it did these changes, it just began replicating all the data on the server for some reason because the root was included in the path.

The new script that ebgreen assisted me with runs from within the folder it is located in, so the only path it concerns itself with where it is located onwards. It runs from the current folder, kind of like running an executable where it is located instead of just using a shortcut on your desktop.

That's the best way I can describe it.

(in reply to ehvbs)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Script to rename lots of folders Page: [1] 2   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts