How to change an image tag url in multiple html files using batch script? -
there more 10 html files image tags. every time deploy our build onto test site need change img source. eg <img src=/live/content/xyz.png />
<img src=/test/content/xyz.png />.
after looking around , reading sometime, have come following batch script, cant figure out how go further here :
for /r %%i in (*.html) echo %%i %%f in (*.html) ( /f %%l in (%%f) ( set "line=%%l" setlocal enabledelayedexpansion set "x= <--------------------what set here? echo %x% endlocal )) pause this first batch script, please guide me in right direction?
@echo off setlocal enabledelayedexpansion /r u:\ %%i in (*.html) ( echo found %%i set outfile="%%~dpni.lmth" ( setlocal disabledelayedexpansion /f "usebackq delims=" %%l in ("%%i") ( set "line=%%l" setlocal enabledelayedexpansion set "line=!line:/live/=/test/! echo !line! endlocal ) endlocal )>!outfile! ) pause goto :eof how development?
notes: i've modified for/r echo html file being processed , use %%i rather switching %%f. u: ramdrive; you'd need modify suit.
outfile set generate filename matches html filename, .lmth extension (can't update in-place) - gets ~dpn prefixing i, means drive, path , name of file %%i. it's quoted take care of potential spaces in filename or pathname.
the next logical statement (for /f...[lines] )>!outfile! sends echoed text new file !outfile!. enabledelayedexpansion in second physical line of batch makes !outfile! run-time value - changed within for r outer loop.
since actual html filename in %%i may contain spaces, needs quoted, hence 'usebackq' clause in for/f. delims= clause ensures entire line file "%%i" applied %%l - not first token (well, actually, makes entire line first token).
the set command substitutes string "/test/" occurrence of "/live/" in run-time value of variable lineand assigns result line. resultant value echod - redirected outfile
note in original, assigning x in set x= echo %x% have reproduced x stood when line parsed because batch substitutes value of variable %var% part of parsing phase. consequently, line have become echo (since x unassigned) , bizarrely have reported echo state (echo off)
a couple of gatchas here. first, % , other characters notoriously hard process batch, careful. next, for/f bypass empty lines. can overcome if required. third, replace occurrence of /live/ in case /test/
good luck!
edit support exclamation marks: 20130711t0624z
added setlocal enabledelayedexpansion line , endlocal before )>!outfile! match
Comments
Post a Comment