How to insert filename into all txt files in a given folder

October 24, 2015 at 12:25:59
Specs: Windows 7
HOW TO INSERT FILENAME INTO first row each TXT FILES IN A GIVEN FOLDER.

is there a dos command that can do this?


See More: How to insert filename into all txt files in a given folder

Report •


#1
October 24, 2015 at 15:04:47
You mean list all .txt files in a directory on one line?
@Echo off
setlocal enabledelayedexpansion
cd "%~dp0"
for /f "tokens=*" %%A in ('dir /b /s "*.txt" 2^>nul') do (
	set line=%%A,!line!
)
echo !line!
pause

Place the script inside the folder you want to read the file names from, otherwise you can change "%~dp0" to the directory you want.

If you mean add text to every .txt file in a folder, it really depends on what you want to add, and if you want to put a variable or just a string there.

Marking answers that solve, or help helps you solve a problem as "Best Answer" also helps other people solve similiar ones in the future.

message edited by RainBawZ


Report •

#2
October 24, 2015 at 20:27:43
I'm making assumptions...
::----- begin script
@echo off & setlocal enabledelayedexpansion
:: for safety, modified files temporarily stored in subdir 'named'
md named
for %%a in (*.txt) do (
>temp echo %%a
copy temp+"%%a" named\"%%a"
)

Report •

#3
October 24, 2015 at 22:20:26
Thanks for the replies but not exactly what I am looking for.

I have a directory located at c:global that contains 100 txt files.

The command that merges all txt files in a given folder is as follows:
copy *.txt mastermerge.txt

I need a DOS command that merges all txt files into ONE to be modified so that the file name of each txt file is brought into the "MASTERMERGE" file during the merge process.

Also, for the reples given above, what APP is used to run the SCRIPTS given?

Thanks a million.


Report •

Related Solutions


Ask Question