我无法让vbscript逐行读取文本。 这是代码应该执行的步骤:
folder.txt包含的示例:
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
folder-list.txt包含的示例:
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
每个dirlist.txt包含
的示例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
这是vbscript代码
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
到目前为止,我只有步骤1到4工作,但我无法阅读dirlist.txt。这里有解决方案吗?
答案 0 :(得分:18)
该行
Set listFile = fso.OpenTextFile(aniLines).ReadAll
不会有两个原因:
Set
只能在将对象分配给变量时使用,但ReadAll
会返回字符串。OpenTextFile()
需要一个字符串,但aniLines
是数组字符串。要处理数组的所有元素,您可以使用以下内容:
For Each line In aniLines
' read dirlist.txt file
Next
问题是您希望如何阅读dirlist.txt
文件。您可以像对待其他文件一样执行操作:读取文件的整个内容并将其拆分为数组:
listFile = fso.OpenTextFile(line).ReadAll
listLines = Split(listFile, vbCrLf)
然后使用上面的另一个循环来处理数组listLines
的字段。这种方法适用于小文件,因为代码更简单。
或者你可以使用ReadLine
方法(当你必须处理大文件时,这是最好的,因为它可以避免内存耗尽):
Set listFile = fso.OpenTextFile(line) ' <-- remove .ReadAll from this line!
Do Until listFile.AtEndOfStream
fName = listFile.ReadLine
' do stuff with fName
Loop
我建议使用Do Until
而不是Do While Not
,因为语义是相同的,但前者更接近自然语言,因此更容易阅读。
在脚本开头一次实例化FileSystemObject
对象就足够了,只需在脚本的其余部分使用该实例即可。一遍又一遍地实例化它是毫无意义的。