How do implement this wildcard?

Discussion in 'Scripting' started by Synapse Syndrome [KGB], Apr 23, 2009.

  1. 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


    Synapse Syndrome [KGB], Apr 23, 2009
    1. Advertisements

  2. Here you go:
    @echo off
    set parm=%1x
    if [%parm:~0,2%]==[\\] echo UNC
    Pegasus [MVP], Apr 23, 2009
    1. Advertisements

  3. 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?


    Synapse Syndrome [KGB], Apr 23, 2009
  4. 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.
    Pegasus [MVP], Apr 23, 2009
  5. Synapse Syndrome [KGB]

    Al Dunbar Guest

    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

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

    should be:


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

    Al Dunbar, Apr 24, 2009
  6. 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
    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:
    I suppose you mean ENDLOCAL, and SETX for permanent changes?


    Synapse Syndrome [KGB], Apr 27, 2009
  7. Yes, it's fine for my needs.
    The FOR command is the one thing that I have not ever been able to
    understand however many times I try.

    Synapse Syndrome [KGB], Apr 27, 2009
  8. 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)
    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
    set Name=ss

    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.
    Pegasus [MVP], Apr 27, 2009
  9. 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

    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.
    Pegasus [MVP], Apr 27, 2009
  10. A belated thanks. I understand all that now.

    Synapse Syndrome [KGB], Jun 13, 2009
  11. 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.

    Synapse Syndrome [KGB], Jun 13, 2009
  12. 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%%"

    Synapse Syndrome [KGB], Jun 13, 2009
  13. 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.
    Pegasus [MVP], Jun 13, 2009
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.