7

In Bash, I can do something like this

somecmd << END
a lot of 
text here
END

to feed input to a command directly from a script. I need to do the same in CMD.exe batch files (.cmd scripts). Is it possible?

Krumelur
  • 667
  • 4
  • 11
  • 19
  • Now that I know what they are called, I found a previous question on Stackoverflow: http://stackoverflow.com/questions/1015163/heredoc-for-windows-batch Too complex for me. Time to switch to another shell/language :) – Krumelur Jan 12 '16 at 15:32

3 Answers3

6

It's simple, but not as clean looking as it is in Unix/Linux. Try this:

(@echo.a lot of
@echo.text here
) | somecmd

Note that the . after the echo statement allows you to begin a line with blanks. The @ symbol is needed to prevent the echo statement from being sent to somecmd. You can eliminate the @ symbol thusly:

echo off
(echo.a lot of
echo.text here
) | somecmd
echo on
B. Reynolds
  • 61
  • 1
  • 3
  • worked on Windows 10 CMD, but does not work on PowerShell FWIW. Used with DiskPart to pipe some startup command (without making a single line text file to use as script). – Kevin Jan 16 '18 at 17:50
  • 1
    Yes, these are statements that work with cmd.exe, not with powershell.exe. If you want to write scrips for PowerShell, this is not the topic for you. – B. Reynolds Jan 23 '18 at 07:27
  • Here's another example... – B. Reynolds Jan 23 '18 at 07:31
  • Suppose you have a program named ABCDEFG.exe, which requires multi-line input from STDIN... otherwise known as "control cards". This is a very common scenario in Unix, Linux, and many "mainframe" operating systems such as Hewlett Packard's MPE and IBM's z/OS. Suppose the program requires three lines of input, and the ABCDEFG.exe program is located in "C:\My Directory" (note the space in My Directory)... Here's how that works in cmd.exe: echo off (echo.CONTROL CARD ONE echo.CONTROL CARD TWO echo.CONTROL CARD THREE ) | "C:\My Directory\ABCDEFG.exe" echo on – B. Reynolds Jan 23 '18 at 07:57
5

I believe you can use a single ^ character for each line.

EG:

echo This is a really long ^
text message that spans multiple ^
lines

returns:

C:\Users\Jonno>echo This is a really long ^
More? text message that spans multiple ^
More? lines
This is a really long text message that spans multiple lines
Jonno
  • 21,049
  • 4
  • 61
  • 70
  • Thanks! While useful, I would need to provide input via stdin, not as command line arguments. I could pipe the `echo` command, but then I lose the linebreaks. I also believe there is a limitation on the length of the command line. – Krumelur Jan 12 '16 at 15:27
  • @Krumelur Ah, I understand now, sorry, I misinterpreted the question. I'm not sure if you saw the previous comment linking to [this question](http://stackoverflow.com/questions/6979747/read-stdin-stream-in-a-batch-file) and how relevant it would be to your situation? – Jonno Jan 12 '16 at 15:31
  • Yes, I saw it and it is not really relevant for me. This question on SO is however exactly what I ask for http://stackoverflow.com/questions/1015163/heredoc-for-windows-batch . Alas, it seems impossible to do in a simple way. – Krumelur Jan 12 '16 at 15:33
  • I am marking your question as an answer since it is apparently the closest you can get in cmd.exe without some serious hacking or external dependencies. – Krumelur Jan 12 '16 at 15:34
0

Until now, I don't have found any solution to this problem !

I have only a workaround in defining some BAT scripts.

Using my script, the solution to your problem look like this

call INIT-TRAMEX.bat

%assign-sysout% FILE.SYSOUT.TXT

%w% a lot of 
%w% text here

somecmd <%sysout%

But in all cases, the direct indirection is impossible.

INIT-TRAMEX.bat file defines %ASSIGN-SYSOUT% and %W% variables

::******************************************************************************
::* INIT-TRAMEX.bat
::******************************************************************************

@echo OFF

set scriptdir=c:\Scripts

set ASSIGN-SYSOUT=call %scriptdir%\AssignSysout.bat

set WRITE-TEXT=call %scriptdir%\WriteText.bat
set W=call %scriptdir%\WriteText.bat

ASSIGN-SYSOUT script defines %sysout% variable and create an empty file. It contains following lines

set sysout=%1
@echo.>%sysout%
del %sysout%

WRITE-TEXT script contains following lines

IF "%1"=="" goto line
echo %* >>%sysout%
goto quit

:line

echo. >>%sysout%

:quit

Using this tips, DOS script is more readable.

schlebe
  • 247
  • 2
  • 14