1

is there any way to add characters to dir output?

for example

C:\temp\temp>dir /B

outputs:

file1.txt

file2.txt

I would like it to add the word "file" and surround filename in single quotes (from within a batch file)

hopeful/expected output would be:

file 'file1.txt'

file 'file2.txt'

Eventually I will use > to export this to a txt file then pass that text file to ffmpeg as a parameter. I tried FART however I couldn't add characters to the beginning of the line.

seems like this should be a fairly common use case, listing files in a directory to use for input to another program. but how would I get these extra characters in there? ("file" and single quotes)

0

I would like it to add the word "file" and surround filename in single quotes

Use the following batch file (test.cmd):

@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%f in ('dir /b') do (
  echo file '%%f'
  )
endlocal

Example usage:

F:\test\test>dir
 Volume in drive F is Seagate 1
 Volume Serial Number is 1A87-3A15

 Directory of F:\test\test

16/04/2020  22:20    <DIR>          .
16/04/2020  22:20    <DIR>          ..
10/06/2020  18:17                 0 file1.txt
10/06/2020  18:17                 0 file2.txt
               2 File(s)              0 bytes
               2 Dir(s)  44,023,545,856 bytes free

F:\test\test>..\test.cmd
file 'file1.txt'
file 'file2.txt'
F:\test\test>

To export to a text file (eg test.out):

@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%f in ('dir /b') do (
  echo file '%%f' >> test.out
  )
endlocal

Example usage:

F:\test\test>..\test.cmd
F:\test\test>type test.out
file 'file1.txt'
file 'file2.txt'

F:\test\test>

Further Reading

0

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.