![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 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 Specs![]() |
| | #3 (permalink) |
| | 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 > ========== OldDog =========== In the line aItems = Split(strFile, " ") you replace the the bit after the comma with the tab character chr(9). |
My System Specs![]() |
| | #4 (permalink) |
| | 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 - |
My System Specs![]() |
| | #5 (permalink) |
| | 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). 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 Specs![]() |
![]() |
| 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 | |||