Commands reading in data from a file often but not always support also reading in data from handle STDIN. The pipe character |
redirects the output written to handle STDOUT of application A to handle STDIN of application B. For more details read the Microsoft article about Using command redirection operators and SS64 article about Redirection.
Which console applications support input from STDIN is a matter of documentation. There are just a few console applications installed with Windows supporting input from STDIN. But there are lots of third-party console applications supporting input from STDIN. Many of them don't explicitly explain in documentation that input can be read also from STDIN and not only from an input file specified as parameter. But support of data input from STDIN can be always found out very quickly with one trial.
Standard Windows console applications supporting also STDIN among FIND and FINDSTR are for example MORE and SORT.
Most standard Windows console applications and internal commands of cmd.exe
do not process the contents of files and therefore don't support STDIN. For a list of Windows standard commands see Microsoft's command-line reference. But more details about the Windows standard command line commands can be found on the webpages linked from SS64.com - A-Z index of the Windows CMD command line.
Redirection from STDOUT of an application to STDIN of another application is very often used with third-party console applications because many of them support input from STDIN. Every compiler/script interpreter writes messages to STDOUT and fatal error messages to STDERR which are redirected by many GUI text editors and IDE applications for capturing, filtering and printing those messages into a GUI window for the programmer. This redirection is done using same mechanisms as Windows command interpreter offers with the redirection operators >
and >>
and |
.
In general all console applications supporting input from keyboard support also input from STDIN redirected from output or another application or from a file. But as always there are exceptions as for example some applications by design accept input only directly from keyboard and not from handle STDIN for example because of security reasons like not an application or script should confirm/enter something, but a real human user via keyboard.
The command SET is for assigning a value to an environment variable, or outputting the values of 1 or more environment variables to STDOUT, or evaluating an arithmetic expression, or prompting a batch user for a string reading from STDIN. This command is not designed for reading from a file or writing to a file.
But it is nevertheless possible to redirect output of an application to an environment variable using set /P
and a special syntax as jeb describes in his answer. But this method is rarely used and I think the reason is the complexity of the required expression.
However, the demo code is for assigning the current time to an environment variable. Very often there is a command parameter or another feature which avoids the need to redirect text to STDIN of a command or console application. This is also the case for getting an environment variable with current time as value.
There are the predefined environment variables DATE and TIME. Run in a command prompt window set /?
and read all pages of output help. On last page some special environment variables often needed in batch file are listed with an explanation including DATE and TIME.
Try it out with
echo Current date is %DATE% and current time is %TIME%
Please note that format of date and time string returned by %DATE%
and %TIME%
depends on current settings in Windows Region and Language settings (country). So use %DATE%
and %TIME%
only if the batch file is executed always on same computer where date/time format is well known and does not change. Otherwise it is better to use wmic.exe OS get localdatetime
. For more details about date/time usage on Windows see answer on Find out if file is older than 4 hours in Batch file.
The command FOR can be used to assign a string output by an application to 1 or more environment variables. Run in a command prompt window for /?
for a help on this command. The method with usage of FOR is most often used to assign output of an application or a text read from a file to an environment variable.
Batch file example:
@echo off
for /F %%I in ('time /T') do set "a=%%I"
echo Time is %a%