Add FileName to a series of TXT files

September 14, 2009 at 16:19:39
Specs: Windows XP, E6750 2.66GHz | 3 Gb Ram
Hello,

I am trying to insert the filename (minus the extension) in front of every line in a series of TXT files. I have put together code to make the change in a single txt file, but am lost as how to get it to perform the action on all TXT files in the folder.

Here is an example of what the TXT files contain:

00:45:10
00:50:04
00:00:03
00:04:57
100
JPEG
579

(There can be more lines after this, but they will follow the same 7 line format.)

Here is an example of what I want the TXT files to look like after:

filename_00:45:10
filename_00:50:04
filename_00:00:03
filename_00:04:57
filename_100
filename_JPEG
filename_579

Here is the code I have created so far:

 @echo off > newfile & setLocal EnableDelayedExpansion
 
 for /f "tokens=* delims=" %%a in (Graphic1000.txt) do (
 echo filename_%%a >> Graphic1000a.txt)

I have only put 'filename_' as a placeholder since I don't know how to get the filename the batch file is currently processing.


See More: Add FileName to a series of TXT files

Report •


#1
September 15, 2009 at 01:01:27
@echo off & setLocal EnableDELAYedExpansion

del *.new 2> nul

for /f "tokens=* delims= " %%f in ('dir/b/a-d *.txt') do (
for /f "tokens=* delims= " %%i in (%%f) do (
echo %%~Nf_%%i >> %%~Nf.new
)
)


=====================================
Helping others achieve escape felicity

M2


Report •

#2
September 15, 2009 at 16:54:19
Thanks Mechanix2Go,

this works perfectly, only 1 thing I can't understand. It adds a
blank " " space to the end of every line. Is there a way to avoid
that?


Report •

#3
September 15, 2009 at 18:16:16
@echo off & setLocal EnableDELAYedExpansion

del *.new 2> nul

for /f "tokens=* delims= " %%f in ('dir/b/a-d *.txt') do (
for /f "tokens=* delims= " %%i in (%%f) do (
echo %%~Nf_%%i>> %%~Nf.new
)
)


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions


Ask Question