List specific files from a directory

2

I can't list a specific files of a directory passed as argument to a batch file.

The problem I am facing is that the pipe "|" character is not recognized, I used it to circumvent the dir command limitations that it can

dir /b *.avi *.mp4 *.mkv

within a directory, but as I am creating this batch file to execute after uTorrent, I need to pass the directory as argument.

Globally, what I am trying to do is to automatically convert files downloaded by uTorrent to AAC audio.

Here's the batch files content:

FOR /F "tokens=*" %%i IN ('dir %1 /b /A-D | findstr /I (avi mp4 mkv)') DO ffmpeg -i "%%~fi" -c:v copy -c:a aac -ac 2 "%%~di%%~pi%%~ni_aac%%~xi" 
PAUSE

AlexandreG

Posted 2019-01-01T20:58:31.913

Reputation: 121

Answers

1

The pipe | character is not recognized

You would need to escape it as follows:

^|

There are some additional errors in your code:

  • Piping to findstr /I (avi mp4 mkv) doesn't work as findstr doesn't work that way. You don't need findstr or piping anyway.

  • If it did work you would have to also escape ( and ).

  • "%%~di%%~pi%%~ni_aac%%~xi" doesn't give the output file the extension .acc

Try the following:

pushd %1
FOR /F "usebackq tokens=*" %%i IN (`dir /b *.avi *.mp4 *.mkv`) DO (
  echo ffmpeg -i "%%~fi" -c:v copy -c:a aac -ac 2 "%%~di%%~pi%%~ni.aac" 
  )
PAUSE
popd

Remove the echo if you are happy with the modified ffmpeg command.

^ Escape character.

Adding the escape character before a command symbol allows it to be treated as ordinary text.

When piping or redirecting any of these characters you should prefix with the escape character: & \ < > ^ |

eg ^\ ^& ^| ^> ^< ^^

Source Quotes, Escape Characters, Delimiters - Windows CMD - SS64.com


Further Reading

PAUSE

DavidPostill

Posted 2019-01-01T20:58:31.913

Reputation: 118 938

No, it does not work: it says ') was expected – AlexandreG – 2019-01-01T21:55:39.987

Answer updated. – DavidPostill – 2019-01-01T22:17:03.157