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
>