Overblog Tous les blogs Top blogs Technologie & Science Tous les blogs Technologie & Science
Editer l'article Suivre ce blog Administration + Créer mon blog
MENU

pipeline batch

Publié le par damcuvelier

With the pipeline in batch you can give an answer to a command in command line.

Example:
C:\>echo Y|del /p file.txt
C:\file.txt, delete (Y/N) ? Y

what is the point? you tell me.
just to give an answer to a command in batch, and because, sometime batch is again usefull

more for my personal wiki:

command > filename Redirect command output to a file
command >> filename APPEND into a file
command < filename Type a text file and pass the text to command
commandA | commandB Pipe the output from commandA into commandB
commandA & commandB Run commandA and then run commandB
commandA && commandB Run commandA, if it succeeds then run commandB
commandA || commandB Run commandA, if it fails then run commandB
commandA && commandB || commandC
If commandA succeeds run commandB, if it fails commandC
Numeric handles:
STDIN = 0 Keyboard input
STDOUT = 1 Text output
STDERR = 2 Error text output
UNDEFINED = 3-9
command 2> filename Redirect any error message into a file
command 2>> filename Append any error message into a file
(command)2> filename Redirect any CMD.exe error into a file
command > file 2>&1 Redirect errors and output to one file
command > fileA 2> fileB Redirect output and errors to separate files
command 2>&1 >filename This will fail!
Redirect to NUL (hide errors)
command 2> nul Redirect error messages to NUL
command >nul 2>&1 Redirect error and output to NUL
command >filename 2> nul Redirect output to file but suppress error
(command)>filename 2> nul Redirect output to file but suppress CMD.exe errors

Note, any long filenames must be surrounded in "double quotes".
A CMD error is an error raised by the command processor itself rather than the program/command.
Redirection with > or 2> will overwrite any existing file.
You can also redirect to a printer with > PRN or >LPT1

Multiple commands on one line
In a batch file the default behaviour is to read and expand variables one line at a time, if you use & to run multiple commands on a single line, then any variable changes will not be visible until execution moves to the next line. For example:
SET /P _cost="Enter the price: " & ECHO %_cost%
This behaviour can be changed using SETLOCAL EnableDelayedExpansion

Creating a new file
Create empty files using the NUL device:
Type NUL >EmptyFile.txt
or
Copy NUL EmptyFile.txt

To prevent the > and < characters from causing redirection, escape with a caret: ^> or ^<
Redirect multiple lines by bracketing a set of commands:
(
Echo sample text1
Echo sample text2
) > c:\logfile.txt

Unicode
The CMD Shell can redirect ASCII/ANSI (the default) or Unicode (UCS-2 le) but not UTF-8.
This can be selected by launching CMD /A or CMD /U
With the default settings a UCS-2 file can be converted by redirecting it (note it's the redirection not the TYPE/MORE command that makes the encoding change)
TYPE unicode.txt > asciifile.txt
European characters like ABCàéÿ will usually convert correctly, but others like £¥ƒ€ will become random extended ASCII characters: œ¾Ÿ?

Pipes and CMD.exe
When a command is piped with '| batch_command ' this will instantiate a new CMD.exe instance, in effect running:
C:\Windows\system32\cmd.exe /C /S /D "batch_command"
This has several side effects:
Any newline (CR/LF) characters in the batch_command will be turned into & operators. (see StackOverflow)
If the batch_command includes any caret escape characters ^ they will need to be doubled up so that the escape survives into the new CMD shell.
Starting a new CMD shell also has a small (likely unnoticable) effect on performance.
For example, this syntax works, but would fail if the second or subsequent (piped) lines were indented with a space:
@Echo Off
echo abc def |^
find "abc" |^
find "def"> outfile.txt
Multi-line single commands with lots of parameters, can be indented as in this example:
Echo abc def ^
ghi jkl ^
mno pqr

When redirecting the output of DIR to a file, you may notice that the output file (if in the same folder) will be listed with a size of 0 bytes. The command interpreter first creates the empty destination file, then runs the DIR command and finally saves the redirected text into the file.
The maximum number of consecutive pipes is 2042

Examples:
DIR >MyFileListing.txt
DIR /o:n >"Another list of Files.txt"
DIR C:\ >List_of_C.txt 2>errorlog.txt
DIR C:\ >List_of_C.txt & DIR D:\ >List_of_D.txt
ECHO y| DEL *.txt
ECHO Some text ^<html tag^> more text
COPY nul empty.txt
MEM /C >>MemLog.txt
Date /T >>MemLog.txt
SORT < MyTextFile.txt
SET _output=%_missing% 2>nul

FIND /i "Jones" < names.txt >logfile.txt
(TYPE logfile.txt >> newfile.txt) 2>nul

source: http://ss64.com/nt/syntax-redirection.html

Commenter cet article

A
beau blog. un plaisir de venir flâner sur vos pages. une découverte et un enchantement.N'hésitez pas à venir visiter mon blog. au plaisir
Répondre
W
How did you make the teacher gift with the words going various ways on a sheet of paper? I believe thee is a site online where you can make those, but I don't know what it is called.
Répondre
B
Hi, I bought the scrabble games at a thrift store. It is hit or miss whether they have them, so I always buy them when they do. Good luck!
Répondre