Discussion:
How do implement this wildcard?
(too old to reply)
Synapse Syndrome [KGB]
2009-04-23 09:38:37 UTC
Permalink
I want a .cmd script to check that %1 is a UNC server name and goto
something else.

You can probably see what I want to do, so how do I do it correctly?

if [%1] == [\\*] goto:UNC

Cheers

ss.
Pegasus [MVP]
2009-04-23 09:47:58 UTC
Permalink
Post by Synapse Syndrome [KGB]
I want a .cmd script to check that %1 is a UNC server name and goto
something else.
You can probably see what I want to do, so how do I do it correctly?
if [%1] == [\\*] goto:UNC
Here you go:
@echo off
set parm=%1x
if [%parm:~0,2%]==[\\] echo UNC
Synapse Syndrome [KGB]
2009-04-23 11:08:10 UTC
Permalink
Post by Pegasus [MVP]
Post by Synapse Syndrome [KGB]
I want a .cmd script to check that %1 is a UNC server name and goto
something else.
You can probably see what I want to do, so how do I do it correctly?
if [%1] == [\\*] goto:UNC
@echo off
set parm=%1x
if [%parm:~0,2%]==[\\] echo UNC
Ah, thanks a lot Pegasus. Got it working now, but I do not really know what
that does. Like what is that x supposed to mean? I have read about this
method of spoofing wildcards, by making environmental variables, before, but
it was not explained in any way that I could understand. Have you got any
link that explain this?

The temporary variables disappear once that CMD instance is closed, right?
Or is there a way to clean them up at the end of the script?

Cheers

ss.
Pegasus [MVP]
2009-04-23 11:49:18 UTC
Permalink
Post by Synapse Syndrome [KGB]
Post by Pegasus [MVP]
Post by Synapse Syndrome [KGB]
I want a .cmd script to check that %1 is a UNC server name and goto
something else.
You can probably see what I want to do, so how do I do it correctly?
if [%1] == [\\*] goto:UNC
@echo off
set parm=%1x
if [%parm:~0,2%]==[\\] echo UNC
Ah, thanks a lot Pegasus. Got it working now, but I do not really know
what that does. Like what is that x supposed to mean? I have read about
this method of spoofing wildcards, by making environmental variables,
before, but it was not explained in any way that I could understand. Have
you got any link that explain this?
The temporary variables disappear once that CMD instance is closed, right?
Or is there a way to clean them up at the end of the script?
The "x" makes the script robust so that it does not fail in the line below
in case you invoke it without a parameter. Any character or string would do,
e.g. set parm=%1Synapse

My script does not really "spoof" wildcards - it merely uses the substring
function available at the console. Since the substring function only works
for environmental variables (at least as far as I know), the script must
assign %1 to an environmental variable.

Every process, whether it is a Command Processor or some other executable,
inherits its environmental variables from the parent that invokes it. When
that process closes then all variables are lost. You need to execute a
special command if you wish to preserve a variable and make it available for
other processes.
Al Dunbar
2009-04-23 23:57:15 UTC
Permalink
Post by Pegasus [MVP]
Post by Synapse Syndrome [KGB]
Post by Pegasus [MVP]
Post by Synapse Syndrome [KGB]
I want a .cmd script to check that %1 is a UNC server name and goto
something else.
You can probably see what I want to do, so how do I do it correctly?
if [%1] == [\\*] goto:UNC
@echo off
set parm=%1x
if [%parm:~0,2%]==[\\] echo UNC
Ah, thanks a lot Pegasus. Got it working now, but I do not really know
what that does. Like what is that x supposed to mean? I have read about
this method of spoofing wildcards, by making environmental variables,
before, but it was not explained in any way that I could understand.
Have you got any link that explain this?
The temporary variables disappear once that CMD instance is closed,
right? Or is there a way to clean them up at the end of the script?
The "x" makes the script robust so that it does not fail in the line below
in case you invoke it without a parameter. Any character or string would
do, e.g. set parm=%1Synapse
My script does not really "spoof" wildcards - it merely uses the substring
function available at the console. Since the substring function only works
for environmental variables (at least as far as I know), the script must
assign %1 to an environmental variable.
Every process, whether it is a Command Processor or some other executable,
inherits its environmental variables from the parent that invokes it. When
that process closes then all variables are lost. You need to execute a
special command if you wish to preserve a variable and make it available
for other processes.
The script determines whether or not the parameter is a UNC, but it is not
necessarily a "UNC server name", as originally requested. Whether a UNC
string is completely valid as in \\server\share or
\\server\share\path\file.ext or partly valid as in \\server would be
significantly more difficult to determine using batch alone. That said, the
specific requirements might not require a completely rigorous solution.

Another technique that might be useful here applies to batch script
parameters andFOR loop variables. For example the output from this
statement:

for %%F in (C:\whatever.txt x.y \\server) do echo/[%%~dF]

should be:

[C:]
[C:]
[\\]

In otherwords, the "drive" component of a UNC is the leading "\\".

