find command not working as expected

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

find command not working as expected

#1 Post by MKANET » 23 May 2012 14:38

The below command-line produced the following respective output

Command-line:
mt.exe mediastatus tape0

Output:

Code: Select all

Capacity (Bytes)        : 0x32f2800000 (218816839680)
Remaining (Bytes)       : 0x31bfd00000 (213671477248)
Block Size              : 0x00000200 (512)
Partition Count         : 0x00000000 (0)
Write Protected         : No
Current Partition       : 0
Absolute Block Position : 0x00000000 0x00000000 (0 0)
Absolute Byte Position  : 0x0000000000000000
Status                  : Ready


If I use the simple command-line shown below to find the string of characters between the quotes, the find command doesnt find anything. Since there's a possibility that there may be other instances of the string "No", I can't search for just "No" unfortunately.

Code: Select all

mt.exe mediastatus tape0| find "Write Protected         : No



The find command can find "Write Protected" and ":No" separately, just not together with spaces in between. I have verified that the number of spaces in between are correct.

What's causing find to not work in this case?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: find command not working as expected

#2 Post by aGerman » 23 May 2012 14:49

Use FINDSTR instead. The /c:"string with spaces" should return what you need.

Regards
aGerman

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: find command not working as expected

#3 Post by MKANET » 23 May 2012 15:01

Hi aGerman, it looks like there is something else wrong. The firststr can't find that string of text either.

Is it because there's a tab in between? However, when I copy and paste that string of text; I only get spaces, I dont get a tab. So, if there's tabs there, they are invisible to me. If it is indeed tabs, how to insert a couple of tab spaces within the quotes?


Also, sorry to make this difficult, but I also have one more thing that's related.

Immediately after the find command, I do an IF statement to check for errorlevel. Is there any way to "find" (or findstr) two completely separate strings from the same output (shown above) and do respective errorlevel checks without writing to a temp file?

Thanks so much aGerman!

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: find command not working as expected

#4 Post by aGerman » 23 May 2012 15:11

Step by step :wink:

The console window displays spaces even if tabs are outputted from the tool.
Redirect the output to a file and check with notepad (or even better with a HEX editor) whether tabs are included and what character encoding is returned.

Regards
aGerman

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: find command not working as expected

#5 Post by MKANET » 23 May 2012 15:47

aGerman, that was a great idea! Hexedited the output that was saved to a file. There are two tab spaces exactly

"Write ProtectedTABTAB: No"

Squashman
Expert
Posts: 4473
Joined: 23 Dec 2011 13:59

Re: find command not working as expected

#6 Post by Squashman » 23 May 2012 15:48

MKANET wrote:Immediately after the find command, I do an IF statement to check for errorlevel. Is there any way to "find" (or findstr) two completely separate strings from the same output (shown above) and do respective errorlevel checks without writing to a temp file?

Why write to a temp file? Set a variable equal to something if the find is true. Use the double && for conditional execution.

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: find command not working as expected

#7 Post by MKANET » 23 May 2012 15:53

Could you show me please? I have no idea how to do that. Im not sure how to do && conditional execution.

Lets supposed I first need to check for "Capacity" string from the output. If that's true, then to check whether "write protected :NO" was found or not (from the same output "Capacity" string came from).


Squashman wrote:
MKANET wrote:Immediately after the find command, I do an IF statement to check for errorlevel. Is there any way to "find" (or findstr) two completely separate strings from the same output (shown above) and do respective errorlevel checks without writing to a temp file?

Why write to a temp file? Set a variable equal to something if the find is true. Use the double && for conditional execution.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: find command not working as expected

#8 Post by aGerman » 23 May 2012 16:07

Is it sufficient to count the found lines?

Untested

Code: Select all

set /a n=0
for /f %%i in ('mt.exe mediastatus tape0^|findstr /c:"string 1" /c:"string 2"') do set /a n+=1
echo %n%

n should be 2 if both strings are found.

Regards
aGerman

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: find command not working as expected

#9 Post by MKANET » 23 May 2012 16:16

Thanks. I'd like to test it out. But, but I still need to somehow specify the tab key twice in "string 2"

aGerman wrote:Is it sufficient to count the found lines?

Untested

Code: Select all

set /a n=0
for /f %%i in ('mt.exe mediastatus tape0^|findstr /c:"string 1" /c:"string 2"') do set /a n+=1
echo %n%

n should be 2 if both strings are found.

Regards
aGerman

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: find command not working as expected

#10 Post by aGerman » 23 May 2012 16:22

If your text editor replaces tabs automatically then you could use notepad to include the tab characters into your code. The tab key causes tab characters in notepad.

Regards
aGerman

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: find command not working as expected

#11 Post by MKANET » 23 May 2012 16:25

It worked brilliantly.. Thanks aGerman!!

Post Reply