Solved

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

Posted on 2014-11-06
5
102 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
  • 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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now