Reputation: 989
I'm trying to create a script which will insert 2 characters into the names of a group of subfolders in a folder. Here are the details of these folders:
Each folder begins with a 14-digit number and an underscore (ex. 60818400200000_)
The folder names are of various lengths
Some folder names have additional underscores in their names
The script needs to insert a dash and an underscore (-_) after the first underscore, or replace the first underscore with an underscore-dash-underscore (_-_).
Each folder afterwards should look like [14-digit number]_-_[rest of file name].
Here is what I've come up with so far:
Dim objFso, folder, fc, rname as string
Set objFso = CreateObject("Scripting.FileSystemObject")
Set folder = objFso.GetFolder("F:\Eagle_Ford_20130220\TO_LOAD")
Set fc = folder.SubFolders
Set rname = Mid(folder.Name,15,1)
For each folder in fc
If rname = "_" Then
rname = rname.replace("_","_-_")
End If
Next
Wscript.Echo "Finished"
However, whenever I run this script I end up with some kind of error message. If I don't define the rname as a string, I end up with an "Object Required" error message. If I define the rname as a string (as is shown in the script above), I end up with an "Expected end of statement" error.
I've looked everywhere on the web to figure out what I'm doing wrong. Would someone here be willing to point me in the right direction? I would be ever so grateful! :)
EDIT:
After getting some direction from Rory, I modified my script slightly to this:
Dim objFso, folder, sfolder, rname
Set objFso = CreateObject("Scripting.FileSystemObject")
Set folder = objFso.GetFolder("F:\Eagle_Ford_20130220\TO_LOAD")
Set sfolder = folder.SubFolders
For each n in sfolder
rname = Mid(folder.Name,15,1)
If rname = "_" Then
folder.name = rname.replace("_","_-_")
End If
Next
Wscript.Echo "Finished"
The main changes were to rename a couple of my variables and remove the Set and string definition for the rename variable. I also moved my rname variable within the For Each statement, which eliminated the Object error I kept getting.
The script runs, but no changes are made to the subfolders in the folder listed in the script. I've tested the For Each statement using an Echo command to make sure it was picking up each of the folders (it is). The only thing I can conclude is that there is something wrong with my If statement.
I've ran my script through a couple of debuggers, but get no error messages. I would really appreciate any additional assistance, especially since coding is not my strong suit (obviously).
Upvotes: 1
Views: 1308
Reputation: 989
I finally figured out what I was doing wrong. Here is the script I came up with for anyone else who has this issue:
'Script to add two (or more) characters to a folder name
Dim objFso, folder, sfolder
Set objFso = CreateObject("Scripting.FileSystemObject")
Set folder = objFso.GetFolder("F:\Eagle_Ford_20130220\Loaded") 'Folder with subfolders to be renamed
Set sfolder = folder.SubFolders
For each folder in sfolder
'Position to start search of folder name
rname = Mid(folder.Name,15,1)
'Search for subfolders with a particular string
If rname = "_" Then
'Add/Replace characters to subfolder name
rname = Replace(mid(folder.name,15,1),"_","_-_")
'Write out new subfolder name
folder.name = left(folder.name,14) & rname & mid(folder.name,16,len(folder.name))
End If
Next
Wscript.Echo "Finished"
Thank you to those who helped me figure this out.
Upvotes: 1
Reputation: 35338
You have a few problems with your script. First of all, VBScript doesn't support strongly-typed variables, thus you can't use the As
keyword like you can with other flavors of visual basic. So get rid of that in your variable declarations:
Dim objFso, folder, fc, rname '<-- no "As String"
Next, you are receiving the "Object Required" error on line 7 because you can only use the Set
keyword with object references in VBScript. Mid
function returns a string, which is not an object reference, hence the error. To fix it, just get rid of Set
:
rname = Mid(folder.Name,15,1)
Also, you're re-using your folder
variable. You assign it via Set folder = objFso.GetFolder("F:\Eagle_Ford_20130220\TO_LOAD")
and then you're assigning it again in the for loop via For each folder in fc
. This shouldn't affect your script as written, but it's not a good idea -- it's a code smell. It could result in bugs later if you change your script.
Upvotes: 1