Page 1 of 1

how to do replace in file?

Posted: 28 Jun 2012 10:21
by doscode
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.

Re: how to do replace in file?

Posted: 28 Jun 2012 11:42
by e4nd
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

Re: how to do replace in file?

Posted: 29 Jun 2012 00:04
by doscode
@e4nd:
I think this code is great because it removes white spaces from begin of every line which I also need. Thank you.

Re: how to do replace in file?

Posted: 29 Jun 2012 01:40
by doscode
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!
   )
)