Solved Problem With Leading Zeros In File Names

March 29, 2013 at 11:40:28
Specs: Windows Vista, 1.6 gb 4gb
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

[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

[/code]


See More: Problem With Leading Zeros In File Names

Report •


#1
March 29, 2013 at 19:28:06
✔ Best Answer
This line, I am assuming you want to arithmetically add tally to the suffix?:

fso.MoveFile file.Name, Replace(file.Name,file.name,strPrefix & "_" & strName & "_" & strSuffix+Tally & ".txt")

In which case, you must do the math before any string manipulation, because the math will always strip leading zeros.
I was gonna try to fix it, but I'll wait to see if you wanted concat or arithmetic in the line-in-question. (If concat, use "&" instead of "+".)


Report •

#2
March 30, 2013 at 03:43:02
Hi Nbrane, I keep forgetting that the + and the & characters are not always interchangeable! However, I have now been able to modify my script (see below) and got it to work.

Many thanks again for your support which is much appreciated

[Code]

Option Explicit

'Define vars
Dim fso, f, intMax, iLoop, k, intValue, strChar, strName, intNum
Dim File, strPrefix, strSuffix, Tally, strZeros

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


strZeros = right("00000" & strSuffix+Tally, 5)

'Checkpoint
'wscript.echo "Screen Suffix" & strZeros


fso.MoveFile file.Name, Replace(file.Name,file.name,strPrefix & "_" & strName & "_" & strZeros & ".txt")
end if


Next

Set f = Nothing
Set fso = Nothing

Wscript.Echo " File Names Allocated"

Wscript.Quit

[/Code]


Report •

Related Solutions


Ask Question