• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2034
  • Last Modified:

Batch File with a Pipe Sign

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
tgatif
Asked:
tgatif
  • 6
  • 6
  • 4
  • +1
3 Solutions
 
SteveGTRCommented:
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
 
tgatifAuthor Commented:
@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
 
DrWarezzCommented:
>"I cannot modify the value before passing it to the batch file."
Why not?
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
DrWarezzCommented:
Aren't you manually typing in the parameters?
0
 
brianadkinsCommented:

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
 
SteveGTRCommented:
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
 
DrWarezzCommented:
Nice, Brian. :)
[r.D]
0
 
tgatifAuthor Commented:
@DrWarezz

The values that are being passed to the batch file are out of my control.  They are coming from another system.
0
 
DrWarezzCommented:
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
 
tgatifAuthor Commented:
@ 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
 
DrWarezzCommented:
Ah, actually, this may work:

set var="%1"

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

:)
[r.D]
0
 
SteveGTRCommented:
You can right a C routine to escape the value.
0
 
SteveGTRCommented:
right = write
0
 
SteveGTRCommented:
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
 
brianadkinsCommented:

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

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

-Brian
0
 
brianadkinsCommented:

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
 
DrWarezzCommented:
Brian -- that doesn't seem to work for me  ?

0
 
brianadkinsCommented:

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
 
SteveGTRCommented:
My suggestions offer a number of solutions from the batch file to the C program.
0
 
tgatifAuthor Commented:
@SteveGTR

Thanks for the help
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 6
  • 6
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now