JuniperGreen
Member
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
Thank you
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
Thank you
Code:
Option Explicit
'Define vars
Dim fso, f, intMax, iLoop, k, intValue, strChar, strName, intNum
Dim File, strPrefix, strSuffix, Tally, x, Pad, n
Set fso=CreateObject("Scripting.FileSystemObject")
' 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")
'Check point
wscript.echo " Prefix Entered Is: " & strPrefix
'Input File Suffix
strSuffix = InputBox(chr(13) & chr(10) & chr(13) & chr(10) & " Enter Suffix", "Data Input")
'Checkpoint
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
Randomize()
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
Next
Wscript.Echo strName
'Checkpoint
'wscript.echo len(strName)
'Checkpoint
'wscript.echo len(strSuffix)
x = "0"
'Pad for leading zeros
pad = 4-len(strSuffix)
'Checkpoint
'wscript.echo pad ' 3
'Build leading zeros
for n= 1 to pad
strSuffix = replace(strSuffix,strSuffix,x & strSuffix)
next
'Checkpoint
'wscript.echo "Screen Suffix" & strSuffix
fso.MoveFile file.Name, Replace(file.Name,file.name,strPrefix & "_" & strName & "_" & strSuffix+Tally & ".txt")
end if
'Checkpoint
'wscript.echo "Suffix" & len(strSuffix)
Next
Set f = Nothing
Set fso = Nothing
Wscript.Echo " File Names Allocated"
Wscript.Quit