Solved

VBS Script not completing loop

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

749 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