Solved

Batch File with a Pipe Sign

Posted on 2004-10-26
1,799 Views
Last Modified: 2012-08-13
SCENARIO:
I have one batch file that takes the input parameters.  Afterwards, it executes an external windows application with the help of parameters.

%1 %2 %3 %4
@echo off
REM the command line arguments
set user=%1
set env=%2
set objectID=%3
set objectType=%4

set dbUser=USER
set dbPassword=PWD
set dbDatasource=DS

REM "C:\windowapp.exe" -w%dbUser% -x%dbPassword% -y%dbDatasource% -u%user% -e%env% -m%objectID% -n%objectType%

PROBLEM:
One of the input parameters has a pipe sign in it.  In this way, the batch file does not take the full parameter.  It takes the value before the pipe sign and assign it to the variable.  Pipe sign has meanings in batch file.

QUESTION:
How to workaround this problem?
0
Question by:tgatif
    20 Comments
     
    LVL 30

    Expert Comment

    by:SteveGTR
    You should escape it and wrap it in double quotes. Then in your batch file remove the double quotes. Example:

    Create test.bat as follows:

    @echo Parm: %~1

    Run test.bat like this:

    test "test^|test"

    test.bat will echo test|test.

    The %~1 removes the double quotes from the 1st parameter. The ^ escapes the pipe sign.

    Good Luck,
    Steve
    0
     

    Author Comment

    by:tgatif
    @SteveGTR

    The problem here is that I cannot play with the parameter value.  I cannot modify the value before passing it to the batch file.
    0
     
    LVL 9

    Expert Comment

    by:DrWarezz
    >"I cannot modify the value before passing it to the batch file."
    Why not?
    0
     
    LVL 9

    Expert Comment

    by:DrWarezz
    Aren't you manually typing in the parameters?
    0
     
    LVL 5

    Expert Comment

    by:brianadkins

    Not sure, but maybe this will spark an idea:

    From a command prompt, I tried this....


    // just gets a variable to have a pipe in it)
    C:\>set A=1^|b

    // this does not work
    C:\>echo %A%
    'b' is not recognized as an internal or external command,
    operable program or batch file.

    // This will echo the correct value by replacing the pipe with an escaped pipe before echoing
    C:\>echo %A:|=^|%
    1|b


    -Brian

    0
     
    LVL 30

    Accepted Solution

    by:
    How is your batch file being called? You have no control over that part of the processing? The only way you could pass a special character (|,&,<,>) is to escape it. Do you have an example of how your batch file is being called?
    0
     
    LVL 9

    Expert Comment

    by:DrWarezz
    Nice, Brian. :)
    [r.D]
    0
     

    Author Comment

    by:tgatif
    @DrWarezz

    The values that are being passed to the batch file are out of my control.  They are coming from another system.
    0
     
    LVL 9

    Expert Comment

    by:DrWarezz
    Oh.

    Problem is, I don't *think* that you can extract the pipe from it JUST using DOS :o\  (Unless Steve can work his magic).

    I think you may need to refer to some VBscript, or something....?

    [r.D]
    0
     

    Author Comment

    by:tgatif
    @ SteveGTR

    I am calling the file through C function:

        sprintf ( szCommand, "%s %s %s %s %s",
         evt_envParamValue_GS5A ,
                      lpDS->szUserId,
           lpDS->szEnvironment,
           lpDS->szNameObject,
           lpDS->szTypeObject
           );
      stat = system(szCommand);
    0
     
    LVL 9

    Expert Comment

    by:DrWarezz
    Ah, actually, this may work:

    set var="%1"

    set var2=%var:|=^|%
    set final=%var2:"=%

    :)
    [r.D]
    0
     
    LVL 30

    Assisted Solution

    by:SteveGTR
    You can right a C routine to escape the value.
    0
     
    LVL 30

    Expert Comment

    by:SteveGTR
    right = write
    0
     
    LVL 30

    Assisted Solution

    by:SteveGTR
    The code could be changed to something like this. Didn't compile or test it, but I've done it before and the technique works.

    char* EscapeParm(char* strIn)
    {
      int len;
      char* strOut; = malloc(len + 256);
      int i;

      len = strlen(strIn);
      // Cheat here and add in fudge factor for escaped characters. Should be more than enough
      strOut = malloc(len + 256);
      i = 0;

      while (*strIn)
        {
        if (*strIn == '|' || *strIn == '&' || *strIn == '<' || *strIn == '>')
          strOut[i++] = '^';

        strOut[i++] = *strIn;
        strIn++;
        }  
     
     strOut[i] = '\0';

     return strOut;
    }

    ...

    char* userParm = EscapeParm(lpDS->szUserId);
    char* envParm = EscapeParm(lpDS->szEnvironment);
    char* nameParm = EscapeParm(lpDS->szNameObject);
    char* typeParm = EscapeParm(lpDS->szTypeObject);

    sprintf ( szCommand, "%s \"%s\" \"%s\" \"%s\" \"%s\"",
      evt_envParamValue_GS5A,  userParm, envParm, nameParm, typeParm);

    free(typeParm);
    free(nameParm);
    free(envParm);
    free(userParm);

    stat = system(szCommand);
    0
     
    LVL 5

    Expert Comment

    by:brianadkins

    This seems to work.... (building on earlier posts)

    set var="%1"
    set var=%test:|=^^^|%
    set var=%test:"=%
    echo %var%

    -Brian
    0
     
    LVL 5

    Expert Comment

    by:brianadkins

    Byt he way.... my previous post comes from this question I asked a year ago... and SteveGTR answered for me...

    http://oldlook.experts-exchange.com:8080/Operating_Systems/MSDOS/Q_20800744.html

    -Brian
    0
     
    LVL 9

    Expert Comment

    by:DrWarezz
    Brian -- that doesn't seem to work for me  ?

    0
     
    LVL 5

    Expert Comment

    by:brianadkins

    I swear it worked here a minute ago... but not now...  how about this...

    -----------------------------------8<---------------------
    call :CLEANFIELD "%1"
    set var=%cleanfield:~1,-1%
    echo %var%
     
    goto EXIT
    :CLEANFIELD
    set cleanfield=%1
    set cleanfield=%cleanfield:^=^^^^%
    set cleanfield=%cleanfield:&=^^^&%
    set cleanfield=%cleanfield:,=^^^,%
    set cleanfield=%cleanfield:\=^^^\%
    set cleanfield=%cleanfield:|=^^^|%
    set cleanfield=%cleanfield:<=^^^<%
    set cleanfield=%cleanfield:>=^^^>%

    :EXIT
    -----------------------------------8<---------------------

    I think this is the key from link in previous post:
    It appears that you have to have an extra carat '^' for every time that variable gets used in a set statement because the excaping character (^) gets 'eaten up' in the command.
    0
     
    LVL 30

    Expert Comment

    by:SteveGTR
    My suggestions offer a number of solutions from the batch file to the C program.
    0
     

    Author Comment

    by:tgatif
    @SteveGTR

    Thanks for the help
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Course: MongoDB Object-Document Mapper for NodeJS

    NodeJS (JavaScript on the server) is awesome, but some developers get confused about NoSQL when it comes to working in Node with MongoDB (NoSQL database). Do you need a better explanation of how to use Node.js with MongoDB? The most popular choice is the Mongoose library.

    One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
    VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…
    Want to pick and choose which updates you receive? Feel free to check out this quick video on how to manage your email notifications.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now