Computing.Net > Forums > Programming > String search batch help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

String search batch help

Reply to Message Icon

Name: deanamiller
Date: November 19, 2006 at 16:57:08 Pacific
OS: NT 4.0
CPU/Ram: 2.0
Product: Dell
Comment:

I am trying to write simple batch file that will perform a "string" search in C:\textfile.txt then COPY some file to some folder. Should I not use FOR /F ? The below command works but always performs the DO command regardless if the string is found or not.


FOR /F "usebackq delims==" %i IN (`FIND /I "error" C:\logfile.log`) DO echo Error Found > D:\report.txt

-Thanks



Sponsored Link
Ads by Google

Response Number 1
Name: Shr0Om
Date: November 20, 2006 at 00:43:12 Pacific
Reply:

You dont have to use a FOR loop.
Try:

type "c:\logfile.log" |find "error"
if not errorlevel 1 echo "Error found" >"d:\report.txt"


0

Response Number 2
Name: Mechanix2Go
Date: November 20, 2006 at 03:54:22 Pacific
Reply:

Hi Shroom,

I agree; no need for FOR.

I'd prefer this as a first line:

find "error" < logfile.log > nul


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 3
Name: deanamiller
Date: November 20, 2006 at 20:08:27 Pacific
Reply:

Thanks Shr00m I'm almost there but can I use a command rather than a > D\output.txt ? I need to copy a file rather than produce output. I was using > D\output.txt just to see if I could get the .bat to work hoping it would be simple to change. I tried XCOPY and COPY but that produced nothing useful.


0

Response Number 4
Name: Mechanix2Go
Date: November 20, 2006 at 21:15:29 Pacific
Reply:

I assume you want to copy a file based on the result of the string search. If so, you need to know that FIND sets an errorlevel which you can use for decision making.

If you want to do a copy if the string is found:

::== my.bat
find "error" < logfile.log > nul
if errorlevel 1 goto :eof
copy c:\somefile d:\
:: DONE



=====================================
If at first you don't succeed, you're about average.

M2



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: String search batch help

extracting a part of string in Batch File www.computing.net/answers/programming/extracting-a-part-of-string-in-batch-file/18991.html

Need some help with string search www.computing.net/answers/programming/need-some-help-with-string-search/20328.html

search for string in batch www.computing.net/answers/programming/search-for-string-in-batch/16124.html