I am completely new to Batch files and have been tinkering around with simple commands. I have figured out how to move text to another file, find a line etc., but I want to be able to add a few lines of text into an already existing text file. Here is what I have so far:
@ECHO OFF
CD C:\Documents and Settings\SLZ1FH\Desktop\New Folder
FOR /F "tokens=*" %%A IN (Examples.txt) DO (
ECHO %%A
IF %%A=="Ex3 3"(
TYPE Line_to_add.txt >> Examples.txt
)
)
if Examples.txt contains:
and Line_to_add.txt contains:
I would like the output to be:
tia :)
@ECHO OFF
FOR /F "tokens=*" %%A IN (Examples.txt) DO (
ECHO %%A
IF "%%A" EQU "Ex3" (
TYPE Line_to_add.txt
)
) >> temp.txt
move /y temp.txt Examples.txt
You never bothered to ask a question, but I can see a few problems with your code.
1) Your IF statement is broken because the left paren is treated as part of the string to be compared. You must have at least one space before your paren.
2) Your comparison will never find a match because the quotes are included in the comparison. Your values have spaces, so both sides of the comparison should be enclosed in quotes.
3) You are attempting to insert into the existing file, which cannot work. Your code would simply append the text to the end of the file. You must write the entire desired result to a new temp file, and then move the temp file to the original name.
First I will ignore the update of the file and show how to get the correct output to the screen.
@ECHO OFF
FOR /F "tokens=*" %%A IN (Examples.txt) DO (
ECHO %%A
IF "%%A" EQU "Ex3 3" (
TYPE Line_to_add.txt
)
)
It is then simple to direct the output to a file and move the file to the desired name.
@ECHO OFF
(
FOR /F "tokens=*" %%A IN (Examples.txt) DO (
ECHO %%A
IF "%%A" EQU "Ex3 3" (
TYPE Line_to_add.txt
)
)
) >temp.txt
move /y temp.txt Examples.txt
Good resources for learning batch syntax and techniques:
1) Every command has online help. Simply add the /? option after the command name. For example, DIR /?
2) Here are some sites with good info
http://www.dostips.com/
http://judago.webs.com/
http://www.robvanderwoude.com/batchfiles.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With