Another batch delight!

Hewlett-packard / Iq505
January 19, 2009 at 15:00:26
Specs: Windows Vista , Dual Core/4GB
I have been reading my way into the batch world but I can't seem to get a few things to work. :( I know how to add text to the end of a file with the echo command and these symbols now -- >> -- but how do I add a single line of text to the Start of a file? Please don't laugh at my batch ignorance. :(

See More: Another batch delight!

Report •

January 20, 2009 at 00:17:35
Batch doesn't have any method to do this as such. What can be done to work around this it redirect >> the new top line/lines into a new file and use the type command to redirect the old file into the new file.

>newfile echo this will be line 1
>>newfile echo this will be line 2
>>newfile type oldfile

If your wondering why the redirection >> and file names are before the commands and not after them it is because that if lines end in numbers you must leave a space before the redirection for the command to execute properly. This if fine if your not redirecting echo, but if you are the output will contain the space which can be undesirable. Doing the redirection first solves this problem.

Report •

January 20, 2009 at 18:13:52
Thanks for the reply Judago but I think I was unclear in describing exactly what my challenge is... I have hundreds of strangely named files that I need to insert a single line of text into while keeping the filename the same on all of the text files. For example...
If I have empty text files named:


How do I insert a line that says "Good Morning" in both of these files while keeping the original file names?

Report •

January 20, 2009 at 22:59:03
If the file is empty, how does it matter whether you append at the end or beginning?
if you want to append to only empty files:
for %%a in (*.txt) do if %~za==0 echo good morning >> %%a

Otherwise, that is if you want to append to every file, we create a temp file and then overwrite the old file with the temp file:

for %%a in (*.txt) do (
>%%a.# echo Good Morning
>>%%a.# type %%a
move/y %%a.# %%a


Report •

Related Solutions

January 22, 2009 at 18:48:31
Yahoo! Thank you, Thank you, Thank you Holla, sorry it took me so long to reply. I definitely appreciate the response but I just saw it. This script works great!! Can I bother you to find out what some of it means? I have been reading the online tutorials but I can't figure out what some of your code is doing... :( What is does this chunk mean?


And why are the pieces of the code on different lines? Is that just word wrapping? I tried moving everything to one line but the script broke when I tried that so I quickly put it back to how you had it. :)

The other part I don't understand is basically the rest of the script. :( What does this part mean?

>>%%a.# type %%a
move/y %%a.# %%a

Thanks again. It works, but I can't figure out why? I tried lots of mutations of the TYPE and copy commands for this but they all failed. Take a look at my experiments...

@echo off
@TYPE <<"C:\TEST\Please prepare for the following.txt">>New.txt
@TYPE BlankLine.txt>>New.txt

echo off
@COPY "C:\TEST\Please prepare for the following.txt"+BlankLine.txt+C:\TEST\Insert\*.txt C:\TEST\*.txt

The document named "Please prepare for the following.txt had one line of text that said "Please prepare for the following" and I was trying to join this documetn with another document that had a single carriage return in it... but it all failed miserably after loads of trials. :( I will be greatful for any enlightenment on these things. Thanks again for the great script. :)

Report •

January 22, 2009 at 22:17:50
1. for %%a in (*.txt) do (
2.   >%%a.# echo Good Morning
3.   >>%%a.# type %%a
4.   move/y %%a.# %%a 
5. )

1. For every *.txt, execute all the commands until the "(" bracket is closed with a closing ")" at line5.
Let %%a be the name of the text file.
%%a is the way to use a "variable" in batch file's for loop. type "for /?" at the cmd prompt for help on this.

2. Echo "good morning" to a temp file named %%a.# (if the original file is named f1.txt, the temp file will be named f1.txt.#). This file does not exist. It will be created when you echo. ">%%a.# echo Good Morning" is same as echo Good Morning >%%a.#
3. type the original file %%a and append it to the temp file. Same as type %%a >> %%a.#
4. Move the temp file or original file and overwrite it with the temp file.
5. Indicate the close of for loop with a ")"



Report •

January 23, 2009 at 05:17:23
Oh my... I guess I have a long way to go before I become proficient in batch scripting. :( Thanks a bunch for the explanation. I will play around with this a bit. Just one more question... When did you set the variable to be %%a? ...and where is %%a in the For/? command? I searched through cmd help but it only mentions %%i :( I guess I'm just too clueless but I really wish I could learn this magic of batch scripting. Thanks for the help.

Report •

January 23, 2009 at 18:54:01

From the windows' start menu, run cmd.exe

This launches something called command window
or console.

Now type "for /?" in this command window.
That is help for the for command.


Report •

January 23, 2009 at 19:07:37
okay thanks Holla

Report •

Ask Question