Right, go easy on me as this is my first VBScript and I done it all myself
(with a few looks at dozens of web sites to get the right syntax!), I'm
still learning! I was hoping some of you guys might be able to tell me where
I can improve.
The background behind it is this: at work, we use a document management
system called Hummingbird. Regularly I have to export up to hundreds of
documents from Hummingbird and put them on a CD and transfer them to our
Restricted PC (I work for the MoD here in the UK). When I transfer them
over they have to be named properly, so the person receiving them knows what
they are at a glance.
The big problem is when Hummingbird exports them from it's server, it names
them it's own way, and this can't be changed. The way it names them is:
<library name>-#<Hummingbird's internal doc no>-v1-<doc title, with
underscores for spaces>.<extension, in upper case>.
An example is (the 19446 bit could be 1 or more digits):
BERTHING_U-#19446-v1-Jetty_Support_Building_Supply_Single_Line_Diagram.PDF
As you can see if we have loads like this in a folder, it's not very
readable and user friendly.
We have to rename them to (the bits in <> are field names within
Hummingbird):
<System No>-<Seq No>-<Revision> - <title>.<extension, in lower case>
So the above example would become:
610-023-3A1 - Jetty Support Building Supply Single Line Diagram.pdf
This, at a glance, tells the receiver the system number, seq number,
revision of the doc and the title. Much more readable.
Before I started this job the folk in the office renamed all these manually
by selecting a file in Windows, pressing F2, etc, etc. Obviously for
hundreds of files this was taking ages.
So I came up with idea of writing a script that would automatically do this
for all files in the folder where the script resided. I have no way of
interrogating Hummingbird for the System, Seq and Rev numbers, so the code
has an input box for the user to input this.
Below is my script - it works well, but as I'm a beginner I'm sure some folk
on here could help me optimise/improve it. The comments were added purely
for the purpose of this post, to help you guys see what I'm trying to do at
each stage.
Many thanks in advance, I'm keen to improve my knowledge.
Code
---------------------------------------------------------------------------------------------------------------------------------------------------
Option Explicit
'Declare all the variables
Dim fso, File, re, re2, re3, re4, strNewName, strA, strB, intA, Message,
Title, Text1, result, WSHShell, intHash, intv1, strDocNo, _
intDot, fullpath, path
'Get the path of the current folder for use later, so the script can be run
from any folder
fullpath = WScript.ScriptFullName
path = Left(fullpath, InstrRev(fullpath, "\"))
'Create the FSO and set some Regular Expressions for parsing the file name
Set fso = CreateObject("Scripting.FileSystemObject")
Set re = New RegExp
Set re2 = New RegExp
Set re3 = New RegExp
Set re4 = New RegExp
'Find the bits of the file name I want to change using Regular Expressions
re.Pattern = "BERTHING_U-#[0-9]+-v1-" 'the [0-9]+ bit allows for files that
have 1 or more digits
re.IgnoreCase = False
re2.Pattern = "_" 'find all the underscores for changing to spaces
re2.Global = True
re3.Pattern = " and " 'find any occurrences of "and" for changing to "&"
re4.Global = True
re4.Pattern = " +" 'rarely used, some files have a superflous + sign in
them, change to space later
re4.Global = True
intA = 0 'this will be my incremental integer for use later to append to the
new name when 2 files share the same name after renaming
For Each File In fso.GetFolder(path).Files 'loop through all the files in
the current folder
If File.Name <> "Renamer.vbs" Then 'If statement to stop the script renaming
the script file itself
'The code below is used later to parse the Hummingbird document number from
the original filename
intHash = InStr(1,File.Name,"#")
intv1 = InStr(1,File.Name,"-v1")
strDocNo= Mid(File.Name,intHash+1,intV1-(intHash+1))
strNewName = File.Name
Title = "File Renamer"
Text1 = "No input - renaming without System, Seq & Rev"
'Block of If statements to use the Regular Expressions to build a new file
name and store it in strNewName
If re.Test(strNewName) then
strNewName = re.replace(strNewName, "")
Else
End If
If re2.Test(strNewName) then
strNewName = re2.replace(strNewName, " ")
Else
End If
If re3.Test(strNewName) then
strNewName = re3.replace(strNewName, " & ")
Else
End If
If re4.Test(strNewName) then
strNewName = re4.replace(strNewName, " ")
Else
End If
'used to separate the file name and the extension
intDot = InStr(1,strNewName,".")
strA=Left(strNewName,Len(strNewName)-(Len(strNewName)-(intDot-1)))
strB=Right(strNewName,Len(strNewName)-(intDot-1))
'originally here in case 2 files had the same name after renaming, if so
it would append a number to one of them.
'May be null & void since I've implmented code below to append some user
input to the start of file name, which
'should keep every one unique
If fso.FileExists(strNewName) then
intA = intA+1
strNewName=strA & intA & strB
Else
End If
'Get the user to type in the System, Seq and Rev number for the new
filename - has to be done manually, have no way of interrogating
'Hummingbird for this info. The Hummingbird doc number is shown so the
user can go back to Hummingbird and search for this doc number
'to get the System, Seq and Rev
message = "Please enter System-Seq-Rev for: " & VbCRLf & VBCRLf & strA &
LCase(strB) & VbCRLf & VbCRLf & "Hummingbird Doc #: " & strDocNo
result = InputBox(Message,Title,"", 100, 100)
File.Name = strA & LCase(strB) 'actually rename the file, and also convert
the file extension to lower case
If result = "" Then
WScript.Echo Text1
Else
File.Name = result & " - " & File.Name 'append the user's input to the
beginning of the new file name
End If
Else
End If
Next 'move onto the next file
'new loop to move the files to a folder called "Renamed" in the current
folder. They're moved in case someone tries to run the script
'while there are previously renamed files in the starting folder - not sure
what impact this would have, so I though it best to move them.
'I'm also unsure as to what happens if there is no folder called "Renamed"
in the current folder - I guess an error of some sort.
'I'd like to build code in here so that a "Renamed" folder gets created if
it doesn't exist
For Each File In fso.GetFolder(path).Files
If File.Name <> "Renamer.vbs" Then 'If statement to avoid moving the script
file itself
fso.MoveFile path & File.Name, path & "Renamed\" & File.Name
Else
End If
Next 'move the next file
---------------------------------------------------------------------------------------------------------------------------------------------------