Solved

How can I append a text file to another text file?

Posted on 2014-11-06
5
109 Views
Last Modified: 2014-11-24
I would like to append a text file to another text file.
They should be appended by order of date and time, with the data for the most recent to be on the top.
0
Comment
Question by:100questions
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 12

Expert Comment

by:jkaios
ID: 40427368
Sub appendFile()

   Dim WshShell As Object
   Set WshShell = CreateObject("WScript.Shell")
   WshShell.Run "cmd /C type C:\Test\testfile.txt >> C:\Test\testfile2.txt"

End Sub
0
 
LVL 12

Expert Comment

by:jkaios
ID: 40427374
Or to make the sub routine more flexible:

Sub appendFile(byval strSourceFile, byval strTargetFile)

   Dim WshShell 'As Object   ------- comment out the "AS OBJECT" if runing this code from a VB script
   Set WshShell = CreateObject("WScript.Shell")
   WshShell.Run "cmd /C type " & strSourceFile & " >> " & strTargetFile

End Sub

Open in new window

0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 40427511
Not sure exactly what you are after.  Are there just 2 files involved, or are there multiple files to be added to the end of a given base file?  Need more details...

Does this have to be VBS or would BAT also work?

~bp
0
 
LVL 12

Accepted Solution

by:
jkaios earned 500 total points
ID: 40427550
Just saw the "batch" tag so if you want to do it from a Batch file, then this is another way:

Inside the batch file (actual commands in upper case - not required though):

@ECHO OFF
CLS
ECHO Appending contents of %1 to %2
TYPE %1 >> %2
:: echo with a period followed creates a blank space, not required
ECHO.
ECHO Operation completed
ECHO.
:: this line optional, it's only useful to see results when running batch from outside the command prompt
PAUSE
@ECHO ON

Open in new window


Copy code above and paste to Notepad, save the file as MyAppend.bat, for example.

Now from any command prompt just type:

   
MyAppend.bat C:\temp\file1.log  c:\test\file2.txt

Open in new window

0
 

Author Closing Comment

by:100questions
ID: 40462856
Thanks.  Unsure if this works, however I will try it when possible.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question