Computer Hope

Microsoft => Microsoft DOS => Topic started by: BatchFileCommand on January 18, 2009, 07:34:20 PM

Title: How to see if a string exists in a txt file.
Post by: BatchFileCommand on January 18, 2009, 07:34:20 PM
How would you make the batch file search for a string in a batch file, then if the string exists , it does the specified command.
Title: Re: How to see if a string exists in a txt file.
Post by: BC_Programmer on January 18, 2009, 07:40:05 PM
use find command and errorlevel.
Title: Re: How to see if a string exists in a txt file.
Post by: BatchFileCommand on January 18, 2009, 07:44:41 PM
So would this work.
Code: [Select]
find /i "string" filename
if errorlevel 0 do echo string unfound
Title: Re: How to see if a string exists in a txt file.
Post by: Dias de verano on January 19, 2009, 04:01:26 AM
So would this work.
Code: [Select]
find /i "string" filename
if errorlevel 0 do echo string unfound

Suggestion: try it and see.
Title: Re: How to see if a string exists in a txt file.
Post by: BC_Programmer on January 19, 2009, 04:07:40 AM
So would this work.
Code: [Select]
find /i "string" filename
if errorlevel 0 do echo string unfound

Suggestion: try it and see.


OMG! I was gonna say that!
Title: Re: How to see if a string exists in a txt file.
Post by: BatchFileCommand on January 19, 2009, 08:43:07 AM
Quote
Suggestion: try it and see.

I was looking for a more direct answer  ::). But no, it doesn't work.
Title: Re: How to see if a string exists in a txt file.
Post by: Dias de verano on January 19, 2009, 10:50:25 AM
Quote
Suggestion: try it and see.

I was looking for a more direct answer

Ok, "no".

Quote
it doesn't work.

I can see why.

So can you if you read the error message.

You can roll your eyes all you like. Attitude like that will get you a whole bunch of help, wise guy.



Title: Re: How to see if a string exists in a txt file.
Post by: BatchFileCommand on January 19, 2009, 11:50:13 AM
Unfortunately I turn echo on and there is no error message. I just took a random guess with the errorlevel.
Besides, Dias, why are you always this "I know everything" guy who has no "patience" .
Title: Re: How to see if a string exists in a txt file.
Post by: Dias de verano on January 19, 2009, 12:35:25 PM
Unfortunately I turn echo on and there is no error message. I just took a random guess with the errorlevel.
Besides, Dias, why are you always this "I know everything" guy who has no "patience" .

I am not. I just respond badly to lazy people.

computers.txt contains the text 'bb'

Code: [Select]
@echo off
find /i "bb" computers.txt
if errorlevel 0 do echo string unfound

I saved it as tme.bat

Code: [Select]
C:\>tme

---------- COMPUTERS.TXT
bb
'do' is not recognized as an internal or external command,
operable program or batch file.


Title: Re: How to see if a string exists in a txt file.
Post by: BatchFileCommand on January 19, 2009, 12:46:18 PM
Oh yeah, I took away the do shortly after the first post. Should've edited and took that out. So, any other reason it shouldn't be working?
Title: Re: How to see if a string exists in a txt file.
Post by: Dias de verano on January 19, 2009, 12:51:37 PM
Oh yeah, I took away the do shortly after the first post. Should've edited and took that out. So, any other reason it shouldn't be working?

It depends what you mean by "working".

You have the logic the wrong way around. If errorlevel is 0 that means there was no error, that is, the string was found, not "unfound", (did you mean not found?)
Title: Re: How to see if a string exists in a txt file.
Post by: BatchFileCommand on January 19, 2009, 01:22:18 PM
Oh, I thought if errorlevel was 0 there was an error  ::).
So I need to add a bit more. Would it matter if I have the .txt or not?

Code: [Select]
find /i "text" file
if not errorlevel 0 (echo found) else (echo unfound)
pause>nul

