trouble with pipe character

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
prinses999
Posts: 2
Joined: 14 May 2011 10:55

trouble with pipe character

#1 Post by prinses999 » 14 May 2011 11:17

Hi,

I am trying to get this next line (literally) inside a textfile:

select 'alter user '||username||' identified by values '''||password||''';' from dba_users;

The trouble is with the pipe character, it cuts of there with everything I try. I am not putting my script here (it does not work),
so please give me your ideas. I'm going nuts about it!

Thanx,
Judith

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: trouble with pipe character

#2 Post by orange_batch » 14 May 2011 13:03

The pipe character is used for redirection in DOS. Putting most of these characters between quotation marks will spare them from DOS's interpretation. Alternately, you could try escaping these characters but I don't think it works as well. Delayed expansion also helps with this, when you want to echo the text without quotations for example.

I'm not sure exactly what your trouble is, because I tested this even without quotations and it works fine, but...

textfile.txt

Code: Select all

select 'alter user '||username||' identified by values '''||password||''';' from dba_users;

command line:

Code: Select all

for /f "delims=" %%a in (textfile.txt) do set "myvar=%%a"

or, a trick:

Code: Select all

set /p myvar=<textfile.txt



Example, working with quotations...

Does not work:

Code: Select all

set myvar=^^^&&&|||

Works:

Code: Select all

set "myvar=^^^&&&|||"
echo:"%myvar%"

setlocal enabledelayedexpansion
echo:!myvar!

Also works:

Code: Select all

set myvar="^^^&&&|||"
echo:%myvar%

setlocal enabledelayedexpansion
echo:!myvar:"=!

prinses999
Posts: 2
Joined: 14 May 2011 10:55

Re: trouble with pipe character

#3 Post by prinses999 » 14 May 2011 13:52

Thanx, it works now. I did not use delayedexpansion, and put only the string itself into quotes, so that gave me trouble with the pipe character. With delayedexpansion, and putting the whole "str=..." inside quotes, it works like a charm:

@echo off
cls
setlocal enabledelayedexpansion
set "str=select 'alter user '||username||' identified by values '''||password||''';' from dba_users;"
echo !str!>c:\file.txt"
echo.-------[ output ]-------
type c:\file.txt
echo.------------------------
del c:\file.txt

Thanx a lot!

Post Reply