0

In batch, you can print some text to a file like this:

echo Foo Bar> test.txt

And you can print a block of text like this:

> test.txt (
    echo Foo Bar
    echo Bar Foo
)

So, is there nice, pretty way to print a block of text without using echo on each line? Something like:

echo> test.txt (
    Foo Bar
    Bar Foo
)

Note: The issue is not that I can't have echo... It's that I don't want it -- the point of this is to make the code easier to read and nicer to look at and write.

Quelklef
  • 1,999
  • 2
  • 22
  • 37

4 Answers4

1

Like this, perhaps?

>u:\file.txt (FOR %%a IN ("line 1" "line 2" "line3") do echo %%~a)

Edit by rojo: It should be noted that each of the quoted parameters can be placed on its own line for further improved code readability.

@echo off
setlocal

>out.txt (
    for %%I in (
        "the quick brown"
        "fox jumps over"
        "the lazy dog"
    ) do echo %%~I
)

type out.txt
rojo
  • 24,000
  • 5
  • 55
  • 101
Magoo
  • 77,302
  • 8
  • 62
  • 84
  • This is almost perfect... See the reason why it's _almost_ so here (not in comments 'cause it's multi-lined): http://pastebin.com/2tT6zVU7 – Quelklef Jul 01 '15 at 02:13
  • 1
    Try using `%%~a` as I coded, not `%%a`. The quotes surround a string which may contain a separator like space or comma. The `~` removes the surrounding quotes. It can't be used for required-strings containing quotes. Modify `echo %%~a` to `echo(%%~a` and use `""` for an empty line. The `echo(` syntax allows an empty argument whereas traditional `echo ` will report the current `echo` status if the argument is empty. – Magoo Jul 01 '15 at 03:05
  • You may have included this in your comment, and I just didn't understand, but: - Will I be able to have `"`'s in my strings, and how - What other options do I have besides surrounding each string with `"`'s? It works very nicely for `"strings like this"`, though. Looks like it's my answer. – Quelklef Jul 01 '15 at 04:13
  • @Magoo Hope you don't mind the edit. I was going to post it as an answer, but it's basically the same answer as yours already. If I'm out of line, I won't be offended if you roll back and offer your own suggestion of where I should stick my code. – rojo Jul 01 '15 at 04:32
  • @Quelklef Do see [the page linked by Aacini](http://stackoverflow.com/questions/1015163/) for other ideas. – rojo Jul 01 '15 at 04:34
0

You may show several lines with just one echo command:

@echo off
setlocal EnableDelayedExpansion

set NL=^
%Don't remove%
%these lines%

(
echo ^
Line one!NL!^
Line two!NL!^
Line three
) > test.txt

EDIT: After read this additional specification in a comment:

It's a large block of more batch code, maybe 50 lines... I may want to edit this code later on, so ease of reading is a necessity. I don't want to have all 50 lines in one line, as this is for clarity rather than functionality.

I think I finally understood the request, that is: "How to embeed lines of text in a Batch file, and extract them to another file?". This is the method I use for the same purpose:

@echo off 

rem Use :GetResource subroutine to extract text placed in this file; for example:
call :GetResource example.bat
call :GetResource example.txt

rem Execute the just extracted Batch file
call example.bat

goto :EOF


A couple examples of embedded files


<resource id="example.bat">
@echo off
echo Hello, I am example.bat
type example.txt
</resource>

<resource id="example.txt">
An example of data file
</resource>


rem Extract text from lines placed in a "resource" in this .bat file

:getResource resourceId
setlocal EnableDelayedExpansion
rem Resource data start format: <resource id="resourceId">
set start=
set lines=
for /F "tokens=1,3 delims=:=>" %%a in ('findstr /N "^</*resource" "%~F0"') do (
   if not defined start (
      if "%1" equ "%%~b" set start=%%a
   ) else (
      if not defined lines set /A lines=%%a-start-1
   )
)
set line=0
(for /F "skip=%start% tokens=1* delims=]" %%a in ('find /N /V "" ^< "%~F0"') do (
   setlocal DisableDelayedExpansion
   echo(%%b
   endlocal
   set /A line+=1
   if !line! equ %lines% exit /B
)) > "%~1"
exit /B
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • The issue is not that I can't have `echo`... It's that I don't want it -- the point of this is to make the code easier to read and nicer to look at and write. – Quelklef Jul 01 '15 at 02:16
0

I've written a PrintHere.bat utility that I think gives you exactly what you are looking for :-)
Follow the link to get the code and for some explanation as to how it works.

Here is how you would use it for your example:

call PrintHere /- " " :text >test.txt
    Foo Bar
    Bar Foo
:text

I added the /- " " to strip the leading spaces from the output. Without that option, you would use the following to get the same result:

call PrintHere :text >test.txt
Foo Bar
Bar Foo
:text
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
0

Well, this is pretty old, and has some interesting answers. Let's see if this one also proves useful to searchers. If your text is a fixed block of text, like a bit of code you want to insert to a program, then you can put it in its own file, and "type" it.

myblock.txt:
Hello world
isn't this nice?

Then you can insert these two lines with a simple type command

@echo off
type myblock.txt >> target_file.sql.

I'm doing this right now with an oracle "deployment script builder", which takes what we have in Subversion and packages it nicely into schema-based scripts. Don't forget the use ">>" if you want to append the text to an existing file.

VeteranCoder
  • 444
  • 1
  • 4
  • 12