1

I am using batch file to write to a file, and want to not add the same thing if it already being added. I have tried this which has worked.

find "jackdows loves me" %appdata%\data.html || echo jackdows loves me >>%appdata%\data.html

However, I can't make this one work as there are special chars:

find "<!-- saved from url=(0017)http://localhost/ -->" %appdata%\data.html || echo <!--  saved from url=(0017)http://localhost/ --> >>%appdata%\data.html 

What is the correct way to do that?

1
  • I notice that the strings for find and echo are not equal (one vs. two spaces before the word "saved") Might it be, that you search for the wrong string (or is it just a typing error)? Besides that, @DavidHoelzer's answer is correct. Commented Oct 26, 2014 at 8:29

1 Answer 1

4

To escape < and > in anything in the NT line of OSs, use the caret:

echo ^< > test.out

Sign up to request clarification or add additional context in comments.

7 Comments

Correct. :) That's all there is to it.
it works, but it always adds the data, doesnt look if its already added or not. Here is a line I used for find "^<!-- saved from url=(0017)localhost --^>" %appdata%\data.html || echo ^<!-- saved from url=(0017)localhost --^>>>%appdata%\data.html
I've never tried it but I was surprised that || worked in that way in a Batch script at all, but I assumed you were correct because you claimed it functioned in another context... I don't know what to tell you about that.. I would have approached this with an if ERRORLEVEL check, but the caret solved the question that you asked. :)
Well, certainly.. but I meant that I would have run the find and then done an IF ERRORLEVEL 1 ECHO... You may want to double check, but my memory says that if FIND doesn't find what you search for it sets ERRORLEVEL to 1. Makes the logic two lines but, as I said, I don't remember being able to do an DO A || DO B in a Batch script. :)
@DavidHoelzer: || works as "if previous command failed, then". && works as "if previous command was successful, then".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.