Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect output of command in for loop of batch script

...
for /F %%F in ('dir /B %* 2> nul') do (
...

What I'm attempting to do here is discard the err output of the command (and loop over the stdout output). However, it complains:

2> was unexpected at this time.

Is this some way to achieve this?

like image 656
TripShock Avatar asked Jun 14 '13 16:06

TripShock


People also ask

How do I redirect the output of a command in command prompt?

To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.

Can I use for loop in CMD?

FOR /F. Loop command: against the results of another command. FOR /F processing of a command consists of reading the output from the command one line at a time and then breaking the line up into individual items of data or 'tokens'. The DO command is then executed with the parameter(s) set to the token(s) found.

What does %% do in batch file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.


1 Answers

in this case you need to escape the > like this

for /F %%F in ('dir /B %* 2^> nul') do (
like image 147
RGuggisberg Avatar answered Nov 03 '22 01:11

RGuggisberg