Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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"
cls
echo.&echo List Generated!
echo.&echo.&ECHO.Press ANY key to EXIT and view your file
PAUSE>NUL
start notepad "C:\Users\%username%\Desktop\%_JOB%PDFlist.txt"
EXIT
share|improve this question

1 Answer 1

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

 
discard

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.