Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


RE: 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 >> RE: Script to rename lots of folders
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: <<   < prev  1 [2]
Login
Message << Older Topic   Newer Topic >>
 RE: Script to rename lots of folders - 5/19/2006 2:00:20 AM   
  ehvbs

 

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

thank you very much for your explanation. I will remember it, when move/rename
at my customer's system will behave badly! That's (for me) a very good exchange
for 1 line of VBScript with some Mid() function calls!

Thank you very much

ehvbs

(in reply to thinicer)
 
 
Post #: 21
 
 RE: Script to rename lots of folders - 5/26/2006 1:31:08 AM   
  thinicer

 

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

The script works great, but only if I don't have a lot of folders. It messes up the naming convention for every folder when there are lots of them. I then have to manually go and change them.

When there are lots of folders needed to be changed, the script changes all the folders from a ##-####.## Description format to a ##-##...0000000## Description

I do not believe I am using proper error correction in it. If you or somebody else could help, I would be most grateful.

Again, here is the script:

Dim fso,f,fc
Set fso = CreateObject("Scripting.FileSystemObject")
Set oWSHl = WScript.CreateObject("WScript.Shell")
strPWD = oWSHl.CurrentDirectory
Set f = fso.GetFolder(strPWD)
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 )
'Error correction?
  f1.Name = strNewFolder
End Sub


< Message edited by thinicer -- 5/26/2006 1:32:50 AM >

(in reply to ehvbs)
 
 
Post #: 22
 
 RE: Script to rename lots of folders - 5/26/2006 3:23:34 AM   
  ehvbs

 

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

sorry to hear that. Could you post a list of folder names that causes the
script to misbehave?

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

 

Posts: 18
Score: 0
Joined: 5/17/2006
Status: offline
I don't think there are any folder names that cause the script to misbehave. If there are, I don't know what they are. I've looked at all the folders and they all appear to be named in the correct convention.

There could be one that is throwing the script off. Still, if there is one, I would like some error control so that it doesn't change the name of the folder and leaves it alone.

This is what happens when the script malfunctions:

03-0002.00 CC Rural Fringe LDRs becomes 03.....0000000000.00 CC Rural Fringe LDRs
 
03-0018.00 GGE, Unit 14, Tract 1 becomes 03.....0000000000.00 GGE, Unit 14, Tract 1
 
These folders are supposed to be renamed in the following naming convention:
 
030002.00.00 CC Rural Fringe LDRs
030018.00.00 GGE, Unit 14, Tract 1

Any ideas ehvbs? Thanks for your help regarding this.

(in reply to ehvbs)
 
 
Post #: 24
 
 RE: Script to rename lots of folders - 5/26/2006 6:18:46 AM   
  thinicer

 

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

I did a lot of testing. In a directory that contains folders that I need to rename, I copied batches of 10 into a folder, and ran the script on each batch. It worked each time.

I then attempted to run the script on more and more folders. I got up to 21 before it started going haywire on a certain group of folders.

On another group, it began to go haywire after 23.

It seems to me that the script malfunctions after it reaches a variable point in the amount of folders that need to be renamed. I don't know why.

Any help on this would be appreciated.

(in reply to ehvbs)
 
 
Post #: 25
 
 RE: Script to rename lots of folders - 5/26/2006 6:30:54 AM   
  ehvbs

 

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

From your first post to this topic, I assumed this specs for the
folder names to be changed:


      

From that I made up the code


      

The suspicious folder names from your last post

   03-0002.00 CC Rural Fringe LDRs
   03-0018.00 GGE, Unit 14, Tract 1

still fit this pattern - this can be proved by adding them to the aTests
array in the short test script of my first posting.

What happens if you apply the string operation to an already renamed folder?
The test script can easily be modified to answer that:


      

Now you see what caused your "03.....0000000000.00 CC Rural Fringe LDRs" like
folder names. If we can't guarantee that

  "The existing folders all have the same naming convention, that is....##-####.## Project Name"

then we'll have to make sure, that only folders of the specified naming
convention are renamed.

The information that went into the string operation can be used to define
a regular expression that will match only those strings that fit the specs.
I had hoped to avoid RegExps, because it will open another can of worms,
but if we want to check whether a given folder name fits the specs, we'll
have to use them. To start easy, let's rewrite my test script to use
RegExps:


      

The important part is the oRE.Pattern:

      

All this mess is explained at length in the VBScript Docs (Regexp object,
Introduction to Regular Expressions, Regular Expression Syntax). I hope my
dissection will help you to understand at least this pattern.

Now let's put the RegExp in (a slightly modified version of) your script:


      

If you execute this script, it should tell you, which folders will be renamed and which folders
do not fit the naming scheme.

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

 

Posts: 18
Score: 0
Joined: 5/17/2006
Status: offline
Thanks ehvbs, I'll give it a whirl and I will report back to you.

I wanted to also tell you that upon further examination, when the script fails it seems to run 4 times on all the folder names, adding more zeroes and periods each time.

Again, thanks for so much for taking the time to assist me. I'll post here as soon as I've given your new script some testing.

(in reply to ehvbs)
 
 
Post #: 27
 
 RE: Script to rename lots of folders - 5/26/2006 7:31:14 AM   
  thinicer

 

Posts: 18
Score: 0
Joined: 5/17/2006
Status: offline
THANK YOU!!!

It works like a charm! Folders not in the right convention will not be renamed, and those that are are properly renamed and the script doesn't add any extra zeros and periods to the folder name! OUTSTANDING!

Ehvbs, one more question.

Supposing the directory that needs to be renamed is currently in use. What kind of error control can I write that will skip it over and still execute its code on every other folder?

(in reply to ehvbs)
 
 
Post #: 28
 
 RE: Script to rename lots of folders - 5/26/2006 8:22:26 AM   
  ehvbs

 

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

you're welcome - even if you open yet another can of worms. Modify the sub like this:


      

This is dangerous code, because we hide/mask errors we don't know enough about.
What happens, if the "permission denied" error isn't caused by somebody using the
folder temporarily, but by some user/rights/security setting mess up? Such folders
would never be renamed - and that could lead to serious problems.

So please: read about VBScript Error Handling and apply your knowledge to the
context your script has to work in.

(in reply to thinicer)
 
 
Post #: 29
 
 RE: Script to rename lots of folders - 5/30/2006 11:18:03 PM   
  thinicer

 

Posts: 18
Score: 0
Joined: 5/17/2006
Status: offline
Thanks, ehvbs. Everything worked perfectly. You saved me many long hours and many long headaches. I appreciate all of your help tremendously.

(in reply to ehvbs)
 
 
Post #: 30
 
 
Page:  <<   < prev  1 [2]
 
  

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 >> RE: Script to rename lots of folders Page: <<   < prev  1 [2]
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