ReneGe
asked on
Batch File: String manipulation
Hi there,
I need to replace a the word "SAISON" from a text file to the word "PERIOD+".
SCRIPT TO FIX:
@echo off
setlocal enabledelayedexpansion
FOR "delims=" %%A in (%File_LOG_Mirror%) do (
ECHO !%%A:SAISON=PERIOD^+!>>%Fi le_LOG_Mir ror_TEMP%
DEL %File_LOG_Mirror%
REN %File_LOG_Mirror_TEMP% %File_LOG_Mirror%
)
exit
Thanks,
Rene
I need to replace a the word "SAISON" from a text file to the word "PERIOD+".
SCRIPT TO FIX:
@echo off
setlocal enabledelayedexpansion
FOR "delims=" %%A in (%File_LOG_Mirror%) do (
ECHO !%%A:SAISON=PERIOD^+!>>%Fi
DEL %File_LOG_Mirror%
REN %File_LOG_Mirror_TEMP% %File_LOG_Mirror%
)
exit
Thanks,
Rene
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
u mean the file name should be manipulated or the file content?
ASKER
Content. Thanks
ASKER
==> NO. just batch file.
Thanks
Thanks
ASKER
==> developedtester
NO. just batch file.
Thanks
NO. just batch file.
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
for batch file solution check http://www.dostips.com/?t=Batch.FindAndReplace
Looks like you are getting some good help on a scripting approach. But an alternative is to use a small general purpose search and replace type utility, like the one mentioned in this therad. There are a few of these around.
https://www.experts-exchange.com/questions/23185721/Search-and-Replace-text-in-a-text-file-from-batch-file.html
~bp
https://www.experts-exchange.com/questions/23185721/Search-and-Replace-text-in-a-text-file-from-batch-file.html
~bp
If you know there are no funny characters, or blank lines in the file, then this may get the job done:
@echo off
setlocal enabledelayedexpansion
FOR "delims=" %%A in (%File_LOG_Mirror%) do (
set Line=%%A
ECHO !Line:SAISON=PERIOD^+!>>"% File_LOG_M irror%._te mp"
DEL "%File_LOG_Mirror%"
REN "%File_LOG_Mirror%._temp" "%File_LOG_Mirror%"
)
exit
~bp
@echo off
setlocal enabledelayedexpansion
FOR "delims=" %%A in (%File_LOG_Mirror%) do (
set Line=%%A
ECHO !Line:SAISON=PERIOD^+!>>"%
DEL "%File_LOG_Mirror%"
REN "%File_LOG_Mirror%._temp" "%File_LOG_Mirror%"
)
exit
~bp
it's all very higgledy-piggledy...
firstly, as imaginative as it is, you can't do !%%a:abc=xyz!. I note we'd still have to rely on delayed expansion if it were possible. For that reason, %%a needs to be assigned to a variable first. Like this:
set var=%%a
echo !var:abc=xyz!
prew, if that works I'll take my hat off to you. Obviously, blank lines are not preserved.
As for the renaming thing... how about something like this:
BTW, AFAIK, you can't use DELIMS without 'FOR /F'.
Furthermore, because there are no double-quotes around %File_LOG_Mirror% in the FOR statement, I can only assume it contains no spaces. That being the case, the DEL and REN commands don't require them either.
However, to bo on the safe side, I would prefer to use USEBACKQ and double-quotes (See next comment).
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (%File_LOG_Mirror%) do (
set line=%%a
echo !line:SAISON=PERIOD^+!>>%F ile_LOG_Mi rror%.tmp
)
move /y %File_LOG_Mirror%.tmp %File_LOG_Mirror%
firstly, as imaginative as it is, you can't do !%%a:abc=xyz!. I note we'd still have to rely on delayed expansion if it were possible. For that reason, %%a needs to be assigned to a variable first. Like this:
set var=%%a
echo !var:abc=xyz!
prew, if that works I'll take my hat off to you. Obviously, blank lines are not preserved.
As for the renaming thing... how about something like this:
BTW, AFAIK, you can't use DELIMS without 'FOR /F'.
Furthermore, because there are no double-quotes around %File_LOG_Mirror% in the FOR statement, I can only assume it contains no spaces. That being the case, the DEL and REN commands don't require them either.
However, to bo on the safe side, I would prefer to use USEBACKQ and double-quotes (See next comment).
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (%File_LOG_Mirror%) do (
set line=%%a
echo !line:SAISON=PERIOD^+!>>%F
)
move /y %File_LOG_Mirror%.tmp %File_LOG_Mirror%
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Come clean with me bill.... you were inspired by my previous comment! I like that. Ha ha ha...
Just pulling your leg prew...
It's all good.
~bp
~bp
:)
ASKER
*** TO ALL OF YOU GUYS ***
*I first wish to tell you that I am more than impressed.
*Not only you were extremly proactive, but you worked together with a touch of magic.
************************** ******
The following last script from billprew (that was inspired by the previous comment of the leg puller t0t0...;-)worked without a glitch.
Thanks to you all. You are my heros!!
I just wished I could give more than 500 pts...
Cheers,
Rene
*I first wish to tell you that I am more than impressed.
*Not only you were extremly proactive, but you worked together with a touch of magic.
**************************
The following last script from billprew (that was inspired by the previous comment of the leg puller t0t0...;-)worked without a glitch.
Thanks to you all. You are my heros!!
I just wished I could give more than 500 pts...
Cheers,
Rene
Pleasing feedback.
Pity I missed the definition of %File_LOG_Mirror% - I genuinely thought this was defined elsewhere.
Not to worry though. We got there in the end. It's great to be credited. Thank you.
The challenge far outweighs the points.
Pity I missed the definition of %File_LOG_Mirror% - I genuinely thought this was defined elsewhere.
Not to worry though. We got there in the end. It's great to be credited. Thank you.
The challenge far outweighs the points.
Thanks Rene.
~bp
~bp
Very kind Rene. I wish you could give more points too! lol Glad we could help, or should I say they ;
Cheers,
dt
Cheers,
dt
10x man