Home All Groups Group Topic Archive Search About

How do implement this wildcard?

Author
23 Apr 2009 9:38 AM
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

Cheers

ss.

Author
23 Apr 2009 9:47 AM
Pegasus [MVP]
"Synapse Syndrome [KGB]" <synapse@NOSPAMsyndrome.me.uk> wrote in message
news:u6qDJd$wJHA.1300@TK2MSFTNGP05.phx.gbl...
>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
Author
23 Apr 2009 11:08 AM
Synapse Syndrome [KGB]
Pegasus [MVP] <n***@microsoft.com> wrote:
>
>> 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

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.
Author
23 Apr 2009 11:49 AM
Pegasus [MVP]
Show quote Hide quote
"Synapse Syndrome [KGB]" <synapse@NOSPAMsyndrome.me.uk> wrote in message
news:OICNLPAxJHA.5516@TK2MSFTNGP02.phx.gbl...
> Pegasus [MVP] <n***@microsoft.com> wrote:
>>
>>> 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
>
> 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.
Author
23 Apr 2009 11:57 PM
Al Dunbar
Show quote Hide quote
"Pegasus [MVP]" <n***@microsoft.com> wrote in message
news:OIR5DmAxJHA.3832@TK2MSFTNGP02.phx.gbl...
>
> "Synapse Syndrome [KGB]" <synapse@NOSPAMsyndrome.me.uk> wrote in message
> news:OICNLPAxJHA.5516@TK2MSFTNGP02.phx.gbl...
>> Pegasus [MVP] <n***@microsoft.com> wrote:
>>>
>>>> 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
>>
>> 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
Author
27 Apr 2009 2:40 PM
Synapse Syndrome [KGB]
Al Dunbar <aland***@hotmail.com> wrote:
>
> 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.

> 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 "\\".

The FOR command is the one thing that I have not ever been able to
understand however many times I try.

ss.
Author
27 Apr 2009 5:23 PM
Pegasus [MVP]
"Synapse Syndrome [KGB]" <synapse@NOSPAMsyndrome.me.uk> wrote in message
news:u3Xo7Y0xJHA.6004@TK2MSFTNGP02.phx.gbl...
> 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.
Author
13 Jun 2009 2:27 AM
Synapse Syndrome [KGB]
Pegasus [MVP] <n***@microsoft.com> wrote:
Show quoteHide quote
>
>> 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.
>
> 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.
>

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.
Author
13 Jun 2009 2:42 AM
Synapse Syndrome [KGB]
Synapse Syndrome [KGB] <synapse@NOSPAMsyndrome.me.uk> wrote:
Show quoteHide quote
>>
>>> 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.
>>
>> 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.
>>
>
> 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.
Author
13 Jun 2009 6:00 AM
Pegasus [MVP]
Show quote Hide quote
"Synapse Syndrome [KGB]" <synapse@NOSPAMsyndrome.me.uk> wrote in message
news:eTK9pC96JHA.1564@TK2MSFTNGP06.phx.gbl...
> Synapse Syndrome [KGB] <synapse@NOSPAMsyndrome.me.uk> wrote:
>>>
>>>> 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.
>>>
>>> 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.
>>>
>>
>> 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.

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.
Author
27 Apr 2009 2:38 PM
Synapse Syndrome [KGB]
Pegasus [MVP] <n***@microsoft.com> wrote:
Show quoteHide quote
>>>
>>>> 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
>>
>> 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

> 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

> 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.
Author
27 Apr 2009 3:05 PM
Pegasus [MVP]
"Synapse Syndrome [KGB]" <synapse@NOSPAMsyndrome.me.uk> wrote in message
news:e9MiGY0xJHA.1516@TK2MSFTNGP06.phx.gbl...
<snip>
> 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

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.
Author
13 Jun 2009 2:23 AM
Synapse Syndrome [KGB]
Pegasus [MVP] <n***@microsoft.com> wrote:
Show quoteHide quote
> <snip>
>> 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
>
> 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.

A belated thanks.  I understand all that now.

ss.