locked
Wildcards on variables in cmd batch file RRS feed

  • Question

  • I have the following script that will be set as a logoff script. Where variable username can be CI_mike, CI_sasusan or CI_saJones etc..

    SET var=%username%

    IF "%var%"=="CI_Mike" (
       msiexec /f 1234.msi
    )

    :end

    So I want to modify the following logic where the variable evaluates ci_%

    SET var=%username%

    IF "%var%"=="CI_%" (
       msiexec /f 1234.msi
    )

    :end

    Let me know if it's unclear.

    Monday, September 21, 2015 10:04 PM

Answers

  • "In your second sample the string CI_% makes no sense. Are you saying that %UserName% includes a % character?"

    Sorry for the confusion. This is where I am confused, I am trying to place a wildcard after CI_ in the even that the person is CI_susan or CI_Mike as an example.

    Wildcards are usually denoted with *, not with %. In most cases they are used with files or folders. You cannot use them in a batch file to compare strings. This means that you need to think differently. You want the comparison to be true if %UserName% starts with CI_. This code will do it:

    if  /i  "%UserName:~0,3%"=="CI_" msiexec /f 1234.msi

    • Marked as answer by Goku777 Tuesday, September 22, 2015 3:28 PM
    Tuesday, September 22, 2015 12:09 PM

All replies

  • Your first code sample seems convoluted:
    SET var=%username%
    IF "%var%"=="CI_Mike" (
    msiexec /f 1234.msi
    )

    This version does exactly the same but is more robust, simpler and easier to understand :
    if  /i  "%UserName%"=="CI_Mike" msiexec /f 1234.msi

    In your second sample the string CI_% makes no sense. Are you saying that %UserName% includes a % character?

    Tuesday, September 22, 2015 7:51 AM
  • "In your second sample the string CI_% makes no sense. Are you saying that %UserName% includes a % character?"

    Sorry for the confusion. This is where I am confused, I am trying to place a wildcard after CI_ in the even that the person is CI_susan or CI_Mike as an example.

    • Proposed as answer by Frederik Long Tuesday, September 22, 2015 12:05 PM
    • Unproposed as answer by Frederik Long Tuesday, September 22, 2015 12:05 PM
    Tuesday, September 22, 2015 11:45 AM
  • "In your second sample the string CI_% makes no sense. Are you saying that %UserName% includes a % character?"

    Sorry for the confusion. This is where I am confused, I am trying to place a wildcard after CI_ in the even that the person is CI_susan or CI_Mike as an example.

    Wildcards are usually denoted with *, not with %. In most cases they are used with files or folders. You cannot use them in a batch file to compare strings. This means that you need to think differently. You want the comparison to be true if %UserName% starts with CI_. This code will do it:

    if  /i  "%UserName:~0,3%"=="CI_" msiexec /f 1234.msi

    • Marked as answer by Goku777 Tuesday, September 22, 2015 3:28 PM
    Tuesday, September 22, 2015 12:09 PM
  • You're a good man!!! Thanks again!
    Tuesday, September 22, 2015 3:28 PM
  • I met a similar problem.

    The following is  the part of my input and output in CMD of Window10.

    INPUT:C:\Users     python babynames.py --summaryfile  baby*.html

    OUTPUT: OSError : [Errno 22] Invalid argument: 'baby*.html'

    So what should I do  to make it work.

    Tuesday, December 27, 2016 3:47 PM