Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Refresh on Reading more than one variable from a Text file

Reply
 
Old 09-09-2008   #1 (permalink)
OldDog


 
 

Refresh on Reading more than one variable from a Text file

Hi,

I know this is an old one, but I can't recall how to do it. I want to
read a text file and pick up two variables for my script. The first
item (0) is a file name and the second (1) is a directory.

MPFT.txt looks like this:

input1.txt "\customer1\Sales"
input2.txt "\customer2\Sales"


script looks like this;

Const ForWriting = 2
Const ForReading = 1
'On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Scripts\MPFT.txt",
ForReading)
Do Until objTextFile.AtEndOfStream
strFile = Trim(objTextFile.Readline)
If (strFile <> "") Then
'Check if file exists
ftp_filename = strFile.Item(0)

WScript.Echo ftp_filename

if objFSO.FileExists(strFile.Item(0)) then
wscript.Echo strFile.Item(0) & " exists"

ftp_local_pending = "\\Secretary\Aspen\Data\Reports\" & Year(date) &
strFile.item(1)
ftp_local_archive = "\\Secretary\Aspen\Data\Archives\" & Year(date)
& strFile.item(1)
WScript.Echo ftp_local_pending
WScript.Echo ftp_local_archive

My System SpecsSystem Spec
Old 09-09-2008   #2 (permalink)
Tom Lavedas


 
 

Re: Refresh on Reading more than one variable from a Text file

On Sep 9, 6:37*pm, OldDog <michael.r.felk...@xxxxxx> wrote:
Quote:

> Hi,
>
> I know this is an old one, but I can't recall how to do it. I want to
> read a text file and pick up two variables for my script. The first
> item (0) is a file name and the second (1) is a directory.
>
> MPFT.txt looks like this:
>
> input1.txt * * *"\customer1\Sales"
> input2.txt * * *"\customer2\Sales"
>
> script looks like this;
>
> Const ForWriting = 2
> Const ForReading = 1
> 'On Error Resume Next
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objTextFile = objFSO.OpenTextFile("C:\Scripts\MPFT.txt",
> ForReading)
> Do Until objTextFile.AtEndOfStream
> * * strFile = Trim(objTextFile.Readline)
> * * * * If (strFile <> "") Then
> * * * * * * * * 'Check if file exists
> * * * * * * * * ftp_filename = strFile.Item(0)
>
> * * * * * * * * WScript.Echo ftp_filename
>
> * * * * * * * * if objFSO.FileExists(strFile.Item(0)) then
> * * * * * * * * wscript.Echo strFile.Item(0) *& " exists"
>
> * * * * * * * * ftp_local_pending * * * = "\\Secretary\Aspen\Data\Reports\" & Year(date) &
> strFile.item(1)
> * * * * * * * * ftp_local_archive * * * = "\\Secretary\Aspen\Data\Archives\" & Year(date)
> & strFile.item(1)
> * * * * * * * * WScript.Echo ftp_local_pending
> * * * * * * * * WScript.Echo ftp_local_archive
I think you want the Split() function. If the two items are separated
on a line by as space, then use something like this ...

strFile = Trim(objTextFile.Readline)
If (strFile <> "") Then
aItems = Split(strFile, " ")
'Check if file exists
ftp_filename = aItems(0)
' Note the change of syntax. The file input is a string, not an
object.

Tom Lavedas
==========
My System SpecsSystem Spec
Old 09-10-2008   #3 (permalink)
Pegasus \(MVP\)


 
 

Re: Refresh on Reading more than one variable from a Text file


"OldDog" <mikef2691@xxxxxx> wrote in message
news:05faccf6-c79f-4b38-8ba5-8e2684bb45f4@xxxxxx
On Sep 9, 9:01 pm, Tom Lavedas <tglba...@xxxxxx> wrote:
Quote:

> On Sep 9, 6:37 pm, OldDog <michael.r.felk...@xxxxxx> wrote:
>
>
>
Quote:

> > Hi,
>
Quote:

> > I know this is an old one, but I can't recall how to do it. I want to
> > read a text file and pick up two variables for my script. The first
> > item (0) is a file name and the second (1) is a directory.
>
Quote:

> > MPFT.txt looks like this:
>
Quote:

