I have a list full of directories and I want to loop through those directories and open the txt file. Then read the data inside that txt file and assign to a variable. This is an example of the directory list looks like: 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 Code (markup): Now here's the code 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 Code (markup): Now the problem with the code is it reads the contents of folders.txt instead of going to the directory and read the test.txt file. Can anyone help me fix this?
1) You have to have s "set filename, open file" function for each file, since the files are all in different folders. 2) Change strTextFile = "C:\Documents and Settings\Administrator\Desktop\ArtistCG\folders.txt" to strTextFile = "C:\Documents and Settings\Administrator\Desktop\ArtistCG\[ Go! Go! Heaven!!]_____________25 -______ ___- [525067]\text.txt" for the first one, strTextFile = "C:\Documents and Settings\Administrator\Desktop\ArtistCG\[12CUT] _____ (Gakkou no Kaidan) [518382]\test.txt" for the second one, etc. "folders.txt" isn't "test.txt" in each folder, it's a file named "folders.txt".
Your script is actually very close, you just need to iterate through arrLines elements, use FSO to open files contained in each line path and assign their content to variables. Here is an example: Dim objFSO, strTextFile, strData, strLine, arrLines CONST ForReading = 1 'name of the text file strTextFile = "C:\scripts\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 Redim arrContents(UBound(arrLines) - 1) i = 0 For Each strLine in arrLines If objFSO.FileExists(strLine) Then arrContents(i) = objFSO.OpenTextFile(strLine).ReadAll i = i + 1 End If Next For Each strContent in arrContents WScript.Echo strContent Next 'Cleanup Set objFSO = Nothing Code (markup):