Friday 15 January 2010

powershell - Replace mulitiple line using CMD -



powershell - Replace mulitiple line using CMD -

i trying create batch file conver files in folder .html folder .txt, find , replace text , rever .txt file .html. html file contain line of code

`$(".screenshot").click(function () { $(".ui-background").show(); $(this) .clone() .appendto($('body')) .addclass("full"); });`

and replace

$("img.screenshot").addclass("fulldisplay"); $(".fulldisplay").css('float', 'left'); $(".fulldisplay").css('position', 'relative'); $(".fulldisplay").css('width', '100%'); $(".fulldisplay").css('height', 'auto');

using ren cmd line able convert html file .txt can search string , replace required one. when comes find , replace nil worked me. tried utilize

powershell -command "(gc c:\users\ivis\downloads\m2test.txt) -replace '$(".screenshot").click(function(){$(".ui-background").show();$(this).clone().appendto($('body')).addclass("full");});','$("img.screenshot").addclass("fulldisplay"); $(".fulldisplay").css('float', 'left'); $(".fulldisplay").css('position', 'relative'); $(".fulldisplay").css('width', '100%'); $(".fulldisplay").css('height', 'auto');'| out-file c:\myfile.txt"

and few other things nil seems working. tried utilize fnr.exe didnt work either.

i think issue having powershell -replace uses regex matching, search string have escaped regex in order work. think work improve create script in powershell, , either run straight or phone call script batch file. this:

param([string]$dir='.') $seek=(@' `$(".screenshot").click(function () { $(".ui-background").show(); $(this) .clone() .appendto($('body')) .addclass("full"); }); '@).trim() $newtext=(@' $("img.screenshot").addclass("fulldisplay"); $(".fulldisplay").css('float', 'left'); $(".fulldisplay").css('position', 'relative'); $(".fulldisplay").css('width', '100%'); $(".fulldisplay").css('height', 'auto'); '@).trim() get-childitem "$dir\*.html" | foreach{ $filepath=$_.fullname (get-content $_.fullname -readcount 0) -replace [regex]::escape($seek), $newtext|out-file $filepath }

then can phone call batch file , pass folder path , process of html files in folder.

powershell replace cmd find

No comments:

Post a Comment