> > input1.txt "\customer1\Sales"
> > input2.txt "\customer2\Sales"
>
Quote:

> > script looks like this;
>
Quote:

> > Const ForWriting = 2
> > Const ForReading = 1
> > 'On Error Resume Next
> > Set objFSO = CreateObject("Scripting.FileSystemObject")
> > Set objTextFile = objFSO.OpenTextFile("C:\Scripts\MPFT.txt",
> > ForReading)
> > Do Until objTextFile.AtEndOfStream
> > strFile = Trim(objTextFile.Readline)
> > If (strFile <> "") Then
> > 'Check if file exists
> > ftp_filename = strFile.Item(0)
>
Quote:

> > WScript.Echo ftp_filename
>
Quote:

> > if objFSO.FileExists(strFile.Item(0)) then
> > wscript.Echo strFile.Item(0) & " exists"
>
Quote:

> > ftp_local_pending = "\\Secretary\Aspen\Data\Reports\" & Year(date) &
> > strFile.item(1)
> > ftp_local_archive = "\\Secretary\Aspen\Data\Archives\" & Year(date)
> > & strFile.item(1)
> > WScript.Echo ftp_local_pending
> > WScript.Echo ftp_local_archive
>
> I think you want the Split() function. If the two items are separated
> on a line by as space, then use something like this ...
>
> strFile = Trim(objTextFile.Readline)
> If (strFile <> "") Then
> aItems = Split(strFile, " ")
> 'Check if file exists
> ftp_filename = aItems(0)
> ' Note the change of syntax. The file input is a string, not an
> object.
>
> Tom Lavedas
> ==========
Thanks, I give it a try. How would I split on a Tab?

OldDog

===========

In the line
aItems = Split(strFile, " ")
you replace the the bit after the comma with the tab character chr(9).


My System SpecsSystem Spec
Old 09-10-2008   #4 (permalink)
OldDog


 
 

Re: Refresh on Reading more than one variable from a Text file

On Sep 10, 12:38*am, "Pegasus \(MVP\)" <I....@xxxxxx> wrote:
Quote:

> "OldDog" <mikef2...@xxxxxx> wrote in message
>
> news:05faccf6-c79f-4b38-8ba5-8e2684bb45f4@xxxxxx
> On Sep 9, 9:01 pm, Tom Lavedas <tglba...@xxxxxx> wrote:
>
>
>
>
>
Quote:

> > On Sep 9, 6:37 pm, OldDog <michael.r.felk...@xxxxxx> wrote:
>
Quote:
Quote:

> > > Hi,
>
Quote:
Quote:

> > > I know this is an old one, but I can't recall how to do it. I want to
> > > read a text file and pick up two variables for my script. The first
> > > item (0) is a file name and the second (1) is a directory.
>
Quote:
Quote:

> > > MPFT.txt looks like this:
>
Quote:
Quote:

> > > input1.txt "\customer1\Sales"
> > > input2.txt "\customer2\Sales"
>
Quote:
Quote:

> > > script looks like this;
>
Quote:
Quote:

> > > Const ForWriting = 2
> > > Const ForReading = 1
> > > 'On Error Resume Next
> > > Set objFSO = CreateObject("Scripting.FileSystemObject")
> > > Set objTextFile = objFSO.OpenTextFile("C:\Scripts\MPFT.txt",
> > > ForReading)
> > > Do Until objTextFile.AtEndOfStream
> > > strFile = Trim(objTextFile.Readline)
> > > If (strFile <> "") Then
> > > 'Check if file exists
> > > ftp_filename = strFile.Item(0)
>
Quote:
Quote:

> > > WScript.Echo ftp_filename
>
Quote:
Quote:

> > > if objFSO.FileExists(strFile.Item(0)) then
> > > wscript.Echo strFile.Item(0) & " exists"
>
Quote:
Quote:

> > > ftp_local_pending = "\\Secretary\Aspen\Data\Reports\" & Year(date) &
> > > strFile.item(1)
> > > ftp_local_archive = "\\Secretary\Aspen\Data\Archives\" & Year(date)
> > > & strFile.item(1)
> > > WScript.Echo ftp_local_pending
> > > WScript.Echo ftp_local_archive
>
Quote:

> > I think you want the Split() function. If the two items are separated
> > on a line by as space, then use something like this ...
>
Quote:

