Replace string with enviroment variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Lins
Posts: 2
Joined: 14 Nov 2008 05:57

Replace string with enviroment variable

#1 Post by Lins » 14 Nov 2008 06:14

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 ?

Odder93
Posts: 11
Joined: 15 Jul 2008 12:43
Contact:

Re: Replace string with enviroment variable

#2 Post by Odder93 » 18 Nov 2008 04:13

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

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#3 Post by DosItHelp » 23 Nov 2008 01:10

Odder93,

Try:

Code: Select all

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

DosItHelp? :wink:

Lins
Posts: 2
Joined: 14 Nov 2008 05:57

Re: Replace string with enviroment variable

#4 Post by Lins » 24 Nov 2008 05:43

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%%

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#5 Post by DosItHelp » 24 Nov 2008 14:10

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:

Post Reply