It always says found. I've tried this with the extension on and off the file name.

Title: Re: How to see if a string exists in a txt file.
Post by: Dias de verano on January 19, 2009, 01:54:28 PM
You must always use the full file name including extension.

This works

Code: [Select]
find /i "text" filename.ext
if %errorlevel% EQU 0 (echo found) else (echo not found)
Title: Re: How to see if a string exists in a txt file.
Post by: BatchFileCommand on January 19, 2009, 02:23:24 PM
Finally! It works  ;D.
Title: Re: How to see if a string exists in a txt file.
Post by: BC_Programmer on January 19, 2009, 07:12:12 PM
awwwww. Darn. I was reading the thread and hoping BFC got contrexed. But he isn't worthy of a Contrexing. As entertaining as they are, they likely take some effort.

Now he's gonna ask- "whats contrexing?"
Title: Re: How to see if a string exists in a txt file.
Post by: BatchFileCommand on January 19, 2009, 07:23:08 PM
The reason I may of seemed a bit "feeble-minded" is because I was quite unfamiliar with errorlevel. Contrexing is a word that doesn't exist   :P. Can't fool me there. I checked Wikipedia. 4 different dictionaries. I googled it. It doesn't exist. So what's the meaning of the meaningless word contrexing.
Title: Re: How to see if a string exists in a txt file.
Post by: BC_Programmer on January 19, 2009, 07:29:48 PM
har har har. it's a secret word. Look in the forum archives.


it's standard procedure for a error flag to me a number, and 0 means success. each different number means a different error. That way, one can easily check success by determining if the error code is 0.

Title: Re: How to see if a string exists in a txt file.
Post by: Helpmeh on January 20, 2009, 04:27:34 PM
I wasn't around when Contrex was, but I think I know how it works...

Contrexing (proper-verb=must have capital) is the act of flaming another user, in the way that Contrex would have.

(Is that right?)
Title: Re: How to see if a string exists in a txt file.
Post by: BC_Programmer on January 20, 2009, 05:12:08 PM
I wasn't around when Contrex was, but I think I know how it works...

Contrexing (proper-verb=must have capital) is the act of flaming another user, in the way that Contrex would have.

(Is that right?)

yep, very good.

Contrex is among us.
Title: Re: How to see if a string exists in a txt file.
Post by: Helpmeh on January 20, 2009, 06:40:01 PM
I wasn't around when Contrex was, but I think I know how it works...

Contrexing (proper-verb=must have capital) is the act of flaming another user, in the way that Contrex would have.

(Is that right?)

yep, very good.

Contrex is among us.

Really? Then I have never met him(/her). The way you and Ivy and other members were talking about him (or her) made it sound like (s)he left a while ago.
Title: Re: How to see if a string exists in a txt file.
Post by: BC_Programmer on January 20, 2009, 07:14:25 PM
I wasn't around when Contrex was, but I think I know how it works...

Contrexing (proper-verb=must have capital) is the act of flaming another user, in the way that Contrex would have.

(Is that right?)

yep, very good.

Contrex is among us.

Really? Then I have never met him(/her). The way you and Ivy and other members were talking about him (or her) made it sound like (s)he left a while ago.

lol. He's closer then you realize. It's not me though, before you suspect it.
Title: Re: How to see if a string exists in a txt file.
Post by: Dias de verano on January 21, 2009, 12:15:56 AM
Flaming is just gratuitous rudeness; contrexing is usually deserved.
Title: Re: How to see if a string exists in a txt file.
Post by: BC_Programmer on January 21, 2009, 12:55:29 AM
Flaming is just gratuitous rudeness; contrexing is usually deserved.

agreed.

I knew what it meant- couldn't immaculate it.
Title: Re: How to see if a string exists in a txt file.
Post by: Helpmeh on January 21, 2009, 02:22:20 PM
Flaming is just gratuitous rudeness; contrexing is usually deserved.

