Extract a list of files names from a folder using CMD

Extract a list of files names from a folder using CMD

Author
Discussion

ThingsBehindTheSun

Original Poster:

2,028 posts

46 months

Tuesday
quotequote all
I want to extract a list of filename with the path using the DIR function under CMD but I only want to return the records with a file, I don't want to include all the folders i.e

If I had

c:\Temp\1.txt
c:\Temp\2.txt
c:\temp\test\5.txt

I would only want

c:\temp\1.txt
c:\temp\2.txt
c:\temp\test\5.txt

Not to include

c:\Temp
c:\temp\test

Is this possible to exclude the folders and just return the files?

cb31

1,234 posts

151 months

Tuesday
quotequote all
dir /A-D

that should do the trick. To see the options available use dir /?

Xenoous

1,751 posts

73 months

Tuesday
quotequote all
Could also use PowerShell.

Get-ChildItem -Path C:\Path\Goes\Here -File | Select FullName

PaulV

331 posts

241 months

Yesterday (09:42)
quotequote all
DIR /S /B /A-D

Would give you the full pathname of files only within the current folder structure.

TonyRPH

13,316 posts

183 months

Yesterday (09:52)
quotequote all
I find the OPs request a bit confusing?

c:\temp\1.txt
c:\temp\2.txt
c:\temp\test\5.txt

The impression I get is that they only want to return:

1.txt
2.txt
5.txt

Without the directory / folder names?

ThingsBehindTheSun said:
<snip>

Is this possible to exclude the folders and just return the files?
If so then:


FOR /R %F IN (*) DO @IF NOT EXIST "%F\" ECHO %~nxF

** Or in a batch file:

FOR /R %%F IN (*) DO (
IF NOT EXIST "%%F\" ECHO %%~nxF
)







Edited by TonyRPH on Thursday 3rd July 09:56

MesoForm

9,471 posts

290 months

Yesterday (09:54)
quotequote all
This is the kind of thing AI is actually good at - simple commands that would take ages to search for and figure out for yourself and can be easily checked to see if it has actually given you the correct answer:




This is just Copilot in Windows 11.