Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to write a quick batch program that will either copy the names of the files in a folder or that folder and the sub folders. The issue I'm having is: I don't want the directory names.

Here is my code:

if %_JOB%! NEQ ! set _JOB=%_JOB%-
if %_FOLDERS%! == y! set _FOLDERS=Y

echo Created %cdate% > "C:\Users\%username%\Desktop\%_JOB%PDFlist.txt"
echo Folder: %CD% >> "C:\Users\%username%\Desktop\%_JOB%PDFlist.txt"
if %_FOLDERS%! == Y! (
  echo Include sub-folders: True >> "C:\Users\%username%\Desktop\%_JOB%PDFlist.txt"
) ELSE (
echo Include sub-folders: False >> "C:\Users\%username%\Desktop\%_JOB%PDFlist.txt"
echo. >> "C:\Users\%username%\Desktop\%_JOB%PDFlist.txt"
echo ============================== BEGIN LIST ============================== >> "C:\Users\%username%\Desktop\%_JOB%PDFlist.txt"
if %_FOLDERS%! == Y! (
  dir *.pDF /s /b /A-D-H >> "C:\Users\%username%\Desktop\%_JOB%PDFlist.txt"
) ELSE (
  dir *.pdf /b /A-D-H >> "C:\Users\%username%\Desktop\%_JOB%PDFlist.txt"
echo =============================== END LIST =============================== >> "C:\Users\%username%\Desktop\%_JOB%PDFlist.txt"
echo.&echo List Generated!
echo.&echo.&ECHO.Press ANY key to EXIT and view your file
start notepad "C:\Users\%username%\Desktop\%_JOB%PDFlist.txt"
share|improve this question

If you do not recurse into subfolders, then the bare format switch (/b) is sufficient. If you recurse with the same switch, however, you get the whole path.

With Command Extensions enabled, you can use the following to filter out just the filenames:

for /f "delims=" %%F in ('dir *.pdf /s /b /A-D-H') do @echo %%~nxF

See help for for more info, but what the above does, is take the output of the dir … command and for each item then print out just the name and extension (effectively dropping drive and path).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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