|Looking for assistance again! Thought I would put together a small script to let me allocate random names to files and at the same time add a Prefix or a Suffix as required. However, the problem I ran into was when I wanted the suffix to include leading zeros namely, if the file name was to end ..... 1.txt I wanted to have .....0001.txt. Providing 0001 in the suffix data input box did not work despite showing as .....0001.txt on screen. The resultant file came out as 1.txt. Using a function seemed to work on a single file until I remembered that a function doesn't work in a loop. Can any of you guys out there assist and point me in the right direction?|
I've added my script below. It does what I want apart from adding the leading zeros but it's a bit rickety. The files I was testing it on are named:
Library_123.txt, Library_456.txt, Library_789.txt
Dim fso, f, intMax, iLoop, k, intValue, strChar, strName, intNum
Dim File, strPrefix, strSuffix, Tally, x, Pad, n
' Characters available for random choice
Const Chars = "abcdefghijklmnopqrstuvwxyz123456789"
' Specify length of names.
intMax = 40
'Input File Prefix
strPrefix = InputBox(chr(13) & chr(10) & chr(13) & chr(10) & " Enter Prefix", "Data Input")
wscript.echo " Prefix Entered Is: " & strPrefix
'Input File Suffix
strSuffix = InputBox(chr(13) & chr(10) & chr(13) & chr(10) & " Enter Suffix", "Data Input")
wscript.echo " Suffix Entered Is: " & strSuffix
Tally = "-1"
Set f = fso.GetFolder("F:\Test_8\Test\")
For Each file In f.Files
if instr(file.name, "Library")>0 then
tally = Tally+1
strName = ""
For k = 1 To intMax
' Retrieve random character between 0 and 34
intValue = Fix(35 * Rnd())
' Convert to allowed character
strChar = Mid(Chars, intValue + 1, 1)
' Build the name.
strName = strName & strChar
x = "0"
'Pad for leading zeros
pad = 4-len(strSuffix)
'wscript.echo pad ' 3
'Build leading zeros
for n= 1 to pad
strSuffix = replace(strSuffix,strSuffix,x & strSuffix)
'wscript.echo "Screen Suffix" & strSuffix
fso.MoveFile file.Name, Replace(file.Name,file.name,strPrefix & "_" & strName & "_" & strSuffix+Tally & ".txt")
'wscript.echo "Suffix" & len(strSuffix)
Set f = Nothing
Set fso = Nothing
Wscript.Echo " File Names Allocated"