Hello,
I read somewhere that there are no i/o functions for vbscript, but I'd
like to know if there is a way to execute a specific line from a text
file. I have a text file with program paths on each line (no commas or
anything breaking each line, but only one program path per line), and I
want to execute a specific program, based on the line number. This line
number would be randomly generated to a variable PNUMBER (this part
I've already done).

Thanks,
Isuru

Re: Execute line from text file by Richard

Richard
Sun Sep 17 10:53:29 CDT 2006

Isuru wrote:

> I read somewhere that there are no i/o functions for vbscript, but I'd
> like to know if there is a way to execute a specific line from a text
> file. I have a text file with program paths on each line (no commas or
> anything breaking each line, but only one program path per line), and I
> want to execute a specific program, based on the line number. This line
> number would be randomly generated to a variable PNUMBER (this part
> I've already done).

Hi,

You can use FileSystemObject to read a text file one line at a time. You can
use the Run method of the wshShell object to execute a command. For example:
=====
Option Explicit

Dim objFSO, strFileName, objFile, strLine, intCount, objShell, intLineNo

' Specify text file with commands.
strFileName = "c:\scripts\command.txt"

' Specify line number in file to execute.
intLineNo = 5

' Open file for read access.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, 1)

Set objShell = CreateObject("Wscript.Shell")

' Read each line of the file.
intCount = 0
Do Until objFile.AtEndOfStream
intCount = intCount + 1
strLine = Trim(objFile.ReadLine)
' Execute only the line specified.
If (intCount = intLineNo) Then
objShell.Run "%comspec% /c " & strLine
Exit Do
End If
Loop
objFile.Close
=========
You won't see the output, but you can redirect to a text file. For example,
I tested with this command:

dir c:\scripts\*.vbs > temp.txt

Note, you could also put the commands in an array and select one by index
number at random.

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net



Re: Execute line from text file by ixxy

ixxy
Sun Sep 17 11:43:39 CDT 2006

Great, it works! But it doesn't seem to treat pathnames with spaces in
them the same way..
I feel a little dim asking this, im novice you see, but what change
should I do to treat all lines with quotation marks? Also, it'd be
perfect if the script would exit once the pathname is executed, if it
is at all possible.

Thanks alot!

Richard Mueller wrote:
> Isuru wrote:
>
> > I read somewhere that there are no i/o functions for vbscript, but I'd
> > like to know if there is a way to execute a specific line from a text
> > file. I have a text file with program paths on each line (no commas or
> > anything breaking each line, but only one program path per line), and I
> > want to execute a specific program, based on the line number. This line
> > number would be randomly generated to a variable PNUMBER (this part
> > I've already done).
>
> Hi,
>
> You can use FileSystemObject to read a text file one line at a time. You can
> use the Run method of the wshShell object to execute a command. For example:
> =====
> Option Explicit
>
> Dim objFSO, strFileName, objFile, strLine, intCount, objShell, intLineNo
>
> ' Specify text file with commands.
> strFileName = "c:\scripts\command.txt"
>
> ' Specify line number in file to execute.
> intLineNo = 5
>
> ' Open file for read access.
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFile = objFSO.OpenTextFile(strFileName, 1)
>
> Set objShell = CreateObject("Wscript.Shell")
>
> ' Read each line of the file.
> intCount = 0
> Do Until objFile.AtEndOfStream
> intCount = intCount + 1
> strLine = Trim(objFile.ReadLine)
> ' Execute only the line specified.
> If (intCount = intLineNo) Then
> objShell.Run "%comspec% /c " & strLine
> Exit Do
> End If
> Loop
> objFile.Close
> =========
> You won't see the output, but you can redirect to a text file. For example,
> I tested with this command:
>
> dir c:\scripts\*.vbs > temp.txt
>
> Note, you could also put the commands in an array and select one by index
> number at random.
>
> --
> Richard
> Microsoft MVP Scripting and ADSI
> Hilltop Lab - http://www.rlmueller.net


Re: Execute line from text file by ixxy

ixxy
Sun Sep 17 13:42:37 CDT 2006

Another small note: I had a couple of files with some extra characters
such as (, ), ! and they didn't execute properly either. Sorry, forgot
to mentioning it eariler.

ixxy wrote:
> Great, it works! But it doesn't seem to treat pathnames with spaces in
> them the same way..
> I feel a little dim asking this, im novice you see, but what change
> should I do to treat all lines with quotation marks? Also, it'd be
> perfect if the script would exit once the pathname is executed, if it
> is at all possible.
>
> Thanks alot!
>
> Richard Mueller wrote:
> > Isuru wrote:
> >
> > > I read somewhere that there are no i/o functions for vbscript, but I'd
> > > like to know if there is a way to execute a specific line from a text
> > > file. I have a text file with program paths on each line (no commas or
> > > anything breaking each line, but only one program path per line), and I
> > > want to execute a specific program, based on the line number. This line
> > > number would be randomly generated to a variable PNUMBER (this part
> > > I've already done).
> >
> > Hi,
> >
> > You can use FileSystemObject to read a text file one line at a time. You can
> > use the Run method of the wshShell object to execute a command. For example:
> > =====
> > Option Explicit
> >
> > Dim objFSO, strFileName, objFile, strLine, intCount, objShell, intLineNo
> >
> > ' Specify text file with commands.
> > strFileName = "c:\scripts\command.txt"
> >
> > ' Specify line number in file to execute.
> > intLineNo = 5
> >
> > ' Open file for read access.
> > Set objFSO = CreateObject("Scripting.FileSystemObject")
> > Set objFile = objFSO.OpenTextFile(strFileName, 1)
> >
> > Set objShell = CreateObject("Wscript.Shell")
> >
> > ' Read each line of the file.
> > intCount = 0
> > Do Until objFile.AtEndOfStream
> > intCount = intCount + 1
> > strLine = Trim(objFile.ReadLine)
> > ' Execute only the line specified.
> > If (intCount = intLineNo) Then
> > objShell.Run "%comspec% /c " & strLine
> > Exit Do
> > End If
> > Loop
> > objFile.Close
> > =========
> > You won't see the output, but you can redirect to a text file. For example,
> > I tested with this command:
> >
> > dir c:\scripts\*.vbs > temp.txt
> >
> > Note, you could also put the commands in an array and select one by index
> > number at random.
> >
> > --
> > Richard
> > Microsoft MVP Scripting and ADSI
> > Hilltop Lab - http://www.rlmueller.net


Re: Execute line from text file by Richard

Richard
Sun Sep 17 13:42:14 CDT 2006

Hi,

If the command is read from a text file, it would be very difficult to parse
it to determine where quotes should go. However, the command in the text
file can have the needed quotes. For example, I tested the command:

dir "c:\Program Files\*.*" > temp.txt

The program read this from the text file and ran the command successfully.
Once the line is read and the command is run, the statement "Exit Do" causes
the program to stop reading the file. The only statement that runs after
this is objFile.Close, which should be executed to clean up.

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net

"ixxy" <isurum@gmail.com> wrote in message
news:1158511419.551212.325080@m73g2000cwd.googlegroups.com...
> Great, it works! But it doesn't seem to treat pathnames with spaces in
> them the same way..
> I feel a little dim asking this, im novice you see, but what change
> should I do to treat all lines with quotation marks? Also, it'd be
> perfect if the script would exit once the pathname is executed, if it
> is at all possible.
>
> Thanks alot!
>
> Richard Mueller wrote:
>> Isuru wrote:
>>
>> > I read somewhere that there are no i/o functions for vbscript, but I'd
>> > like to know if there is a way to execute a specific line from a text
>> > file. I have a text file with program paths on each line (no commas or
>> > anything breaking each line, but only one program path per line), and I
>> > want to execute a specific program, based on the line number. This line
>> > number would be randomly generated to a variable PNUMBER (this part
>> > I've already done).
>>
>> Hi,
>>
>> You can use FileSystemObject to read a text file one line at a time. You
>> can
>> use the Run method of the wshShell object to execute a command. For
>> example:
>> =====
>> Option Explicit
>>
>> Dim objFSO, strFileName, objFile, strLine, intCount, objShell, intLineNo
>>
>> ' Specify text file with commands.
>> strFileName = "c:\scripts\command.txt"
>>
>> ' Specify line number in file to execute.
>> intLineNo = 5
>>
>> ' Open file for read access.
>> Set objFSO = CreateObject("Scripting.FileSystemObject")
>> Set objFile = objFSO.OpenTextFile(strFileName, 1)
>>
>> Set objShell = CreateObject("Wscript.Shell")
>>
>> ' Read each line of the file.
>> intCount = 0
>> Do Until objFile.AtEndOfStream
>> intCount = intCount + 1
>> strLine = Trim(objFile.ReadLine)
>> ' Execute only the line specified.
>> If (intCount = intLineNo) Then
>> objShell.Run "%comspec% /c " & strLine
>> Exit Do
>> End If
>> Loop
>> objFile.Close
>> =========
>> You won't see the output, but you can redirect to a text file. For
>> example,
>> I tested with this command:
>>
>> dir c:\scripts\*.vbs > temp.txt
>>
>> Note, you could also put the commands in an array and select one by index
>> number at random.
>>
>> --
>> Richard
>> Microsoft MVP Scripting and ADSI
>> Hilltop Lab - http://www.rlmueller.net
>



Re: Execute line from text file by Alexander

Alexander
Mon Sep 18 05:59:34 CDT 2006

ixxy schrieb:

> I read somewhere that there are no i/o functions for vbscript, but I'd
> like to know if there is a way to execute a specific line from a text
> file. I have a text file with program paths on each line (no commas or
> anything breaking each line, but only one program path per line), and I
> want to execute a specific program, based on the line number. This line
> number would be randomly generated to a variable PNUMBER (this part
> I've already done).

A neat one-liner using command-shell scripting:

for /F "delims=" %%T in (textfile.dat) do "%%~T"


MfG,
Alex