How do I check the last 3 characters of a passed parameter in a batch file?

I have a batch file that gets passed a parameter.  The paramater ends up being the subject for an e-mail.  I want to check the parameters last three characters for "- 0" and if it is true exit the batch file before the mail is sent or do something else.  I have tried so many ways and just can't get it to work.
ChrisWibleAsked:
Who is Participating?
 
Steve KnightConnect With a Mentor IT ConsultancyCommented:
Ok then instead of %* use %~1
0
 
Steve KnightIT ConsultancyCommented:
Try this:

set x=%1
echo %x:~-3%

If you have a look at set /?,  you can see various such ioptions.

Steve
0
 
Steve KnightIT ConsultancyCommented:
Sorry, with the check for those chars:
set x=%1
set x=%x:~-3%
if "%x%"=="- 0" goto :eof
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Steve KnightIT ConsultancyCommented:
Doh %1 will never return a space in it though (unless you are adding some quotes etc.) so harvesting the parmater with %*:

@Echo off
REM Get whole command line into x
set x=%*
REM get last three characters of x into y
set y=%x:~-3%
REM Check if y ends in - 0 and exit
if "%y%"=="- 0" goto :eof
REM It doesn't if it gets here
echo There is no -0 on the line
0
 
ChrisWibleAuthor Commented:
I get

 0" goto :eof was unexpected at this time.

when I supply a string with the "- 0" at the end.  What do you think?

PS: The param is supplied in quotes.
0
 
ChrisWibleAuthor Commented:
Thanks, I gathered that, I was just answering you "question" about whether the param was in quotes or not.  What about the error?

 0" goto :eof was unexpected at this time

Any idea?
0
 
ChrisWibleAuthor Commented:
It works.  I added the ~.  Missed what you were saying.  Thanks for your help!
0
 
ChrisWibleAuthor Commented:
Good job!
0
 
Steve KnightIT ConsultancyCommented:
No problem :-)  The ~ strips of the quotes as you see.

Steve
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.