Link to home
Start Free TrialLog in
Avatar of brasiman
brasiman

asked on

How to combine txt files and change .txt extension

I have multiple .txt files in one folder. Each .txt file has a few rows of data. How would I create a batch file that will combine the rows on all the text files into one .txt file. So if there are 5 .txt files and 2 rows in each of those text files, the new .txt file would have 10 rows of data. Also, when this new file is created, I need the file extension to be ".tra". The file name can be "TR2016" Is there a way to do this? Thanks!
Avatar of NVIT
NVIT
Flag of United States of America image

Note: You have not specified if the files must be output in a certain order. e.g. file1.txt, file2.txt, etc. If so, code must be revised.

Make a .bat file of this.
Change FNSrcPatt, and FNOut values per your needs.

@echo off
setlocal enabledelayedexpansion

set FNSrcPatt=c:\local\test\*.txt
set FNOut=TR2016.tra
for %%a in ("%FNSrcPatt%") do (type %%a>>"%FNOut%")

Open in new window

Avatar of brasiman
brasiman

ASKER

Thank you NVIT! This is perfect! One thing I forgot to ask, what if I want to move the .tra file to a different location? So the .txt files are in c:\folder and i want to create the .tra file in the c:\TRA_Files.
ASKER CERTIFIED SOLUTION
Avatar of NVIT
NVIT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
NVIT, thats perfect. Thank. I just ran into the last thing with this. And if i need to start another question I can. When I run this, the old .txt files are left behind in c:\folder\. Is there a way, keeping the script as is, just adding a way for me to specify a folder that those .txt files are put...and have a folder created with it date and time stamped? So, using what you suggested, but then adding code where I specify where to create a new folder, the title of the folder is the current date and time, then the .txt files are moved to the newly created folder. Does that make sense? Thanks!
Hi brasiman.

Please close this question. I'll help you with the next one. In that one, add a link to this page.
Hi NVIT, I'll close this one now. Here's the link to the new question:
https://www.experts-exchange.com/questions/28972004/How-to-combine-txt-and-create-a-new-folder.html
Perfect! Replied quickly. Thanks!