Make sure a variable starts with a specific character

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tbharber
Posts: 32
Joined: 17 Sep 2010 17:08

Make sure a variable starts with a specific character

#1 Post by tbharber » 17 Sep 2010 17:20

To simplify what I am trying to do is prompt the user for the name of a computer on our network. All of our computers will start with the letter n, be an IP Address, or start with a \ (as in \\192.168.1.2). This means the variable must start with a 'n' a number, or a '\'.

Here is the code I am working with. The part I can't figure out is the if statements...

:start
set /p compname=Enter the IP Address or computer name:

IF %compname% starts with a 'n' goto nextstep
IF %compname% starts with a number goto nextstep
IF %compname% starts with a '\' goto nextstep

IF %compname% does not match one of those go back to start

:nextstep
REM Cool stuff happens here...



If anyone has any suggestions it would help out so much!

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Make sure a variable starts with a specific character

#2 Post by ghostmachine4 » 17 Sep 2010 18:14

findstr /?

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Make sure a variable starts with a specific character

#3 Post by orange_batch » 17 Sep 2010 18:33

There could be a "better" way to do this, but for your purposes...

Code: Select all

:start

:: If this line isn't surrounded in quotes, entering command characters will crash the script.
set "/p compname=Enter the IP Address or computer name:"

:: Sets variable to the first character of it's value.
set "compname=%compname:~0,1%"

if "%compname%"=="n" goto nextstep
for /l %%x in (0,1,9) do if "%compname%"=="%%x" goto nextstep
if "%compname%"=="\" goto nextstep

:: If no match occurs...
goto start

:nextstep
:: Chikka bowow.


Imagine you have: set var="Hello World"

These two formats allow you to manipulate variables:

Variables are 0-indexed.

Extract characters from position from start to number of characters: %var:~1,5%
Result: Hello

Extract characters from position from end to number of characters: %var:~-12,5%
Result: Hello

Extract characters from position from start to position from end: %var:~7,-1%
Result: World

Extract characters from position from end to position from end: %var:~-6,-1%
Result: World

Extract all characters after position from start: %var:~7%
Result: World"

Extract all characters after position from end: %var:~-6%
Result: World"

Replace all substring X with string Y, case-insensitive search: %var:X=Y%

Example replaces o with X: %var:O=X%
Result: "HellX WXrld"

Example removes the text "hello": %var:hello=%
Result: " World"

Example replaces the text "hello": %var:hello=Crazy%
Result: "Crazy World"

Replace % with ! where desired if delayed expansion is enabled.

tbharber
Posts: 32
Joined: 17 Sep 2010 17:08

Re: Make sure a variable starts with a specific character

#4 Post by tbharber » 17 Sep 2010 21:00

Wow orange_batch you nailed it thanks so much. Also thanks for pointing out the issue with the set command and quotes. I have noticed in the past just hitting enter at the prompt causes the script to crash so my workaround has alway been to give the variable a result beforehand (ex. compname=x). Maybe this tip will save me some headaches in the future.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Make sure a variable starts with a specific character

#5 Post by orange_batch » 17 Sep 2010 21:45

Yeah, I programmed batch for a month or two before I realized how the interpreter works with quotation marks. It was not made very clear by Microsoft or any tutorial, and due to MS-DOS being pretty shaky regardless, I thought it was just how it was. Also I find it only really solves people's problems if you help explain things in a way that's easy to understand. At least, it should keep them from coming back as often, using less of people's time and be for the better good anyways.

Every time the command interpreter encounters a quotation mark character ", it toggles it's interpretation of characters that follow, between command interpreted and plain text string.

So...

set var=<>|

would fail, but

set "var=<>|"

wouldn't. Keep in mind that this applies universally.

set var="My Text <>|"

set "var2=%var%"

This is bad because it's seen as:

set "var2="My Text <>|""

This is infinitely better than juggling the caret character ^ to escape command characters. It's not fail-proof though, you need to be mindful, nor does it make the escape caret ^ useless. It still needs to be used in certain situations.

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Make sure a variable starts with a specific character

#6 Post by jeb » 18 Sep 2010 02:41

And there is also a difference in the behaviour of
set var="Content"
vs.
set "var=Content"

The second case is an other syntax style, only allowed with ENABLEEXTENSIONS.

A simple test shows

Code: Select all

set xx1="eins zwei" drei
set "xx2=eins zwei" drei
set xx

Output:
xx1="eins zwei" drei
xx2=eins zwei

It's because the second syntax starts the content from the equal sign until the end of the line or to the last quotation mark.
The first style use the complete content of the line, this can be annoying if you have "hidden" spaces at the end.
Like here
set var=%1<space><space><space>

And like orange_batch say.
The quotes can be used to escape special characters, but it does not work for the %, because it is always expanded before the quotes or carets work.

Post Reply