Solved

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

Posted on 2008-10-03
9
176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 

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

How our DevOps Teams Maximize Uptime

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

Question has a verified solution.

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

Suggested Solutions

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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