MSFN Forum: [Solved] Go to path and read txt file in vbscript - MSFN Forum

Jump to content


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[Solved] Go to path and read txt file in vbscript Rate Topic: -----

#1 User is offline   Ruriko 

  • Group: Members
  • Posts: 6
  • Joined: 27-October 12
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 29 October 2012 - 05:45 PM

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


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


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?

This post has been edited by Ruriko: 30 October 2012 - 06:48 PM



#2 User is offline   gunsmokingman 

  • MSFN Master
  • Group: Super Moderator
  • Posts: 2,356
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 29 October 2012 - 08:48 PM

Try this vbs script that will read contents of text file and write them to a new text file.

Demo_Recursive_List_Text.vbs
 Dim Act :Set Act = CreateObject("Wscript.Shell")
 Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
 Dim Arg, Col, Obj, Ts, Txt
 Txt = Act.SpecialFolders("Desktop") & "\List_Text_Contents.txt"
'-> Makes The Folder Where Script Resides Parent Folder For Recursive
  List(Fso.GetFolder("."))
'-> Add Path Fso.GetFolder("SomeDrive\FolderName") 
    Function List(Folder)
'-> List Files In Folder
    For Each Col In Folder.Files
     If LCase(Right(Col,3)) = "txt" Then
      Set Ts = Fso.OpenTextFile(Col)
      Arg = Ts.ReadAll
      Ts.Close 
     If Fso.FileExists(Txt) Then
       Set Ts = Fso.OpenTextFile(Txt,8)
       Ts.WriteLine Col.Name & vbCrLf 
       Ts.WriteLine Arg
       Ts.Close       
      Else
       Set Ts = Fso.CreateTextFile(Txt)
       Ts.WriteLine Col.Name & vbCrLf 
       Ts.WriteLine Arg
       Ts.Close
      End If             
     End If
     Arg = "" 
    Next
'-> Loop Threw All Sub Folders     
    For Each Obj In Folder.SubFolders     
     List(Obj)
    Next
   End Function



Rename Demo_Recursive_List_Text.vbs.txt to Demo_Recursive_List_Text.vbs make active
Attached File  Demo_Recursive_List_Text.vbs.txt (1.03K)
Number of downloads: 1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2013 msfn.org
Privacy Policy