Solved

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

Posted on 2008-10-03
9
173 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:ChrisWible
  • 5
  • 4
9 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22637280
Try this:

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

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

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22637290
Sorry, with the check for those chars:
set x=%1
set x=%x:~-3%
if "%x%"=="- 0" goto :eof
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22637317
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:ChrisWible
ID: 22637458
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
 
LVL 43

Accepted Solution

by:
Steve Knight earned 125 total points
ID: 22637592
Ok then instead of %* use %~1
0
 

Author Comment

by:ChrisWible
ID: 22637654
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
 

Author Comment

by:ChrisWible
ID: 22637875
It works.  I added the ~.  Missed what you were saying.  Thanks for your help!
0
 

Author Closing Comment

by:ChrisWible
ID: 31502892
Good job!
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22638064
No problem :-)  The ~ strips of the quotes as you see.

Steve
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows Batch File - Display current time and date in FOR command 8 81
file size 9 51
xcopy loop 13 56
Robycopy Copy Data \ Disk to Disk (new\changed data) 18 81
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question