Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding headers on multiple file via batch file

I have a list of .c file where I want to add a header. The files are in a folder and the batch file should put an header to every .c file in that folder. I am inquisitive to know, how could we achieve this.

An example of the header I wanted to insert is as follows:

/////////////////////////////////////////////////////////////////////////////

Name: Tom Volvo Riddle
Roll No: 56/BS/352

////////////////////////////////////////////////////////////////////////////

Thanks,

like image 759
Quixotic Avatar asked Jun 11 '26 17:06

Quixotic


1 Answers

You could use a batch script for loop to output the contents of the header file with each C source file. The output would be redirected into a new file.

Some renaming would be necessary to replace the original file with the new file containing the header.

To set up you would place your header text in the file header.txt. The batch file, header text file and C source should all be located in the same folder.

The original C files will be backed up in the process.

@echo off
for %%F in (*.c) do (
  echo Adding header text to C file %%F
  type header.txt "%%F" > "%%~nF.temp"
  rename "%%F" "%%~nF.bak"
  rename "%%~nF.temp" "%%F"
)
like image 170
Reimeus Avatar answered Jun 14 '26 09:06

Reimeus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!