special characters - Replace content of all files with some extension -


i have replace content in files specific extension.

for eg: in folder tochange may have multiple sub folder, want change text isabcd isxyz in files extension html

what batch such command, using windows

actually there multiple items need replace. have them in key value form , need replace of them 1 one in files in folder

my text have double quotes want replaced text follows

if (louseragent.tolowercase().indexof("firefox") > -1 || (louseragent.tolowercase().indexof("msie") > -1 && louseragent.tolowercase().indexof("msie 7") != -1)) 

and want converted to

if (louseragent.tolowercase().indexof(aaa.fire_fox) > -1 || (louseragent.tolowercase().indexof(aaa.msie) > -1 && louseragent.tolowercase().indexof(aaa.ie_7) != -1)) 

the conf.txt file have inputs like

 aaa.py_error_occurred_while_getting_payment_list_screen_for_org_type:::"error occurred while getting payment list screen org type: " aaa.py_ao_payment_list_size:::"aopaymentlistsize" aaa.py_ao_payment_status:::"aopaymentstatus" 

where text on right side of ::: text needs replaced(with double quotes) , text may contain special characters well, , string on left text want replace with

ps: sorry asking direct solution. have no idea batch files :(

i've written hybrid jscript/batch utility named repl.bat can problem. believe faster pure batch solution. nice feature solution adapted regex search , replace instead of string literal search , replace.

assuming repl.bat , conf.txt in same folder html files, following script should work:

@echo off setlocal disabledelayedexpansion  :: define lf contain newline (0x0a) character set lf=^   :: 2 blank lines above critical - not remove  :: read search , replace strings , store them in "array" :: also, create variable containing cascading set of piped commands :: carry out each search , replace :: search in s array, , replacement in r array. :: if replacement empty string, search in r array :: , s array value undefined. set "cnt=0" set "update=" /f delims^=^ eol^= %%a in (conf.txt) (   set "ln=%%a"   set /a cnt+=1   setlocal enabledelayedexpansion   %%n in ("!lf!") set "ln=!ln::::=%%~n!"   %%n in (!cnt!) (     /f delims^=^ eol^= %%u in (""!update!"") (       /f delims^=^ eol^= %%l in ("!ln!") (         if "!"=="" endlocal         if not defined r%%n (           set "r%%n=%%l"         ) else (           set "s%%n=%%l"         )       )       if "!"=="" endlocal       if defined s%%n (         set "update=%%~u|repl s%%n r%%n vl"       ) else if defined r%%n (         set "update=%%~u|repl r%%n "" vl"       )     )   ) )  :: process each file %%f in (*.html) (   echo processing %%f   type "%%f" %update% >"%%f.new"   move /y "%%f.new" "%%f" >nul ) echo done! 

the script builds "array" of search , replace strings: s1,s2,...sn, , r1,r2,...rn

it builds update command looks

|repl s1 r1 vl|repl s2 r2 vl...|repl sn rn vl 

each file passed through set of piped repl commands perform search , replace, 1 per repl command.

the script above has following limitations:

  • the final update command build must less 8191 chars long, there limit number of search , replace operations can performed in 1 pass. theoretical limit around 480 substitutions, though don't know how windows behave many pipes in 1 command.
  • none of search or replacement strings can contain :::. inherent design of conf.txt file

here repl.bat file required script above. full documentation embedded within script. script above uses v option read search , replace strings variables, , l option force literal search instead of regex search.

@if (@x)==(@y) @end /* harmless hybrid line begins jscript comment  ::************ documentation *********** ::: :::repl  search  replace  [options  [sourcevar]] :::repl  /? ::: :::  performs global search , replace operation on each line of input :::  stdin , prints result stdout. ::: :::  each parameter may optionally enclosed double quotes. double :::  quotes not considered part of argument. quotes required :::  if parameter contains batch token delimiter space, tab, comma, :::  semicolon. quotes should used if argument contains :::  batch special character &, |, etc. special character :::  not need escaped ^. ::: :::  if called single argument of /? prints documentation :::  stdout. ::: :::  search  - default case sensitive jscript (ecma) regular :::            expression expressed string. ::: :::            jscript regex syntax documentation available @ :::            http://msdn.microsoft.com/en-us/library/ae5bf541(v=vs.80).aspx ::: :::  replace - default string used replacement :::            each found search expression. full support provided :::            substituion patterns available jscript replace method. :::            $ literal can escaped $$. empty replacement string :::            must represented "". ::: :::            replace substitution pattern syntax documented @ :::            http://msdn.microsoft.com/en-us/library/efy6s3e6(v=vs.80).aspx ::: :::  options - optional string of characters used alter behavior :::            of repl. option characters case insensitive, , may :::            appear in order. ::: :::            - makes search case-insensitive. ::: :::            l - search treated string literal instead of :::                regular expression. also, $ found in replace :::                treated $ literals. ::: :::            b - search must match beginning of line. :::                used literal searches. ::: :::            e - search must match end of line. :::                used literal searches. ::: :::            v - search , replace represent name of environment :::                variables contain respective values. undefined :::                variable treated empty string. ::: :::            m - multi-line mode. entire contents of stdin read , :::                processed in 1 pass instead of line line. ^ anchors :::                beginning of line , $ anchors end of line. ::: :::            x - enables extended substitution pattern syntax support :::                following escape sequences: ::: :::                \\     -  backslash :::                \b     -  backspace :::                \f     -  formfeed :::                \n     -  newline :::                \r     -  carriage return :::                \t     -  horizontal tab :::                \v     -  vertical tab :::                \xnn   -  ascii (latin 1) character expressed 2 hex digits :::                \unnnn -  unicode character expressed 4 hex digits ::: :::                escape sequences supported when l option used. ::: :::            s - source read environment variable instead of :::                stdin. name of source environment variable :::                specified in next argument after option string. :::  ::************ batch portion *********** @echo off if .%2 equ . (   if "%~1" equ "/?" (     findstr "^:::" "%~f0" | cscript //e:jscript //nologo "%~f0" "^:::" ""     exit /b 0   ) else (     call :err "insufficient arguments"     exit /b 1   ) ) echo(%~3|findstr /i "[^smilebvx]" >nul && (   call :err "invalid option(s)"   exit /b 1 ) cscript //e:jscript //nologo "%~f0" %* exit /b 0  :err >&2 echo error: %~1. use repl /? help. exit /b  ************* jscript portion **********/ var env=wscript.createobject("wscript.shell").environment("process"); var args=wscript.arguments; var search=args.item(0); var replace=args.item(1); var options="g"; if (args.length>2) {   options+=args.item(2).tolowercase(); } var multi=(options.indexof("m")>=0); var srcvar=(options.indexof("s")>=0); if (srcvar) {   options=options.replace(/s/g,""); } if (options.indexof("v")>=0) {   options=options.replace(/v/g,"");   search=env(search);   replace=env(replace); } if (options.indexof("l")>=0) {   options=options.replace(/l/g,"");   search=search.replace(/([.^$*+?()[{\\|])/g,"\\$1");   replace=replace.replace(/\$/g,"$$$$"); } if (options.indexof("b")>=0) {   options=options.replace(/b/g,"");   search="^"+search } if (options.indexof("e")>=0) {   options=options.replace(/e/g,"");   search=search+"$" } if (options.indexof("x")>=0) {   options=options.replace(/x/g,"");   replace=replace.replace(/\\\\/g,"\\b");   replace=replace.replace(/\\b/g,"\b");   replace=replace.replace(/\\f/g,"\f");   replace=replace.replace(/\\n/g,"\n");   replace=replace.replace(/\\r/g,"\r");   replace=replace.replace(/\\t/g,"\t");   replace=replace.replace(/\\v/g,"\v");   replace=replace.replace(/\\x[0-9a-fa-f]{2}|\\u[0-9a-fa-f]{4}/g,     function($0,$1,$2){       return string.fromcharcode(parseint("0x"+$0.substring(2)));     }   );   replace=replace.replace(/\\b/g,"\\"); } var search=new regexp(search,options);  if (srcvar) {   wscript.stdout.write(env(args.item(3)).replace(search,replace)); } else {   while (!wscript.stdin.atendofstream) {     if (multi) {       wscript.stdout.write(wscript.stdin.readall().replace(search,replace));     } else {       wscript.stdout.writeline(wscript.stdin.readline().replace(search,replace));     }   } } 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -