I wrote the hybrid JScript / batch utility REPL.BAT, which makes this task extremely simple and very fast. The utility reads stdin, searches for and replaces regular expressions, and writes the output to the stout file.
Utility is a script that works in any modern version of Windows, starting with XP, and does not require the installation of any third-party tools.
The next batch of scripts processes all the files in the given path and deletes the last line of each file only if it is the word exit without any other characters. I use parameter I to make the search case insensitive. Option M also required to allow multiple line searches. Regular regex can be easily cleaned as needed. For example, you can also match and delete the last line if it contains EXIT with a space.
@echo off for %%F in (\somePath\*) do ( type "%%F"|repl "^exit\r?\n?(?!\S|\s)" "" im >"%%F.new" move /y "%%F.new" "%%F" >nul )
The REPL.BAT file should either be in your current folder (the folder from which you are working), or better yet, in one of your PATH folders.
Here is the REPL.BAT script. I found it incredibly useful for such a small script. Full documentation is built into the script.
@if (@X)==(@Y) @end 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)); } } }