Batch file for adding heading to files

November 4, 2010 at 02:58:27
Specs: Windows XP
I had such a great response to my last question that I thought I might be able to get help automating another task. I have multiple input files to prepare for a program. To each of these files, I must add the same heading which is as follows:
Bip input for Sites
60 1208
3
50 100
206 500
875 900
2
pop04 30
pop06 30
When working with only a few input files, I have solved the problem by putting this heading in a text file, called header.txt. Then, say, I have 5 input files:
001bip0406.fas
002bip0406.fas
003bip0406.fas
004bip0406.fas
005bip0406.fas
With few files, I treat them individually. At the command line, I enter
copy /b header.txt +001bip0406.fas 001bip0406.txt
I do this 5 times, changing the .fas file and the final .txt file, accordingly. The header.txt file does not change.
However, I really need to do this for a large number of input files, which would be time-consuming to enter commands for each file. Hence, I need to turn this command into a batch file. I would be so very grateful for help to do this.

See More: Batch file for adding heading to files

Report •


#1
November 4, 2010 at 08:36:48
@echo off
setLocal EnableDelayedExpansion
set n=
for %%i in (*.fas) do (
set /a n+=1
set file=%%i
set file!n!=!file:.fas=!
)
set count=
:loop
set /a count+=1
copy /b header.txt+"!file%count%!.fas" "!file%count%!.txt"
if %count%==%n% goto done
goto loop
:done
pause

Report •

#2
November 4, 2010 at 09:32:37
Hi M2G. All the files to which I must add the header are .fas files. However, when I tried the batch file (I called it addhead), I get the following:
D:\Program Files\sites_win32>addhead
001bip0406
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
Press any key to continue . . .

I definitely have the header.txt file in the directory along with the .fas input files.

Do you think there is a problem with how I numbered them: 001bip0406.fas, 002bip0406.fas, 003bip0406.fas, 004bip0406.fas...100bip0406.fas as opposed to 1bip0406.fas, 2bip0406.fas, 3bip0406.fas, 4bip0406.fas...100bip0406.fas?

Also, when I typed the batch script in a text document, I followed your example as it appears--i.e., I hit return when it seemed you started a new line. Does that affect the outcome?

Thanks very much for your advice.


Report •

#3
November 4, 2010 at 11:25:49
Well, the naming shouldn't be a problem. The script has been edited from the first time I posted it. I added in quotes in case your files has spaces. Maybe you could try this again? If you typed it in yourself, I advise you use copy and paste instead.

Report •

Related Solutions

#4
November 5, 2010 at 01:01:04
Hi M2G. I pasted the script and tried it. It worked! I must have mis-typed something. Thank you very much for your help. I couldn't have done this analysis without your advice.

Report •

#5
November 5, 2010 at 04:57:32
You're welcomed. Glad I helped.

Report •


Ask Question