Batch: Insert custom txt + own filename at Beg. of txt file

April 14, 2012 at 05:34:22
Specs: Windows 7
Hia at all!

I have following questions

I have a txt file, which is called "country.txt".
The content are different countrys but it doesn't matter anywas

Now I'd like to insert a custom text into the first line, in my case:


after this custom text, there should be inserted a space and after that the filename. For my example country.txt, it should look like that:


In the next line there should be inserted the normal file content
But in the last line, after the end of the file, I would like to add the following text:


This whole procedure should be done automatically with many *.txt files in one folder

Is that technically possible with a batch file?

I'm thankful for any Help! Greetings from Germany!


See More: Batch: Insert custom txt + own filename at Beg. of txt file

Report •

April 15, 2012 at 02:15:01
Batch scripting does not allow for the insertion or amendment of text lines in situ, the file content must first be read then amended then written to a new file.

Please test the following script.

@echo off
setlocal enabledelayedexpansion

for /f "tokens=*" %%1 in ('dir /b *.txt') do (
    set filename=%%~n1

    set outfile="%temp%\%%1"

    echo ADD VALUE LABELS !filename!>!outfile!
    more "%%1">>!outfile!
    echo EXECUTE.>>!outfile!

Good luck.

Please come back & tell us if your problem is resolved.

Report •
Related Solutions

Ask Question