Solved Adding file name to a text file through batch programming?

December 17, 2012 at 11:23:46
Specs: Windows Vista

I've created a batch program to merges all the .txt files in a directory into one txt file. What I would like to do is add the file names to a field within the txt files.

For example
121101
121102
121103 will be my files
these are dates
(Nov 1 , Nov 2, Nov 3)

My files have no date assigned within the text file. How can I add the Nov 1- 2012 field into 121101?

Is it easier to do before my merging batch program?

copy *.txt merged.txt currently is my batch


See More: Adding file name to a text file through batch programming?

Report •


✔ Best Answer
December 18, 2012 at 06:52:12

@echo off
for %%j in (*.txt) do for /F "delims=" %%k in (%%j) do echo.%%~nj,%%k>> merged.new
ren merged.new merged.txt



#1
December 17, 2012 at 14:21:51

This works, but it puts blank lines between the contents and the names:
type *.txt > merged.txt 2>&1
This could also work, but may be slow:

@echo off>merged.txt
for %%a in (*.txt) do (
>>merged.txt echo %%a
copy merged.txt+%%a
)
::==== end batch


Report •

#2
December 17, 2012 at 18:31:56

Depends on what you mean by "adding the field." If you just want to inject the file name before its contents in the merged file, something like this will do.
type *.txt > merged.out 2>&1

How To Ask Questions The Smart Way


Report •

#3
December 18, 2012 at 05:04:52

Thanks for the replies guys.
That last command does add the file name to the beginning of the original text file.
What I meant by adding a field was

Currently i have this in the original text file 110112

data
data
data

what i was wanting to have is

110112,data
110112,data
110112,data
or
data,110112
data,110112
data,110112

adding the file name at the end of each row/record or beginning of one. Isn't there an append function or something?


Report •

Related Solutions

#4
December 18, 2012 at 06:52:12
✔ Best Answer

@echo off
for %%j in (*.txt) do for /F "delims=" %%k in (%%j) do echo.%%~nj,%%k>> merged.new
ren merged.new merged.txt


Report •


Ask Question