/Al
Synapse Syndrome [KGB]
2009-04-27 14:40:04 UTC
Permalink
Post by Al Dunbar
The script determines whether or not the parameter is a UNC, but it is
not necessarily a "UNC server name", as originally requested. Whether a
UNC string is completely valid as in \\server\share or
\\server\share\path\file.ext or partly valid as in \\server would be
significantly more difficult to determine using batch alone. That said,
the specific requirements might not require a completely rigorous
solution.
Yes, it's fine for my needs.
Post by Al Dunbar
Another technique that might be useful here applies to batch script
parameters andFOR loop variables. For example the output from this
for %%F in (C:\whatever.txt x.y \\server) do echo/[%%~dF]
[C:]
[C:]
[\\]
In otherwords, the "drive" component of a UNC is the leading "\\".
The FOR command is the one thing that I have not ever been able to
understand however many times I try.

ss.
Pegasus [MVP]
2009-04-27 17:23:35 UTC
Permalink
Post by Synapse Syndrome [KGB]
The FOR command is the one thing that I have not ever been able to
understand however many times I try.
ss.
There are several flavours for the "for" command. Let me show you some of
them. They all work from the Command Prompt.

for %a in (Synaps Syndrome [KGB]) do @echo %a
Here the "for" command looks at each item inside the brackes and assigns it
to the variable %a. I then chose to echo that variable to the console
screen.

for /L %a in (10, 1, 20) do @echo %a
Here I use the /L switch so that the "for" command works as a counter. It
will assign values from 10 to 20 to the variable %a.

for %a in (c:\windows\*.*) do @echo %a
Here we have a whole collection of files inside the bracket. The variable %a
will be set to the name of each of them in turn, one at a time.

This is just scratching the surface. I suggest you play with the above
commands, then move on to more demanding variations. You can see all of them
when you type for /? at the Command Prompt.
Synapse Syndrome [KGB]
2009-06-13 02:42:56 UTC
Permalink
Post by Pegasus [MVP]
Post by Synapse Syndrome [KGB]
The FOR command is the one thing that I have not ever been able to
understand however many times I try.
There are several flavours for the "for" command. Let me show you some
of them. They all work from the Command Prompt.
Here the "for" command looks at each item inside the brackes and assigns
it to the variable %a. I then chose to echo that variable to the
console screen.
Here I use the /L switch so that the "for" command works as a counter.
It will assign values from 10 to 20 to the variable %a.
Here we have a whole collection of files inside the bracket. The
variable %a will be set to the name of each of them in turn, one at a
time.
This is just scratching the surface. I suggest you play with the above
commands, then move on to more demanding variations. You can see all of
them when you type for /? at the Command Prompt.
Thanks. I have tried returning to this matter a few times over the last
few weeks, but I have not got that much further than your examples. :/
Are there any other ways of defining the set? I would find it very
helpful if I could make a set out of a list in a text file.
I have even been using this in batch files, to capitalise parameters, but I
have no idea how it works:

set parm=%1
for %%U in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
call set "parm=%%parm:%%U=%%U%%"
)

ss.
Pegasus [MVP]
2009-06-13 06:00:04 UTC
Permalink
Post by Synapse Syndrome [KGB]
Post by Pegasus [MVP]
Post by Synapse Syndrome [KGB]
The FOR command is the one thing that I have not ever been able to
understand however many times I try.
There are several flavours for the "for" command. Let me show you some
of them. They all work from the Command Prompt.
Here the "for" command looks at each item inside the brackes and assigns
it to the variable %a. I then chose to echo that variable to the
console screen.
Here I use the /L switch so that the "for" command works as a counter.
It will assign values from 10 to 20 to the variable %a.
Here we have a whole collection of files inside the bracket. The
variable %a will be set to the name of each of them in turn, one at a
time.
This is just scratching the surface. I suggest you play with the above
commands, then move on to more demanding variations. You can see all of
them when you type for /? at the Command Prompt.
Thanks. I have tried returning to this matter a few times over the last
few weeks, but I have not got that much further than your examples. :/
Are there any other ways of defining the set? I would find it very
helpful if I could make a set out of a list in a text file.
I have even been using this in batch files, to capitalise parameters, but
set parm=%1
for %%U in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
call set "parm=%%parm:%%U=%%U%%"
)
ss.
This is a question that the boys in alt.msdos.batch.nt would love to dig
their teeth into. It uses some convoluted command processor quirk for the
Set command. When I need an upper/lower case function then I prefer to use
the inbuilt UCase/LCase functions of VB Script.

