Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
goodywp
- Posts: 265
- Joined: 31 Jul 2017 09:57
#1
Post
by goodywp » 12 Mar 2018 15:00
Hi,
I have a txt file and need to replace some txt for updating.
Code: Select all
.......
set oldgen=%oldgen: =%
set newgen=%newgen: =%
@echo off &setlocal
set "search=%oldgen%"
set "replace=%newgen%"
set "textfile=Readme-how-to-use-this-package.txt"
set "newfile=indesnew.txt"
(for /f "delims=" %%i in (%textfile%) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
echo(!line!
endlocal
))>"%newfile%"
del %textfile%
rename %newfile% %textfile%
I can do the job but the thing is that after the converting, the format of this txt file is shrinking in terms of missing blank lines and looks a little bit ugly ...
Is any way to insert new blink line in each paragraph or another work around to do the job?
Thanks
-
goodywp
- Posts: 265
- Joined: 31 Jul 2017 09:57
#2
Post
by goodywp » 13 Mar 2018 16:09
I did try the post suggested and not work as expected. And today I found that post has been withdrawn..
-
elzooilogico
- Posts: 128
- Joined: 23 May 2016 15:39
- Location: Spain
#3
Post
by elzooilogico » 14 Mar 2018 03:30
supposed lines doesn't start with
[ nor
] this should work
Code: Select all
for /F "tokens=1,* delims=[]" %%i in ('"type "%textfile%"|find /N /V """') do (
echo(%%j
)
but, as you are doing string substitutions
Code: Select all
>"%newfile%" (
for /F "tokens=1,* delims=[]" %%i in ('"type "%textfile%"|find /N /V """') do (
set "line=%%j"
if "!line!" == "" (
echo(
) else (
set "line=!line:%search%=%replace%!"
echo(!line!
)
)
)
the
Code: Select all
if "!line!" == "" (
...
) else (
...
)
block is mandatory as if
line is empty you'll end up echoing
%search%=%replace%
-
Compo
- Posts: 600
- Joined: 21 Mar 2014 08:50
#4
Post
by Compo » 14 Mar 2018 06:06
You can do the same thing using
FindStr instead of
Find
Code: Select all
@Echo Off
Set "search=%oldgen%"
Set "replace=%newgen%"
Set "textfile=Readme-how-to-use-this-package.txt"
Set "newfile=indesnew.txt"
(
For /F "Tokens=1* Delims=:" %%A In ('FindStr /N "^" "%textfile%"') Do (
If "%%B"=="" (
Echo=
) Else (
Set "line=%%B"
SetLocal EnableDelayedExpansion
Set "line=!line:%search%=%replace%!"
Echo=!line!
EndLocal
)
)
)>"%newfile%"
Del "%textfile%"
Ren "%newfile%" "%textfile%"
-
goodywp
- Posts: 265
- Joined: 31 Jul 2017 09:57
#5
Post
by goodywp » 19 Mar 2018 08:59
Thank you both Compo and elzooilogico!!! it works perfectly!!! appreciated.