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?
tgatifAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.