Synapse Syndrome [KGB]
2009-06-13 02:27:08 UTC
Permalink
Post by Pegasus [MVP]
Post by Synapse Syndrome [KGB]
The FOR command is the one thing that I have not ever been able to
understand however many times I try.
There are several flavours for the "for" command. Let me show you some of
them. They all work from the Command Prompt.
Here the "for" command looks at each item inside the brackes and assigns
it to the variable %a. I then chose to echo that variable to the console
screen.
Here I use the /L switch so that the "for" command works as a counter. It
will assign values from 10 to 20 to the variable %a.
Here we have a whole collection of files inside the bracket. The
variable %a will be set to the name of each of them in turn, one at a
time.
This is just scratching the surface. I suggest you play with the above
commands, then move on to more demanding variations. You can see all of
them when you type for /? at the Command Prompt.
Thanks. I have tried returning to this matter a few times over the last few
weeks, but I have not got that much further than your examples. :/

Are there any other ways of defining the set? I would find it very helpful
if I could make a set out of a list in a text file.

ss.
Pegasus [MVP]
2009-04-27 15:05:32 UTC
Permalink
"Synapse Syndrome [KGB]" <***@NOSPAMsyndrome.me.uk> wrote in message news:***@TK2MSFTNGP06.phx.gbl...
<snip>
Thanks, with that keyword 'substring', I found this page which describes
http://www.ss64.com/nt/syntax-substring.html
The concept of "substring" (sometimes called "midstring") is used
extensively in most programming languages. It is often complemented by the
"leftstring" and "rightstring" functions, both of which are available under
the Windows Command Processor. Type for /? to see how it's done. Note that
the syntax for these functions under the Command Processor is unbelievably
cryptic. In most programming languages it is far simpler, e.g.
x = mid(Name, 3, 5)
y = left(Name, 2)
z = right(Name, 9)
I suppose you mean ENDLOCAL, and SETX for permanent changes?
When you run a batch file such as
@echo off
set Name=ss

then the variable %Name% remains set within the current Command Processor.
When you modify this batch file like so:
@echo off
setlocal
set Name=ss
endlocal

then the variable %Name% is lost the moment the batch file ends. In either
case the variable is lost when the current Command Processor is closed. To
prevent this, you can use setx.exe. I recommend you test these concepts in
order to become comfortable with them.
Synapse Syndrome [KGB]
2009-06-13 02:23:44 UTC
Permalink
Post by Pegasus [MVP]
<snip>
Thanks, with that keyword 'substring', I found this page which describes
http://www.ss64.com/nt/syntax-substring.html
The concept of "substring" (sometimes called "midstring") is used
extensively in most programming languages. It is often complemented by
the "leftstring" and "rightstring" functions, both of which are
available under the Windows Command Processor. Type for /? to see how
it's done. Note that the syntax for these functions under the Command
Processor is unbelievably cryptic. In most programming languages it is
far simpler, e.g. x = mid(Name, 3, 5)
y = left(Name, 2)
z = right(Name, 9)
I suppose you mean ENDLOCAL, and SETX for permanent changes?
When you run a batch file such as
@echo off
set Name=ss
then the variable %Name% remains set within the current Command
@echo off
setlocal
set Name=ss
endlocal
then the variable %Name% is lost the moment the batch file ends. In
either case the variable is lost when the current Command Processor is
closed. To prevent this, you can use setx.exe. I recommend you test
these concepts in order to become comfortable with them.
A belated thanks. I understand all that now.

ss.
Synapse Syndrome [KGB]
2009-04-27 14:38:34 UTC
Permalink
Post by Pegasus [MVP]
Post by Synapse Syndrome [KGB]
Post by Pegasus [MVP]
Post by Synapse Syndrome [KGB]
I want a .cmd script to check that %1 is a UNC server name and goto
something else.
You can probably see what I want to do, so how do I do it correctly?
if [%1] == [\\*] goto:UNC
@echo off
set parm=%1x
if [%parm:~0,2%]==[\\] echo UNC
Ah, thanks a lot Pegasus. Got it working now, but I do not really know
what that does. Like what is that x supposed to mean? I have read
about this method of spoofing wildcards, by making environmental
variables, before, but it was not explained in any way that I could
understand. Have you got any link that explain this?
The temporary variables disappear once that CMD instance is closed,
right? Or is there a way to clean them up at the end of the script?
The "x" makes the script robust so that it does not fail in the line
below in case you invoke it without a parameter. Any character or
string would do, e.g. set parm=%1Synapse
I see. I do not need to use the x in this case as the script starts with..

if [%1] == [] goto :help
if [%1] == [/?] goto :help
Post by Pegasus [MVP]
My script does not really "spoof" wildcards - it merely uses the
substring function available at the console. Since the substring
function only works for environmental variables (at least as far as I
know), the script must assign %1 to an environmental variable.
Thanks, with that keyword 'substring', I found this page which describes it
all, and it is a lot easier to work out than I thought previously:

http://www.ss64.com/nt/syntax-substring.html
Post by Pegasus [MVP]
Every process, whether it is a Command Processor or some other
executable, inherits its environmental variables from the parent that
invokes it. When that process closes then all variables are lost. You
need to execute a special command if you wish to preserve a variable
and make it available for other processes.
I suppose you mean ENDLOCAL, and SETX for permanent changes?

Cheers

ss.
Loading...