Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read txt file line by line

Tags:

vbscript

I am having trouble of making the vbscript 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:

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:

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

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

And this is the vbscript 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 image 680
Trish Pham Avatar asked Oct 31 '12 00:10

Trish Pham


People also ask

How do I read a file line by line?

Method 1: Read a File Line by Line using readlines() readlines() is used to read all the lines at a single go and then return them as each line a string element in a list. This function can be used for small files, as it reads the whole file content to the memory, then split it into separate lines.

How do I read a .TXT file?

You can open a TXT file with any text editor and most popular web browsers. In Windows, you can open a TXT file with Microsoft Notepad or Microsoft WordPad, both of which come included with Windows. To open a TXT file with Notepad, select File → Open....

How do you read each line of a text file as a list in Python?

readlines() returns a list of lines from the file. First, open the file and read the file using readlines() . If you want to remove the new lines (' \n '), you can use strip() .


1 Answers

The line

Set listFile = fso.OpenTextFile(aniLines).ReadAll

won't work for two reasons:

  • Set can only be used when assigning objects to variables, but ReadAll returns a string.
  • OpenTextFile() expects a string, but aniLines is an array of strings.

To process all elements of the array, you could use something like this:

For Each line In aniLines
  ' read dirlist.txt file
Next

The question is how you want to read the dirlist.txt files. You could do the same as you did with the other files: read the entire content of the file and split it into an array:

listFile  = fso.OpenTextFile(line).ReadAll
listLines = Split(listFile, vbCrLf)

and then use another loop like the one above to process the fields of the array listLines. This approach is preferrable for small files, because the code is simpler.

Or you could use the ReadLine approach (which is preferrable when you have to process large files, because it avoids memory exhaustion):

Set listFile = fso.OpenTextFile(line)  ' <-- remove .ReadAll from this line!
Do Until listFile.AtEndOfStream
  fName = listFile.ReadLine
  ' do stuff with fName
Loop

I recommend using Do Until instead of Do While Not, because the semantic is the same, but the former is closer to natural language and thus easier to read.

Also it would be sufficient to instantiate a FileSystemObject object once at the beginning of the script and just use that instance in the rest of the script. It's pointless to instantiate it over and over again.

like image 175
Ansgar Wiechers Avatar answered Sep 25 '22 19:09

Ansgar Wiechers