Solved

VBS Script not completing loop

Posted on 2011-02-13
3
368 Views
Last Modified: 2012-05-11
Hello, the first script below works. It creates text files.

Const ForWriting = 2
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
'Repeats the same sequence for the numbers 75 through 407
For i = 75 to 407
	'Adds a leading zero if the counter is a 2-digit number
	If Len(i) = 2 Then
		intNum = "0" & i
	Else
		intNum = i
	End If
	
	'Defines the new file name
	strNewFile = "c1dsf_" & intNum & ".dsf"
	
	'Creates the new file and adds text, including the counter
	Set objNewFile = objFSO.CreateTextFile(strNewFile, ForWriting)
	objNewFile.WriteLine "0"
	objNewFile.WriteLine "4"
	objNewFile.WriteLine "hwyc1_" & intNum & "_fp"
	objNewFile.WriteLine "0.2000"
	objNewFile.WriteLine "rdc1_" & intNum & "_fp"
	objNewFile.WriteLine "0.2000"
	objNewFile.WriteLine "slopec1_" & intNum & "_fp"
	objNewFile.WriteLine "0.4000"
	objNewFile.WriteLine "urbanc1_" & intNum & "_fp"
	objNewFile.WriteLine "0.2000"
	objNewFile.Close
Next

Open in new window


I have modified slightly. The range of numbers is now different (starts with 0) and and also I no longer need the padding zero (single digit numbers can stay that way):

Const ForWriting = 2
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
'Repeats the same sequence for the numbers 75 through 407
For i = 0 to 114
	'Adds a leading zero if the counter is a 2-digit number
	If Len(i) = 5 Then
		intNum = "0" & i
	Else
		intNum = i
	End If
	
	'Defines the new file name
	strNewFile = "c1dsf_" & intNum & ".dsf"
	
	'Creates the new file and adds text, including the counter
	Set objNewFile = objFSO.CreateTextFile(strNewFile, ForWriting)
	objNewFile.WriteLine "0"
	objNewFile.WriteLine "4"
	objNewFile.WriteLine "hwy_" & intNum & "_c1p"
	objNewFile.WriteLine "0.2000"
	objNewFile.WriteLine "road_" & intNum & "_c1p"
	objNewFile.WriteLine "0.2000"
	objNewFile.WriteLine "slope_" & intNum & "c1p"
	objNewFile.WriteLine "0.4000"
	objNewFile.WriteLine "urban_" & intNum & "_c1p"
	objNewFile.WriteLine "0.2000"
	objNewFile.Close
Next

Open in new window


This only outputs one file with no number insertion.  I made the number for padding = 5 hoping it would effectively skip this unnecessary step.  I don't know how to set it up with out it.

Please advise,
Thanks,
JE
0
Comment
Question by:justearth
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 34884580
If you don't need the padding then get rid of lines #7 thru #12.

A better way to do the padding, though, would be to simply check if the value is less than 10:

      If i < 10 Then
            intNum = "0" & i
      Else
            intNum = i
      End If
0
 
LVL 14

Assisted Solution

by:Psy053
Psy053 earned 250 total points
ID: 34884622
Something else that looks out of place is the argument that you are using for the CreateTextFile method. Generally the ForWriting part of that line is used for iomode argument in the OpenTextFile method.

Perhaps you could try changing it to suit the method you're using.

Change:
Set objNewFile = objFSO.CreateTextFile(strNewFile, ForWriting)

To:
Set objNewFile = objFSO.CreateTextFile(strNewFile, True)


0
 

Author Closing Comment

by:justearth
ID: 34884637
Thanks.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now