Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading txt line by line 1

Status
Not open for further replies.

SushiLover

Programmer
Oct 29, 2012
5
AU
I am having trouble of making the script to read the text line by line.
This is the steps the code should do:

1. Read folder.txt
2. Open the file listed in folder.txt
3. Echo the contents inside of test.txt
4. Read folder-list.txt
5. Open the file listed in folder-list.txt
6. Open dirlist.txt and echo line by line

An example of what folder.txt contains:
Code:
C:\Documents and Settings\Administrator\Desktop\ArtistCG\[ Go! Go! Heaven!!]_____________25 -______ ___- [525067]\test.txt
C:\Documents and Settings\Administrator\Desktop\ArtistCG\[12CUT] _____ (Gakkou no Kaidan) [518382]\test.txt
C:\Documents and Settings\Administrator\Desktop\ArtistCG\[2____] _____!__CD__________ [521206]\test.txt
C:\Documents and Settings\Administrator\Desktop\ArtistCG\[Ability] _____________________ [514182]\test.txt

An example of what folder-list.txt contains:
Code:
C:\Documents and Settings\Administrator\Desktop\ArtistCG\[ Go! Go! Heaven!!]_____________25 -______ ___- [525067]\dirlist.txt
C:\Documents and Settings\Administrator\Desktop\ArtistCG\[12CUT] _____ (Gakkou no Kaidan) [518382]\dirlist.txt
C:\Documents and Settings\Administrator\Desktop\ArtistCG\[2____] _____!__CD__________ [521206]\dirlist.txt
C:\Documents and Settings\Administrator\Desktop\ArtistCG\[Ability] _____________________ [514182]\dirlist.txt

An example of what each dirlist.txt contains
Code:
C:\Documents and Settings\Administrator\Desktop\ArtistCG\[ Go! Go! Heaven!!]_____________25 -______ ___- [525067]\00.jpg
C:\Documents and Settings\Administrator\Desktop\ArtistCG\[ Go! Go! Heaven!!]_____________25 -______ ___- [525067]\a_01.jpg
C:\Documents and Settings\Administrator\Desktop\ArtistCG\[ Go! Go! Heaven!!]_____________25 -______ ___- [525067]\a_02.jpg
C:\Documents and Settings\Administrator\Desktop\ArtistCG\[ Go! Go! Heaven!!]_____________25 -______ ___- [525067]\a_03.jpg
Code:
Option Explicit

Dim objFSO, strTextFile, strData, strLine, arrLines, aniTextFile, aniData, aniLines, meLine, objTextFile, fso, inputFileList, sFolderName, fname
Dim iim1, iret, iret2, iret3, i
CONST ForReading = 1

strTextFile = "C:\Documents and Settings\Administrator\Desktop\ArtistCG\folder.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll
arrLines = Split(strData,vbCrLf)

For Each strLine in arrLines
  strData = objFSO.OpenTextFile(strLine,ForReading).ReadAll
WScript.Echo strData

aniTextFile = "C:\Documents and Settings\Administrator\Desktop\ArtistCG\folder-list.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
aniData = objFSO.OpenTextFile(aniTextFile,ForReading).ReadAll
aniLines = Split(aniData,vbCrLf)

Set fso = CreateObject("Scripting.FileSystemObject")
Set listFile = fso.OpenTextFile(aniLines).ReadAll
do while not listFile.AtEndOfStream 
    fName =  listFile.ReadLine()
    WScript.Echo fName
    Loop
Next

So far I only got steps 1 to 4 working but I can't get it to read dirlist.txt. Any solutions here?
 
Like this ?
Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
strTextFile = "C:\Documents and Settings\Administrator\Desktop\ArtistCG\folder.txt"
strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll
For Each strLine In Split(strData,vbCrLf)
  strData = objFSO.OpenTextFile(strLine,ForReading).ReadAll
  WScript.Echo strData
Next
aniTextFile = "C:\Documents and Settings\Administrator\Desktop\ArtistCG\folder-list.txt"
aniData = objFSO.OpenTextFile(aniTextFile,ForReading).ReadAll
For Each aniLine In Split(aniData,vbCrLf)
  Set listFile = objFSO.OpenTextFile(aniLine,ForReading)
  Do While Not listFile.AtEndOfStream
    fName = listFile.ReadLine
    WScript.Echo fName
  Loop
  listFile.Close
Next

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
The looping isn't exactly how I wanted. Here's how I want to loop:
Read the 1st line of folder.txt and 1st line of folder-list.txt.
Loop all the lines in dir.txt
Go to the next line of folder.txt & folder-list.txt
Loop all the lines in dir.txt

and so on...

I tried moving the for loop around but it didn't loop right
Code:
Option Explicit

Dim objFSO, strTextFile, strData, strLine, arrLines, aniTextFile, aniData, aniLines, aniLine, objTextFile, fso, inputFileList, listFile, fname
Dim iim1, iret, iret2, iret3, i
CONST ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
strTextFile = "C:\Documents and Settings\Administrator\Desktop\ArtistCG\folder.txt"
strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll
For Each strLine In Split(strData,vbCrLf)
  strData = objFSO.OpenTextFile(strLine,ForReading).ReadAll
  WScript.Echo strData

aniTextFile = "C:\Documents and Settings\Administrator\Desktop\ArtistCG\folder-list.txt"
aniData = objFSO.OpenTextFile(aniTextFile,ForReading).ReadAll
For Each aniLine In Split(aniData,vbCrLf)
  Set listFile = objFSO.OpenTextFile(aniLine,ForReading)
  Do While Not listFile.AtEndOfStream
    fName = listFile.ReadLine
    WScript.Echo fName
  Loop
  listFile.Close
Next 
Next
 
Like this ?
Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
strTextFile = "C:\Documents and Settings\Administrator\Desktop\ArtistCG\folder.txt"
strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll
arrLines = Split(strData,vbCrLf)
aniTextFile = "C:\Documents and Settings\Administrator\Desktop\ArtistCG\folder-list.txt"
aniData = objFSO.OpenTextFile(aniTextFile,ForReading).ReadAll
aniLines = Split(aniData,vbCrLf)
For i = 0 To UBound(arrLines)
  strData = objFSO.OpenTextFile(arrLines(i),ForReading).ReadAll
  WScript.Echo strData
  Set listFile = objFSO.OpenTextFile(aniLines(i),ForReading)
  Do While Not listFile.AtEndOfStream
    fName = listFile.ReadLine
    WScript.Echo fName
  Loop
  listFile.Close
Next

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top