1.

Solve : Need a Batch file to append txt in all files first line?

Answer»

I need a batch FILE that, when given a list of files in a folder will insert the filename itself in the first LINE for all the files.

For Eg :- C:\12345 folder is having abc.txt, def.txt, ghi.txt with some junk text in them.

When the batch file is executed it should insert abc in the first line of abc.txt and def in first line of def.txt and ghi in the first line of ghi.txt

Is this POSSIBLE??

Thank u for ur time.
Raj
Anything is possible. Whether batch is the best SOLUTION is another story.

Code: [Select]for /f "tokens=* delims=" %%x in ('dir C:\12345 /a-d /b') do (
echo %%x > c:\temp\%%x
for /f "tokens=* delims=" %%i in (%%x) do (
echo %%i >> c:\windows\temp\%%x
)
)

The batch file must be in the C:\12345 directory and you need to run it logged into that directory (the file paths are not carried forward). The output files are sent to the c:\windows\temp directory. You can change the output directory. This will make a mess of non-text files.



Standard disclaimer: Batch language is not a programming language, it is a command language. There are better tools for this such as any Windows scripting language (vbscript, jscript, perl, rexx, python etc). here's another way:
Code: [Select]for /f "tokens=* delims=" %%x in ('dir C:\12345\*.txt /a-d /b') do (
echo %%x > temp
more /E +1 %%x >> temp
move temp %%x
Hi,
This gives me 2 destination folder where one folder contains files with the content being its file name alone and another folder containing the same set of source files without any modification.

Actually i need one destination folder which contains the same set of source files WITH JUST ONE LINE (FILE NAME) ADDED TO IT AS THE FIRST LINE only.

One more condition to be checked is, if the first token is PROMPT in source file then dont disturb the file, just copy source to destination as such.

Thanx
Raj



Discussion

No Comment Found