Linux (Bash) has some very useful functionality for dumping literal text into another file as follows:
cat > see.txt << EOF contents going into my file EOF
What I need is the equivalent for a windows script package. I did not find such functionality built-in, but I thought I could write a subroutine for this (I do not want to rely on something that was not originally on Windows with XP), but I had problems. Here is what I have so far through various sources :
call:catMyChunk myCustomText c:\see.txt exit /b goto:myCustomText This is my test file Hope you like it. <got these> % and these % ! these too yeah :myCustomText :catMyChunk ::Should call this function with 2 args, MYDELIM and outFile.txt ::where is to be catted to outFile.txt ::and text starts with <beginning of line>goto:MYDELIM ::and ends with <beginning of line>:MYDELIM set searchStart=goto:%~1 set searchStop=:%~1 set outFile=%~2 set startLine=0 set endLine=0 for /f "delims=:" %%a in ('findstr -b -n !searchStart! %~dpnx0') do set "startLine=%%a" for /f "delims=:" %%a in ('findstr -b -n !searchStop! %~dpnx0') do set "endLine=%%a" set /a linesLeftToRead=%endLine% - %startLine% del %outFile% if "%linesLeftToRead%" LEQ "0" ( echo Error finding start and end delmieters for %searchStop% in catMyChunk routine exit /B 1 ) setlocal DisableDelayedExpansion for /f "usebackq skip=%startLine% delims=" %%a in (`"findstr /n ^^ %~dpnx0"`) do ( set "oneLine=%%a" setlocal EnableDelayedExpansion set "oneLine=!oneLine:*:=!" set /a linesLeftToRead-=1 if !linesLeftToRead! LEQ 0 exit /B echo(!oneLine!>>%outFile% ) goto: EOF
So, my requirement is that a piece of text is displayed literally without any changes (i.e. I do not want to run empty lines,%,!, <, Etc.). This code does almost everything I need, except that I have not found a way to get the exclamation points correctly. Here is the result that I get, which is not entirely correct:
This is my test file Hope you like it. <got these> % and these % these too yeah
Edit: For those who want to change a modified version of a routine that now works, here it is:
:catMyChunk ::Should call this function with 2 args, MYDELIM and outFile.txt ::where is to be catted to outFile.txt ::and text starts with <beginning of line>goto:MYDELIM ::and ends with <beginning of line>:MYDELIM set searchStart=goto:%~1 set searchStop=:%~1 set outFile=%~2 set startLine=0 set endLine=0 for /f "delims=:" %%a in ('findstr -b -n !searchStart! %~dpnx0') do set "startLine=%%a" for /f "delims=:" %%a in ('findstr -b -n !searchStop! %~dpnx0') do set "endLine=%%a" set /a linesLeftToRead=%endLine% - %startLine% del %outFile% if "%linesLeftToRead%" LEQ "0" ( echo Error finding start and end delmieters for %searchStop% in catMyChunk routine exit /B 1 ) setlocal DisableDelayedExpansion for /f "usebackq skip=%startLine% delims=" %%a in (`"findstr /n ^^ %~dpnx0"`) do ( set "oneLine=%%a" set /a linesLeftToRead-=1 setlocal EnableDelayedExpansion set "oneLine=!oneLine:*:=!" if !linesLeftToRead! LEQ 0 exit /B echo(!oneLine!>>%outFile% endlocal ) endlocal goto: EOF
Jared source share