d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Vbs Merge Files
Add Reply New Topic New Poll
Member
Posts: 8,635
Joined: Dec 28 2007
Gold: 87.00
Oct 1 2012 04:08am
So I need to find all .log files from one folder and its subfolders and then merge them in to one single file.

With this code I can do this from one folder, but it excludes the subfolders:
Code
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFSO.CreateTextFile("output.txt")

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set FileList = objWMIService.ExecQuery _
   ("ASSOCIATORS OF {Win32_Directory.Name='C:\temp'} Where " _
       & "ResultClass = CIM_DataFile")

For Each objFile In FileList
   Set objTextFile = objFSO.OpenTextFile(objFile.Name, ForReading)  
   strText = objTextFile.ReadAll
   objTextFile.Close
   objOutputFile.WriteLine strText
Next

objOutputFile.Close


I have tried this for main folder + subfolders, but so far I only get the files from main folder merged and message "file not found" at position where it looks for files in subfolders:
Code
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFSO.CreateTextFile("output.txt")

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set FileList = objWMIService.ExecQuery _
   ("ASSOCIATORS OF {Win32_Directory.Name='C:\temp'} Where " _
       & "ResultClass = CIM_DataFile")

For Each objFile In FileList
   Set objTextFile = objFSO.OpenTextFile(objFile.Name, ForReading)  
   strText = objTextFile.ReadAll
   objTextFile.Close
   objOutputFile.WriteLine strText
Next

ShowSubfolders objFSO.GetFolder("C:\temp")

Sub ShowSubFolders(Folder)
   For Each Subfolder in Folder.SubFolders
       Set objFolder = objFSO.GetFolder(Subfolder.Path)
       Set colFiles = objFolder.Files
 For Each objFile In colFiles
  Set objTextFile = objFSO.OpenTextFile(objFile.Name, ForReading)  
  strText = objTextFile.ReadAll
  objTextFile.Close
  objOutputFile.WriteLine strText
       Next
       ShowSubFolders Subfolder
   Next
End Sub

objOutputFile.Close


Any suggestions to fix this issue?
Member
Posts: 8,635
Joined: Dec 28 2007
Gold: 87.00
Oct 1 2012 05:23am
Got it working now. Pretty new to VBS so was missing just a little there but couldnt figure it out first.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll