Solved

VBS Script not completing loop

Posted on 2011-02-13
3
366 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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.
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

757 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

22 Experts available now in Live!

Get 1:1 Help Now