• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Refresh on Reading more than one variable from a Text file

O

OldDog

#1
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
 
T

Tom Lavedas

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

> 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
==========
 
P

Pegasus \(MVP\)

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

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

> > 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
> ==========
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).
 
O

OldDog

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

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

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

> > > 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
> > ==========
>
> 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
 
A

Al Dunbar

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

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

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

>> > 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
>> ==========
>
> 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