Solved

VBS Script not completing loop

Posted on 2011-02-13
3
375 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 86

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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will learn how to count occurrences of each item in an array.
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.

717 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