簡體   English   中英

轉到路徑並使用vbscript閱讀txt文件

[英]Go to path & read txt file with vbscript

我有一個充滿目錄的列表,我想遍歷這些目錄並打開txt文件。 然后讀取該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
C:\Documents and Settings\Administrator\Desktop\ArtistCG\[Abo Manten] Kyuusho seme maniacs vol. 3 (Weak Spot Maniacs vol.3) [521993]\test.txt
C:\Documents and Settings\Administrator\Desktop\ArtistCG\[Afro] Futanari angel cg collection [521560]\test.txt
C:\Documents and Settings\Administrator\Desktop\ArtistCG\[Aim-ZERO] Case Vol.4 [519927]\test.txt
C:\Documents and Settings\Administrator\Desktop\ArtistCG\[Air Hike] ________CG_2 [525114]\test.txt

現在這是代碼

Option Explicit

Dim objFSO, strTextFile, strData, strLine, arrLines
CONST ForReading = 1

'name of the text file
strTextFile = "C:\Documents and Settings\Administrator\Desktop\ArtistCG\folders.txt"

'Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Open the text file - strData now contains the whole file
strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll

'Split the text file into lines
arrLines = Split(strData,vbCrLf)

'Step through the lines
For Each strLine in arrLines
wscript.echo strLine
Next

'Cleanup
Set objFSO = Nothing

現在,代碼的問題是它讀取folder.txt的數據,而不是轉到目錄並讀取test.txt文件。 誰能幫我解決這個問題?

代替這個:

wscript.echo strLine

您需要這樣做:

text = objFSO.OpenTextFile(strLine, ForReading).ReadAll

但是,這將在每次迭代時覆蓋變量text的內容,因此您需要確定是要將所有文件的文本放入單個變量中(將從文件中讀取的文本追加到變量中)還是將其分開變量(將每個文件讀入數組的單獨字段或類似內容)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM