Solved

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

Posted on 2014-11-06
5
105 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 53

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Input information on K column using userform. 15 43
how to add field in my script 2 35
VBS to check if Visio file open and if so close it 4 75
Enable Intel-VT on a IBM Yoga 7 33
In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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