Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > How do implement this wildcard?

Reply
Thread Tools Display Modes

How do implement this wildcard?

 
 
Synapse Syndrome [KGB]
Guest
Posts: n/a

 
      04-23-2009
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.


 
Reply With Quote
 
 
 
 
Pegasus [MVP]
Guest
Posts: n/a

 
      04-23-2009

"Synapse Syndrome [KGB]" <> wrote in message
news:u6qDJd$...
>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


 
Reply With Quote
 
Synapse Syndrome [KGB]
Guest
Posts: n/a

 
      04-23-2009
Pegasus [MVP] <> 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.


 
Reply With Quote
 
Pegasus [MVP]
Guest
Posts: n/a

 
      04-23-2009

"Synapse Syndrome [KGB]" <> wrote in message
news:...
> Pegasus [MVP] <> 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.


 
Reply With Quote
 
Al Dunbar
Guest
Posts: n/a

 
      04-23-2009

"Pegasus [MVP]" <> wrote in message
news:...
>
> "Synapse Syndrome [KGB]" <> wrote in message
> news:...
>> Pegasus [MVP] <> 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


 
Reply With Quote
 
Synapse Syndrome [KGB]
Guest
Posts: n/a

 
      04-27-2009
Pegasus [MVP] <> 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


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.


 
Reply With Quote
 
Synapse Syndrome [KGB]
Guest
Posts: n/a

 
      04-27-2009
Al Dunbar <> 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.


 
Reply With Quote
 
Pegasus [MVP]
Guest
Posts: n/a

 
      04-27-2009

"Synapse Syndrome [KGB]" <> wrote in message
news:...
<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.


 
Reply With Quote
 
Pegasus [MVP]
Guest
Posts: n/a

 
      04-27-2009

"Synapse Syndrome [KGB]" <> wrote in message
news:...
> 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.


 
Reply With Quote
 
Synapse Syndrome [KGB]
Guest
Posts: n/a

 
      06-13-2009
Pegasus [MVP] <> wrote:
> <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.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
wildcard subdomains in MS DNS rvj DNS Server 7 07-28-2008 01:01 PM
Wildcard searches fractalsuar Windows Vista File Management 5 08-23-2006 12:58 PM
MSH: recursive wildcard ** Keith Hill Scripting 2 11-30-2005 04:42 PM
Wildcard search. Matt Carter Active Directory 3 10-07-2005 03:45 PM
Wildcard * James Fly Scripting 1 01-11-2005 05:32 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59