tags:

views:

78

answers:

3

Hello,

i'm creating a batch file and just a little thing i want to know:

my (WINDOWS) batch file needs to search 'hello' in a text file.

If it is there, it should use the GOTO command to somewhere else in the batch file.

If it isn't there, it should write 'hello' on a new line.

How can you do that?

I know that you can use FIND and ERRORLEVEL, but i don't know how.

+1  A: 

If you are using windows batchfiles, check if you can use helpers like the Unxtools. They are adding a lot of functionality.
Then you can use a grep and check the errorlevel (return value) ...

EDIT:
Here is some code to give you an idea:

find /i /c "hello" file.txt
if %errorlevel% equ 0 goto :found
echo hello
tanascius
i dont think you need unxtools, i think you can use the FIND command, but i'm not sure. And please give the code.
YourComputerHelpZ
You don't need them - but I recommend to have a look at them!
tanascius
what would if %errorlevel%... do? will it GOTO if it IS Found?
YourComputerHelpZ
its good now. i did it. Thanks!
YourComputerHelpZ
open a console and enter the find command - the very next line write "echo %errorlevel%" ... you can see that it returns 0 or 1, depending on what is found in your file ... it should do what you want - if hello is found the goto is used, otherwise hello is printed.
tanascius
it doesnt. it should have been 'equ 0' because now it goes to :found when it is not found
YourComputerHelpZ
I corrected the code
tanascius
does not work on windows 7... Gives me a list of all the folders in the c directory. it says something like: find /i is no medium or so?
YourComputerHelpZ
A: 

This may not be the most helpful answer... but, does it have to be a batch file? It sounds like Perl might be a better fit for what you are trying to do.

CressNZ
nah, i think that's not needed. I thought you could use something with FIND and ERRORLEVEL, but i dont know how.
YourComputerHelpZ
A: 

Try this one

echo off
find /c "hello" myfile.txt > nul
if errorlevel 1 goto notfound
echo hello
:notfound
Can
no, i dont want to do it like that, if it IS found, than use GOTO.if its NOT found, than it has to write that to the text file on a new line.
YourComputerHelpZ
Come on... he gave you a solution, you can customize it the way you want... I don't think that you can expect other people to write your code. Take a look at http://www.allenware.com/icsw/icsw070.htm for appending text to a file.
x3ro