> > strFile = Trim(objTextFile.Readline)
> > If (strFile <> "") Then
> > aItems = Split(strFile, " ")
> > 'Check if file exists
> > ftp_filename = aItems(0)
> > ' Note the change of syntax. The file input is a string, not an
> > object.
>
Quote:

> > Tom Lavedas
> > ==========
>
> Thanks, I give it a try. How would I split on a Tab?
>
> OldDog
>
> ===========
>
> In the line
> aItems = Split(strFile, " ")
> you replace the the bit after the comma with the tab character chr(9).- Hide quoted text -
>
> - Show quoted text -
Kool, thanks
My System SpecsSystem Spec
Old 09-11-2008   #5 (permalink)
Al Dunbar


 
 

Re: Refresh on Reading more than one variable from a Text file


"Pegasus (MVP)" <I.can@xxxxxx> wrote in message
news:umZV3dwEJHA.4904@xxxxxx
Quote:

>
> "OldDog" <mikef2691@xxxxxx> wrote in message
> news:05faccf6-c79f-4b38-8ba5-8e2684bb45f4@xxxxxx
> On Sep 9, 9:01 pm, Tom Lavedas <tglba...@xxxxxx> wrote:
Quote:

>> On Sep 9, 6:37 pm, OldDog <michael.r.felk...@xxxxxx> wrote:
>>
>>
>>
Quote:

>> > Hi,
>>
Quote:

>> > I know this is an old one, but I can't recall how to do it. I want to
>> > read a text file and pick up two variables for my script. The first
>> > item (0) is a file name and the second (1) is a directory.
>>
Quote:

>> > MPFT.txt looks like this:
>>
Quote:

>> > input1.txt "\customer1\Sales"
>> > input2.txt "\customer2\Sales"
>>
Quote:

>> > script looks like this;
>>
Quote:

>> > Const ForWriting = 2
>> > Const ForReading = 1
>> > 'On Error Resume Next
>> > Set objFSO = CreateObject("Scripting.FileSystemObject")
>> > Set objTextFile = objFSO.OpenTextFile("C:\Scripts\MPFT.txt",
>> > ForReading)
>> > Do Until objTextFile.AtEndOfStream
>> > strFile = Trim(objTextFile.Readline)
>> > If (strFile <> "") Then
>> > 'Check if file exists
>> > ftp_filename = strFile.Item(0)
>>
Quote:

>> > WScript.Echo ftp_filename
>>
Quote:

>> > if objFSO.FileExists(strFile.Item(0)) then
>> > wscript.Echo strFile.Item(0) & " exists"
>>
Quote:

>> > ftp_local_pending = "\\Secretary\Aspen\Data\Reports\" & Year(date) &
>> > strFile.item(1)
>> > ftp_local_archive = "\\Secretary\Aspen\Data\Archives\" & Year(date)
>> > & strFile.item(1)
>> > WScript.Echo ftp_local_pending
>> > WScript.Echo ftp_local_archive
>>
>> I think you want the Split() function. If the two items are separated
>> on a line by as space, then use something like this ...
>>
>> strFile = Trim(objTextFile.Readline)
>> If (strFile <> "") Then
>> aItems = Split(strFile, " ")
>> 'Check if file exists
>> ftp_filename = aItems(0)
>> ' Note the change of syntax. The file input is a string, not an
>> object.
>>
>> Tom Lavedas
>> ==========
>
> Thanks, I give it a try. How would I split on a Tab?
>
> OldDog
>
> ===========
>
> In the line
> aItems = Split(strFile, " ")
> you replace the the bit after the comma with the tab character chr(9).
Or with the pre-defined constant vbTab.

But I am curious - it appears that the filename is guaranteed to contain no
whitespace, while the quotes around the directory path imply that it might
contain spaces.

If you have control over the format of the file, why not change it to this
format:

\customer1\Sales\input1.txt
\customer2\Sales\input2.txt
\customer number 3\sales\input file 2.txt

You could then use the available file system objects to extract various
components such as drive, parentfolder, basename, extension, and etc.

/Al



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Reading A Text File VB Script
Reading text file at a URL VB Script
Reading file names and Searching for text VB Script
Reading a text file with StreamReader .NET General
Reading text file and charting them via powergadget PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46