Page 1 of 1

trouble with pipe character

Posted: 14 May 2011 11:17
by prinses999
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

Re: trouble with pipe character

Posted: 14 May 2011 13:03
by orange_batch
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:"=!

Re: trouble with pipe character

Posted: 14 May 2011 13:52
by prinses999
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!