0

I have a text file with some text in it, I need that text to be as header in all the files which are in different folder using batch script.

Let us say Header.txt is the file with text This is Heading and I want this text to be as heading in each and every text file of the folder C:\...\*.txt.

For example Body.txt is one of the files in different folder with text as I am the body. I want the text in Body.txt to be as:

This is Heading
I am the body
aschipfl
  • 33,626
  • 12
  • 54
  • 99

1 Answers1

2

Try this (adapt file names and paths accordingly):

for %%F in ("C:\...\*.txt") do (
    > "%%~F.tmp" type "Header.txt"
    >> "%%~F.tmp" type "%%~F"
    move /Y "%%~F.tmp" "%%~F"
)

This combines the content of Header.txt and the processed text file in a temporary file (for instance, the text file is Body.txt, so the temporary file is Body.txt.tmp), then it moves the temproary file over the original text file, hence it gets replaced. Note that the heading in the file Header.txt must be terminated with a line-break; otherwise, the heading and the first line of the text file are combined into a single line of text.


In order to check the files whether the already have been inserted the header to, you could redirect their first lines into a variable and compare them with the first line of the header file, like this:

< "Header.txt" set /P HEAD=""
for %%F in ("C:\...\*.txt") do (
    < "%%~F" set /P LINE=""
    setlocal EnableDelayedExpansion
    if not "!LINE!"=="!HEAD!" (
        endlocal
        > "%%~F.tmp" type "Header.txt"
        >> "%%~F.tmp" type "%%~F"
        move /Y "%%~F.tmp" "%%~F"
    ) else endlocal
)

Every single file is checked individually here. For this you need delayed expansion as you are writing to and reading from the same variable within a block of code, that is the for loop.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • Thanks a lot... its working... :) I need an additional functionality on this. Please help me in it.. Once the batch file had run and task has been completed successfully, Again if the user tries to run the same batch file, it should restrict the user in adding text(I mean header) into files and should through an error message like "header is already included in the files. Do not run the batch file". .....Thanks in advance..... – UmamaheswaraReddy Ambati Nov 12 '16 at 06:05
  • I also need to add footer at the end of the line. Footer content will be in separate file. – UmamaheswaraReddy Ambati Jan 17 '17 at 12:12
  • After the `move` command, insert the line `>> "%%~F" type "Footer.txt"`... – aschipfl Jan 17 '17 at 14:19
  • @aschipfl wonderfull script you made long time ago. Its possible to make an extended version where every txt files that are found get a unique header and footer? More exactly Header.txt have 100 lines so if we have 100 txt files found, each of the txt will get unique line. File 1 -> line 1 from header.txt file txt 2 -> line 2 from header.txt. SAME FOR FOOTER. In addition if we have line breacks its ok as 1.. more exactlly if that header with 100 lines have a line breack from 4 to 4 lines, then in files will be added as header four lines as 1, same strategy for footer. Thank you so much! – Ionut Bejinariu Jan 12 '23 at 00:07
  • @IonutBejinariu, this is a different task that needs to be approached in another way as I think; perhaps you try something and when stuck, post a question here and share what you have so far; hint: refer to [this post](https://stackoverflow.com/a/14523100) for how to loop over both header and footer file in parallel… – aschipfl Jan 12 '23 at 16:30
  • I used standard handles before,, i was stuck maybe when you said :"Note that the heading in the file Header.txt must be terminated with a line-break; otherwise, the heading and the first line of the text file are combined into a single line of text." So as I dont want to have spaces, just line after line, I thought will be difficult. I will start an approach. Thank you – Ionut Bejinariu Jan 12 '23 at 16:42
  • @IonutBejinariu, you seem to confuse *spaces* with *line-breaks*: the former separates words, the latter lines. What I meant is, that the line in the header file needs to be terminated by a line-break in order not to become joined to the first line of the file; so when header is `header` and the first line of the file is `first line`, lack of said line-break results in the file to begin with the line `headerfirst line`… – aschipfl Jan 17 '23 at 18:16
  • @aschipfl I got it now,, I am not native English specker.. and sometimes I confuse things... thank you so much for your time and energy for SO community. I found so many interesting scripts made by you on this forum. God bless you for the help giving us. – Ionut Bejinariu Jan 17 '23 at 18:24