I can process file-and-command, file-and-line or several files with FOR / F, but no other combinations. Why?

Here is my code:

@echo off echo ->minus echo _>underscore rem if this file is used it tries to execute a command echo not-at-all>'notacomand' echo processing two files at once for /f "delims=" %%# in (minus underscore ) do echo %%# echo processing file and command for /f "delims=" %%# in (minus '"echo ++"') do echo %%# echo processing files and string for /f "delims=" %%# in (minus underscore "zzz") do echo %%# rem searches for file "zzz" minus rem for /f "delims=" %%# in ("zzz" minus) do echo %%# rem searches for file zzz" minus rem for /f "delims=" %%# in ("zzz" minus) do echo %%# echo the command at the end is not processed for /f "delims=" %%# in (minus "zzz" 'echo ---') do echo %%# rem searches for file 'echo rem for /f "delims=" %%# in (minus 'echo ---' "zzz") do echo %%# 

Tested on Windows 8.1 and Windows XP. ]: -).

I tested very few combinations, but my conclusions so far are:

  • You can process as many files as you want at once. Wild cards not accepted. For files with spaces, useback should be used.
  • You can process as many files as you want, simultaneously with the puls ONE command (but with extra double quotes) or string.Everything after the line or command is ignored except for syntax errors.

I don’t know why (most likely, without a response to the CMD code it is impossible). Also, do not suspect if this might be a useful or well-known behavior, but definitely curious.

+7
cmd batch-file
source share
1 answer

I am sure that you have come across mixed documentation and an error.

I believe that the documentation is trying to convey that / f has three mutually exclusive choices: file set, line, command output. One paragraph in the help begins with "You can also use the FOR / F parsing logic for the immediate line." I think "really" means "one." Similarly, the next paragraph says: "... the output of the command ...". Again, I think that "a" means one thing. My discussion of mutually exclusive is how it is implemented more or less.

One of the errors that I am claiming is that one line works like a line if it is at the end of a set of files. Another may be the odd parsing of the quote characters that have been marked.

I think that a high-level parsing algorithm is something like: 1) look for an open bracket, 2) if the first non-empty quote character, use the appropriate handler for the string or command cases. 3) if the first character is not a quote or returnback is used, and the first character is a double quote, then make a file with number 4), if the previous token was a file specification, then continue parsing, but if the previous token was not a file specification, stop parsing

4) explains the behavior of one line at the end of a set of files

The line parsing algorithm is as follows: 1) look for the starting quote as the first quote after the open paren, 2) look for the final quote as the last quote before the closed paren, 3) everything in between is a line (or command) 4) quotes even do not have to be balanced. The following all behaves sequentially with this explanation:

 rem space is delimiter for /f "tokens=1,2*" %i in ( "zzz" "yyy" ) do echo i %ij %jk %k rem unbalanced quotes for /f "tokens=1,2* delims=" %i in ( 'echo zzz' yyy' ) do echo i %ij %jk %k rem apostrophe is delimiter for /f "tokens=1,2* usebackq delims='" %i in ( 'zzz' 'yyy' ) do echo i %ij %jk %k 
+1
source share

All Articles