We are trying to convert a bash shell script into a Windows batch script. We ship these scripts with our software product, and we cannot assume that the customer will have, or will be able to download, sed/awk/cygwin or any other non-standard tools. Therefore, the script must work with whatever tools come out-of-the-box with Windows. The minimum target platform is Windows XP SP2. This script is called from another batch script.
One part of the script needs to search through a file for a particular string and modify that string. In bash, this is easy. The approach we are taking in the Windows batch script is to walk through the file, line by line. If a line does not contain the target string, we echo it to a temp file as-is. If a line contains the target string, we echo a hardcoded string to the temp file.
The problem is that some lines of the input file contain the less-than and greater-than signs. If we put quotation marks around the lines, they are written intact to the temp file, but quoted.
"-- this is a comment in the output .sql file"
"SELECT foo FROM bar WHERE something < 10;"
"--<SpecialInternalTagComment>"
If we unquote them using %~1, then CMD.EXE attempts to treat the < and > as redirection symbols, and produces errors when the script encounters one of those lines, because the resulting command would be something like this:
echo --<SpecialInternalTagComment> >> temp.file
Here's what we have right now. It produces the file contents with every line enclosed in quotation marks.
:clean_install
echo New installation >> %INSTALL_LOG%
echo > %INSTALL_FOLDER%"\sql\oracle\table_maintenance_tmp.sql
for /f "usebackq tokens=* delims=" %%A in (table_maintenance.sql) do (
call :replace_width_and_write_line "%%A"
)
goto done
:replace_width_and_write_line
echo %1 | find "&&TARGET_STRING" > NUL
if %ERRORLEVEL% == 0 (
echo TABLE_PARTITION_WIDTH CONSTANT NUMBER := 7 ; >> %INSTALL_FOLDER%"\sql\oracle\table_maintenance_tmp.sql
goto :eof
)
echo %1 >> %INSTALL_FOLDER%"\sql\oracle\table_maintenance_tmp.sql
goto :eof
The question is, how can we echo the contents of a string to a file, unquoted, if the string contains redirect symbols? If that can't be done, then is there another way to accomplish our original goal, which is to take as input a file that may or may not contain a certain target string, and produce as output a file that is identical to the original file except that if that target string existed, it is replaced by a new string?
I've searched here on stackoverflow, and the two most useful questions are these:
* How do you strip quotes out of an ECHO’ed string in a Windows batch file?
* Dealing with quotes in Windows batch scripts
Note: Please don't suggest Cygwin, sed, awk, Perl, Python, Ruby, unx, or any other technology that does not ship with Windows XP out of the box. Those answers will be voted down.
Thanks,
shoover