Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-11-06
5
Medium Priority
?
112 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 59

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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

926 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