Page 1 of 1

Replace string with enviroment variable

Posted: 14 Nov 2008 06:14
by Lins
Hi to all.

I've this problem, i need to replace a string with enviroment variable on text file.

This is my source code, but i don't know how replace env. variable :

Code: Select all

set DIR=c:\TEMP

SET FILEPATH=e:\home\
SET FILE=e:\home\config.pro
SET FNAME=config.pro
CALL :PROCESS

GOTO :EOF

:PROCESS
pushd %FILEPATH%
FOR /F "tokens=1 delims=" %%F in (%FILE%) DO (
 SET TEXT=%%F
 CALL :PROCTEXT
)

DEL %FILE%
REN newFile.txt %FNAME%
popd

GOTO :EOF

:PROCTEXT
ECHO %TEXT:e:\home\plot=e:\home\plot\%username%% >>newFile.txt


But the line

Code: Select all

ECHO %TEXT:e:\home\plot=e:\home\plot\%username%% >>newFile.txt


is incorrect.

Any suggestion ?

Re: Replace string with enviroment variable

Posted: 18 Nov 2008 04:13
by Odder93
Lins wrote:Hi to all.

I've this problem, i need to replace a string with enviroment variable on text file.

This is my source code, but i don't know how replace env. variable :

Code: Select all

set DIR=c:\TEMP

SET FILEPATH=e:\home\
SET FILE=e:\home\config.pro
SET FNAME=config.pro
CALL :PROCESS

GOTO :EOF

:PROCESS
pushd %FILEPATH%
FOR /F "tokens=1 delims=" %%F in (%FILE%) DO (
 SET TEXT=%%F
 CALL :PROCTEXT
)

DEL %FILE%
REN newFile.txt %FNAME%
popd

GOTO :EOF

:PROCTEXT
ECHO %TEXT:e:\home\plot=e:\home\plot\%username%% >>newFile.txt


But the line

Code: Select all

ECHO %TEXT:e:\home\plot=e:\home\plot\%username%% >>newFile.txt


is incorrect.

Any suggestion ?



I'm not that good with these, but you can't do %1halfcommand%insidecommand%2halfcommand%... instead try:

Code: Select all

ECHO !TEXT:e:\home\plot=e:\home\plot\%username%! >>newFile.txt

Posted: 23 Nov 2008 01:10
by DosItHelp
Odder93,

Try:

Code: Select all

Call ECHO %%TEXT:e:\home\plot=e:\home\plot\%username%%%>>newFile.txt

DosItHelp? :wink:

Re: Replace string with enviroment variable

Posted: 24 Nov 2008 05:43
by Lins
Odder93 wrote:I'm not that good with these, but you can't do %1halfcommand%insidecommand%2halfcommand%... instead try:

Code: Select all

ECHO !TEXT:e:\home\plot=e:\home\plot\%username%! >>newFile.txt


This don't work, it replace all test with the string (and not only c:\home\plot)

DosItHelp wrote:Odder93,

Try:

Code: Select all

Call ECHO %%TEXT:e:\home\plot=e:\home\plot\%username%%%>>newFile.txt

DosItHelp? :wink:


No, this also don't work

Example

Code: Select all

SET TEXT=Prova 123 e:\home\plot 456
ECHO %%TEXT:e:\home\plot=e:\home\plot\%username%%%


Result :

Code: Select all

Prova 123 e:\home\plot\ 456username%%

Posted: 24 Nov 2008 14:10
by DosItHelp
Lins,

Use it within a batch and also use with "CALL".
It doesn't work from the command line for some reason.

Code: Select all

SET TEXT=Prova 123 e:\home\plot 456 
CALL ECHO %%TEXT:e:\home\plot=e:\home\plot\%username%%%

Output:
Prova 123 e:\home\plot\DosItHelp 456

:wink: