Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-03
9
Medium Priority
?
181 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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 500 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

719 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