Faking standard input on the Windows command line

2020-03-01 23:06发布

问题:

I want to use Evernote's ENScript.exe to create new notes, entering the text and title as arguments. The problem is that ENScript only allows the text to be entered via a file or via standard input.

For my current workaround I use a .bat file to write the argument to a file, then call ENScript with the /s argument pointing to the file to read it in, but that forces the default title to the temporary file's filename (a behavior I do not want).

So my question is: Is there a way to "fake" standard input on the Windows command line so that I can use an argument (passed from another program) to generate the note text? The beginnings of the script would be something like

ENScript.exe createNote /i %1

with the standard input following.

回答1:

You are looking for a pipe operation that captures the output of one command and sends it as input to the next. This is a standard capability in most operating systems.

The pipe symbol for Windows CMD is |

Your script could be as simple as:

@echo %~2|@ENScript.exe createNote /i %1

If your script is called MakeNote.bat, you would call it like

MakeNote "your title" "This is the text of your note"


回答2:

One can "fake" standard input by using redirection:

command args... < filename args...

where the < means input redirection ("read standard input from the filename after the < instead of the terminal").

(Note that old Windows or DOS programs may read straight from the terminal, making input redirection useless; this hopefully won't apply to something as recent as Evernote.)

For your example:

ENScript.exe < "%1"

Feel free to add more arguments before or after the redirection. For example, if your script will be called as script filename title, you will want to invoke ENScript /i "%2" < "%1".