Ruriko Posted October 29, 2012 Share Posted October 29, 2012 (edited) 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.txtC:\Documents and Settings\Administrator\Desktop\ArtistCG\[12CUT] _____ (Gakkou no Kaidan) [518382]\test.txtC:\Documents and Settings\Administrator\Desktop\ArtistCG\[2____] _____!__CD__________ [521206]\test.txtC:\Documents and Settings\Administrator\Desktop\ArtistCG\[Ability] _____________________ [514182]\test.txtC:\Documents and Settings\Administrator\Desktop\ArtistCG\[Abo Manten] Kyuusho seme maniacs vol. 3 (Weak Spot Maniacs vol.3) [521993]\test.txtC:\Documents and Settings\Administrator\Desktop\ArtistCG\[Afro] Futanari angel cg collection [521560]\test.txtC:\Documents and Settings\Administrator\Desktop\ArtistCG\[Aim-ZERO] Case Vol.4 [519927]\test.txtC:\Documents and Settings\Administrator\Desktop\ArtistCG\[Air Hike] ________CG_2 [525114]\test.txtNow here's the codeOption ExplicitDim objFSO, strTextFile, strData, strLine, arrLinesCONST ForReading = 1'name of the text filestrTextFile = "C:\Documents and Settings\Administrator\Desktop\ArtistCG\folders.txt"'Create a File System ObjectSet objFSO = CreateObject("Scripting.FileSystemObject")'Open the text file - strData now contains the whole filestrData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll'Split the text file into linesarrLines = Split(strData,vbCrLf)'Step through the linesFor Each strLine in arrLineswscript.echo strLineNext'CleanupSet objFSO = NothingNow 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? Edited October 31, 2012 by Ruriko Link to comment Share on other sites More sharing options...
gunsmokingman Posted October 30, 2012 Share Posted October 30, 2012 Try this vbs script that will read contents of text file and write them to a new text file.Demo_Recursive_List_Text.vbs'-> This code is property of Gunsmokingman and Or Jake1Eye and you must have his permission to use.'-> This is only posted as example code and meant only to used as such.Dim Act :Set Act = CreateObject("Wscript.Shell")Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")Dim Arg, Col, Obj, Ts, TxtTxt = Act.SpecialFolders("Desktop") & "\List_Text_Contents.txt"'-> Makes The Folder Where Script Resides Parent Folder For RecursiveList(Fso.GetFolder("."))'-> Add Path Fso.GetFolder("SomeDrive\FolderName")Function List(Folder)'-> List Files In FolderFor Each Col In Folder.FilesIf LCase(Right(Col,3)) = "txt" ThenSet Ts = Fso.OpenTextFile(Col)Arg = Ts.ReadAllTs.CloseIf Fso.FileExists(Txt) ThenSet Ts = Fso.OpenTextFile(Txt,8)Ts.WriteLine Col.Name & vbCrLfTs.WriteLine ArgTs.CloseElseSet Ts = Fso.CreateTextFile(Txt)Ts.WriteLine Col.Name & vbCrLfTs.WriteLine ArgTs.CloseEnd IfEnd IfArg = ""Next'-> Loop Threw All Sub FoldersFor Each Obj In Folder.SubFoldersList(Obj)NextEnd Function Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now