Solved

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

Posted on 2014-11-06
5
99 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 51

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

13 Experts available now in Live!

Get 1:1 Help Now