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