Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Batch File with a Pipe Sign

Posted on 2004-10-26
21
Medium Priority
?
1,998 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
Comment
Question by:tgatif
  • 6
  • 6
  • 4
  • +1
21 Comments
 
LVL 30

Expert Comment

by:SteveGTR
ID: 12413699
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
ID: 12413768
@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
ID: 12413797
>"I cannot modify the value before passing it to the batch file."
Why not?
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 9

Expert Comment

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

Expert Comment

by:brianadkins
ID: 12413827

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:
SteveGTR earned 1000 total points
ID: 12413835
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
ID: 12413849
Nice, Brian. :)
[r.D]
0
 

Author Comment

by:tgatif
ID: 12413862
@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
ID: 12413889
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
ID: 12413915
@ 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
ID: 12413919
Ah, actually, this may work:

set var="%1"

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

:)
[r.D]
0
 
LVL 30

Assisted Solution

by:SteveGTR
SteveGTR earned 1000 total points
ID: 12413953
You can right a C routine to escape the value.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 12413958
right = write
0
 
LVL 30

Assisted Solution

by:SteveGTR
SteveGTR earned 1000 total points
ID: 12414118
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
ID: 12414841

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
ID: 12414880

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

0
 
LVL 5

Expert Comment

by:brianadkins
ID: 12415014

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
ID: 12628649
My suggestions offer a number of solutions from the batch file to the C program.
0
 

Author Comment

by:tgatif
ID: 12630086
@SteveGTR

Thanks for the help
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month11 days, 17 hours left to enroll

916 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