Avatar of Mary Martis
Mary Martis
 asked on

VB Script to merge multiple text files into one inorder of last modified time

I want to merge multiple text files into one file according the modified time through a VB Script or Powershell Script text1.txt modified at 01:00 Text2.txt modified at 04:00 text3.txt modified at 06:00

Output should be "TextOutput.txt" which has file contents in the sequence Text1, text2.text3 contents.
PowershellVB Script

Avatar of undefined
Last Comment
oBdA

8/22/2022 - Mon
Tahir Qureshi

try this please

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='z:\Scripts\Test'} 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

Open in new window

oBdA

Powershell:
Get-ChildItem C:\Temp\*.txt | Sort-Object LastWriteTime | Get-Content | Set-Content "C:\Temp\TextOutput.txt"

Open in new window


thr qureshi,
that does not sort by Modified date, it just takes the files as they come.
Mary Martis

ASKER
Thank you , Is there a VB script for this too, @oBda
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Mary Martis

ASKER
Yes i agree , server was in windows 2003 , so i wanted VB script, But now had changed the server in which i will run , so PS makes the best solution, Thanks @oBdA
oBdA

FWIW: Even XP/Server 2003 support up to PS 2.0.
Windows Management Framework (Windows PowerShell 2.0, WinRM 2.0, and BITS 4.0)
https://support.microsoft.com/kb/968929