OHHH...now I understand.
Title: Re: How to see if a string exists in a txt file.
Post by: BatchFileCommand on January 21, 2009, 05:37:29 PM
So, Contrexing is flaming another person because you think you can judge if they deserve it.
Title: Re: How to see if a string exists in a txt file.
Post by: Helpmeh on January 21, 2009, 06:18:27 PM
So, Contrexing is flaming another person because you think you can judge if they deserve it.
From what I understand, Contrexing is flaming another person BECAUSE they DO deserve it, not flaming someone because you hate their idea.
Title: Re: How to see if a string exists in a txt file.
Post by: BatchFileCommand on January 22, 2009, 06:18:21 AM
Idea has nothing to do with it  :P.
Title: Re: How to see if a string exists in a txt file.
Post by: Dias de verano on January 22, 2009, 10:10:45 AM
Roughly speaking, a contrexing is a rebuke for attitude or bumptiousness or rudeness or severe lack of respect or being a troll.
Title: Re: How to see if a string exists in a txt file.
Post by: BC_Programmer on January 22, 2009, 10:25:33 AM
Roughly speaking, a contrexing is a rebuke for attitude or bumptiousness or rudeness or severe lack of respect or being a troll.


ahhh, yes, Trolls get something they don't expect. They are begging for Flaming, and yet, POOF! they get a contrexing! what you do, is you make it bullet-proof. You take each point given by the poster and give a well-researched, immaculately drawn, bulletproof counterpoint. If you see any direct personal insults that seem out-of-context, then the contrexer is likely a trainee in the fine art of Contrexing. Although, the rules if Contrexing border the land of Flames, so one or two "yo mamas" might slip out even from the most season professional.

"overwhelmed by the dark side you must not be. Use insults in context and not as double-adjectives you must"

-The book of Contrex, "Fire Wrath Thee" 13-40
Title: Re: How to see if a string exists in a txt file.
Post by: BatchFileCommand on January 22, 2009, 04:45:33 PM
Oh, so now contrexing is some kind of rule. Sure....
Title: Re: How to see if a string exists in a txt file.
Post by: Dias de verano on January 23, 2009, 12:13:27 AM
Oh, so now contrexing is some kind of rule. Sure....

Not a rule, more a process.
Title: Re: How to see if a string exists in a txt file.
Post by: Helpmeh on January 24, 2009, 06:45:26 PM
Oh, so now contrexing is some kind of rule. Sure....
Contrexing is not A rule, but it HAS a (almost) unspoken set of rules that you must follow to be successful in Contrexing.
Title: Re: How to see if a string exists in a txt file.
Post by: BatchFileCommand on January 24, 2009, 07:42:19 PM
Quote
Contrexing (proper-verb=must have capital) is the act of flaming another user, in the way that Contrex would have.

What happened to the simple definition.
Title: Re: How to see if a string exists in a txt file.
Post by: BC_Programmer on January 24, 2009, 07:45:23 PM
Quote
Contrexing (proper-verb=must have capital) is the act of flaming another user, in the way that Contrex would have.

What happened to the simple definition.

What simple definition?
Title: Re: How to see if a string exists in a txt file.
Post by: BatchFileCommand on January 24, 2009, 07:52:07 PM
Here's the simple explanation of Contrexing

Quote
Contrexing (proper-verb=must have capital) is the act of flaming another user, in the way that Contrex would have.

But after that it comes out to a process, some kind of proverb. Whatever you people call it.
Title: Re: How to see if a string exists in a txt file.
Post by: BC_Programmer on January 24, 2009, 07:55:18 PM
Here's the simple explanation of Contrexing

Quote
Contrexing (proper-verb=must have capital) is the act of flaming another user, in the way that Contrex would have.

But after that it comes out to a process, some kind of proverb. Whatever you people call it.

The definition advanced because you refused to understand the previous explanations.