Windows – How to output a list of filenames surrounded by quotation marks

cmd.execommand linedirwindows

In Windows Command Prompt, I can use the dir command to output the names of all PNG files within a directory:

dir *.png /od /b > files.txt
file 1.png
file 2.png
...

However, the image files I am working with contain spaces in their filenames, and hence will not work with ImageMagick unless I surround their names with quotation marks, like so:

"file 1.png"
"file 2.png"
...

What is the best way to go about this?

Is there a command that lists filenames surrounded by quotes?

Or will I need to add them in after creating the txt file?

Best Answer

You can use FOR /F to run the DIR command and surround the output with quotes:

This is from the prompt

for /f "delims=" %A in ('dir /b /od *.png') do @echo "%A"

In a batch script, you would double the percent signs, so %A becomes %%A in both places.

Related Question