Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VBS Script not completing loop

Posted on 2011-02-13
3
Medium Priority
?
380 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 86

Accepted Solution

by:
Mike Tomlinson earned 1000 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 1000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

773 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