Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to compare a string that needs to be a case-sensitive compare in a DOS batch script. I cannot figure out how to turn ON the case sensitivity in a IF statement. Here is what I am doing and it is also matching on a lower case "-f", which I am trying to avoid:

SET "ARGS=%*"
IF "%ARGS:-F=%" NEQ "%ARGS%" (
  ECHO Pro option -F was found and not allowed.
  GOTO :ERROR
)
share|improve this question
1  
The DOS IF statement is case-sensitive by default; it should only be case-insensitive with the /I switch. Have you echoed the values of "!%ARGS%:-F=!" and "!%ARGS%!" before your IF statement to see what they hold? – LittleBobbyTables Jun 10 at 19:47
That sounds odd, as EQ and NEQ (and ==) are case-sensitive by default, unless the "/i" flag is specified. – Gabriele Giuseppini Jun 10 at 19:50
More specifically, the error I was getting was "character ( was unexpected" or a message similar to that. This message was resolved by replacing 'NEQ' with the 'NOT ==' method. – djangofan Jun 11 at 16:12

3 Answers

up vote 4 down vote accepted

Your IF statement is properly performing a case sensitive search. The problem is your expansion search and replace is corrupted, as Endoro has pointed out, and the correct form "!ARGS:-F=!" is case insensitive. While expanding ARGS, it first looks for -F, disregarding case, and replaces it with nothing.

Unfortunately, that is how search and replace works. There is no method to make it case sensitive.

You could use the following to do a case sensitive test:

echo(!ARGS!|find "-F" >nul && echo Pro option -F was found.
share|improve this answer
This is new for me, thank you! – Endoro Jun 10 at 21:42
I created a unit test that proves this answer. Thanks. gist.github.com/djangofan/5767940 – djangofan 2 days ago
IF "%args:-F=-F%"=="%args%" (ECHO no sign of -f) ELSE (echo -f detected)

Should work for you - it does for me!


Interesting result.

Here's my test routine:

@ECHO OFF
SETLOCAL
CALL :test something -f
CALL :test something -F
CALL :test -f
CALL :test -F
GOTO :eof
:test
ECHO testing %*
SET "args=%*"
IF "%args:-F=-F%"=="%args%" (ECHO no sign of -f) ELSE (echo -f detected)
GOTO :EOF

And results:

testing something -f
-f detected
testing something -F
no sign of -f
testing -f
-f detected
testing -F
no sign of -f

So - works for me. Perhaps it's version-dependent. I'm using W7/64.

share|improve this answer
This answer is not case-sensitive. I tried it. It seems like it should be because the IF doesn't have the /I option, but regardless, the behavior I get is case-insensitive. – djangofan 2 days ago

Please look at my examples and the output (call <script> -F):

@echo off &setlocal 
SET "ARGS=%*"
echo "%ARGS%"
echo "%ARGS:-F=%"

"-F"
""

your version:

@echo off &setlocal enabledelayedexpansion
SET "ARGS=%*"
echo "%ARGS%"
echo "!%ARGS%:-F=!"

"-F"
"-F="
share|improve this answer
2  
I don't need to read a scripting book. This mistake is something anyone could make. Thanks for figuring out the problem for me. – djangofan Jun 10 at 20:25
OK, if you don't want to become a scripting specialist :-) I made an edit ..... – Endoro Jun 10 at 20:29
+1, good catch with the corrupted search and replace syntax, but that does not help solve case insensitive issue. See my answer – dbenham Jun 10 at 21:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.