how to do replace in file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
doscode
Posts: 175
Joined: 15 Feb 2012 14:02

how to do replace in file?

#1 Post by doscode » 28 Jun 2012 10:21

Is any chance to do this in CMD? I have html file where I would like to replace some strings like this:

Code: Select all

<span></span>.

would be replaced by

Code: Select all

.<span>

Edit:
I found better to completely remove

Code: Select all

<span></span>
from the file.
Last edited by doscode on 29 Jun 2012 01:49, edited 3 times in total.

e4nd
Posts: 12
Joined: 17 Mar 2012 02:28

Re: how to do replace in file?

#2 Post by e4nd » 28 Jun 2012 11:42

Sorry for my English, I use the translator :oops:

The file "file.txt"

Code: Select all

<span></span>.
<span></span>.
<span></span>.

Code: Select all

@echo off & setlocal enabledelayedexpansion
 ::
 :: remember the "^" before each ">", "<"
 :: replaces "<span> </ span>" -> ".<span>"
 ::
 set file=file.txt
 ::
 for /f "tokens=*" %%t in (%file%) do (
    set S=%%t
   set S=!S:^<span^>^</span^>.=.^<span^>!
   >>$tmp$ echo !S!
   )
   del %file%
   ren $tmp$ %file%
pause>nul

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: how to do replace in file?

#3 Post by doscode » 29 Jun 2012 00:04

@e4nd:
I think this code is great because it removes white spaces from begin of every line which I also need. Thank you.

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: how to do replace in file?

#4 Post by doscode » 29 Jun 2012 01:40

Thank you.

I finally decided to change the code, but it helped me.

Code: Select all

FOR /f "tokens=*" %%t in (%1) do (
   SET S=%%t
   if defined S (
     SET S=!S:^<span^>^</span^>=!
     >>$tmp$ echo !S!
   )
)

Post Reply