With this command

dir "directory" / s / b> c: \ file.txt

I save all files in a given directory to a text file that formatting

C:\Full Name

But how to list files with the same extension (.wav) and save with this formatting?

?C:\Full Name.wav

If this is possible of course.

  • 1
    dir "directory" /b /s should give the file names with extensions. Do you want it to list files matching a particular extension only ? Apr 2, 2015 at 4:32

2 Answers 2


dir syntax errors

You are using the command:

dir "directory" / s / b> c: \ file.txt

You cannot have spaces between the parts of a filename, so c: \ file.txt should be c:\file.txt.

If filename contains spaces it should be quoted, eg "c:\filename with space.txt".

Although not required it is normal to not have a space after the / that specifies a command option. So your command should be something like:

dir "directory" /s /b > c:\file.txt

How do I get a list of files (.wav) and save with this formatting?

?C:\Full Name.wav

Use the following command:

dir "directory\*.wav" /s /b >> c:\file.txt

If you wish to prefix the name with ? (as per your example), then use the following command:

for /f "usebackq tokens=*" %a in (`dir /b /s "directory\*.wav"')  do echo ?%a >> c:\file.txt


  • To use the previous command in a batch file replace each % with %%.
  • In the for /f command use >> c:\file.txt instead of > c:\file.txt to append instead of overwriting the output each time through the loop.

Further Reading

  • Thank you for responding. But I see that the data is overwritten leaving only the last line of the loop in the C: \ file.txt. How to work around this? Apr 7, 2015 at 3:38
  • @CaitanoOliveira Answer updated to prevent overwriting of data when using for /f
    – DavidPostill
    Apr 7, 2015 at 4:27
  • Thank you, almost everything works well, now I'm having trouble with long file names. C:\Program Files (x86)\Mid\FileTest.exe output is C:\Program How to get around this ? Apr 9, 2015 at 2:22
  • @CaitanoOliveira for /f answer updated to work with file/directory names containing a space
    – DavidPostill
    Apr 9, 2015 at 4:25
  • It all worked here, I am grateful for the information which gave me. Apr 10, 2015 at 19:47

(You will probably need to Run 'cmd' as Admin to save in the c: drive)

DIR [drive:][path][*.wav] /s/b >c:\file.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.