Solved

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

Posted on 2008-10-03
9
177 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

626 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