180

This is probably a very simple question, but I'm having trouble with it.

I am trying to write a Batch File and I need it to list all the files in a certain directory. The dir command will do this, but it also gives a bunch of other information; I want it to list ONLY the file names and exclude anything else.

I just want the output to look like this:

file1.txt
file2.txt
file3.txt
Rafael Tavares
  • 5,678
  • 4
  • 32
  • 48
user3558570
  • 1,803
  • 2
  • 12
  • 4
  • Possible duplicate of [Suppress directory names being listed with DIR](https://stackoverflow.com/questions/7164184/suppress-directory-names-being-listed-with-dir) – Helen Jun 06 '17 at 15:30

9 Answers9

332

The full command is:

dir /b /a-d

Let me break it up;

Basically the /b is what you look for.

/a-d will exclude the directory names.


For more information see dir /? for other arguments that you can use with the dir command.

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • 11
    Is OK for me :) `dir /b /a-d > tmp.txt` – Stéphane GRILLON Dec 27 '16 at 21:16
  • 4
    It doesn't work with `/S` to get file names of sub-directories also. – Farhan Ghumra Mar 05 '18 at 17:49
  • 1
    @xyroid did you read the seltene `/a-d will the exclude directory names`? – Stephan Mar 05 '18 at 18:26
  • 4
    It's not excluding if I want to print names of sub-directories' files. – Farhan Ghumra Mar 06 '18 at 08:52
  • 2
    This was very close to what I needed. I needed the absolute paths and recursion into subdirectories. Here is what I used: `dir /B /A-D /S` – EmpathicSage Dec 26 '18 at 20:52
  • @Stephan, i try to find multiple file, but when echo the output, the result still showing the path after using /A-D. My code `set /p yy=Please enter cycle Year (YYYY) : set /p mm=Please enter cycle Month (MM) : set /p dd=Please enter cycle Date (DD) : cd /d E:\ rem count the files dir /b *%yy%%mm%%dd%_*_P*.tgz /s 2> nul | find "" /v /c > %temp%\count set /p _count=<%temp%\count rem cleanup del %temp%\count rem output the number of files echo Files found : %_count% dir /B /A-D *%yy%%mm%%dd%_*_P*.tgz /s` – afifi Feb 01 '21 at 12:20
  • @afifi That's because you use `/s` (that's called FQFN (Full Qualified File Name)). How else would you distinguish files with the same name in different folders? If you really need only the names recursive (think twice about it), use `for /r %a in (*%yy%%mm%%dd%_*_P*.tgz) do @echo %~nxa` (some other answers here also use similar `for` loops). – Stephan Feb 01 '21 at 16:26
  • Just to clarify (as some comments complain about "folders do show"): the command in my answer **does** exclude folders. It shows files only. That it shows the files (note: no folders, just files) **including** their path, is a completely different thing and only occurs when `/s` is used (recursive). – Stephan Feb 01 '21 at 16:26
  • hi @Stephan there any alternative to generate filepath? I am using the one you wrote and take 3 minutes for around 20000 files.. seems to much .. – Ionut Bejinariu Jan 22 '23 at 17:07
  • I also used PowerShell, same .. its slow.. – Ionut Bejinariu Jan 22 '23 at 18:36
  • I found robocopy very very fast.. but i cannot make export with `file '%path%'` someone know how? with robocopy? – Ionut Bejinariu Jan 22 '23 at 21:06
45

You can also try this:

for %%a in (*) do echo %%a

Using a for loop, you can echo out all the file names of the current directory.

To print them directly from the console:

for %a in (*) do @echo %a
npocmaka
  • 55,367
  • 18
  • 148
  • 187
17
  • Why not use where instead dir?

In command line:

for /f tokens^=* %i in ('where .:*')do @"%~nxi"

In bat/cmd file:

@echo off 

for /f tokens^=* %%i in ('where .:*')do %%~nxi
  • Output:

file_0003.xlsx
file_0001.txt
file_0002.log
where .:*
  • Output:

G:\SO_en-EN\Q23228983\file_0003.xlsx
G:\SO_en-EN\Q23228983\file_0001.txt
G:\SO_en-EN\Q23228983\file_0002.log

For recursively:

where /r . *
  • Output:

G:\SO_en-EN\Q23228983\file_0003.xlsx
G:\SO_en-EN\Q23228983\file_0001.txt
G:\SO_en-EN\Q23228983\file_0002.log
G:\SO_en-EN\Q23228983\Sub_dir_001\file_0004.docx
G:\SO_en-EN\Q23228983\Sub_dir_001\file_0005.csv
G:\SO_en-EN\Q23228983\Sub_dir_001\file_0006.odt
  • For loop get path and name:

  • In command line:
for /f tokens^=* %i in ('where .:*')do @echo/ Path: %~dpi ^| Name: %~nxi
  • In bat/cmd file:
@echo off 

for /f tokens^=* %%i in ('where .:*')do echo/ Path: %%~dpi ^| Name: %%~nxi
  • Output:

 Path: G:\SO_en-EN\Q23228983\ | Name: file_0003.xlsx
 Path: G:\SO_en-EN\Q23228983\ | Name: file_0001.txt
 Path: G:\SO_en-EN\Q23228983\ | Name: file_0002.log

  • For loop get path and name recursively:

In command line:

for /f tokens^=* %i in ('where /r . *')do @echo/ Path: %~dpi ^| Name: %~nxi

In bat/cmd file:

@echo off 

for /f tokens^=* %%i in ('where /r . *')do echo/ Path: %%~dpi ^| Name: %%~nxi
  • Output:

 Path: G:\SO_en-EN\Q23228983\ | Name: file_0003.xlsx
 Path: G:\SO_en-EN\Q23228983\ | Name: file_0001.txt
 Path: G:\SO_en-EN\Q23228983\ | Name: file_0002.log
 Path: G:\SO_en-EN\Q23228983\Sub_dir_001\ | Name: file_0004.docx
 Path: G:\SO_en-EN\Q23228983\Sub_dir_001\ | Name: file_0005.csv
 Path: G:\SO_en-EN\Q23228983\Sub_dir_001\ | Name: file_0006.odt

Io-oI
  • 2,514
  • 3
  • 22
  • 29
14

1.Open notepad

2.Create new file

3.type bellow line

dir /b > fileslist.txt

4.Save "list.bat"

Thats it. now you can copy & paste this "list.bat" file any of your folder location and double click it, it will create a "fileslist.txt" along with that directory folder and file name list.

Sample Output: enter image description here

Note: If you want create file name list along with sub folder, then you can create batch file with bellow code.

dir /b /s > fileslist.txt
Litisqe Kumar
  • 2,512
  • 4
  • 26
  • 40
Premkumar Manipillai
  • 2,121
  • 23
  • 24
8

If you need the subdirectories too you need a "dir" command and a "For" command

dir /b /s DIRECTORY\*.* > list1.txt

for /f "tokens=*" %%A in (list1.txt) do echo %%~nxA >> list.txt

del list1.txt

put your root directory in dir command. It will create a list1.txt with full path names and then a list.txt with only the file names.

Mike_Gre
  • 81
  • 1
  • 3
4

create a batch-file with the following code:

dir %1 /b /a-d > list.txt

Then drag & drop a directory on it and the files inside the directory will be listed in list.txt

Dennis
  • 41
  • 1
3

Windows 10:

  1. open cmd

  2. change directory where you want to create text file(movie_list.txt) for the folder (d:\videos\movies)

  3. type following command

    d:\videos\movies> dir /b /a-d > movie_list.txt

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
nanthaa.ks
  • 71
  • 3
2

In Powershell dir uses Get-ChildItem, which you can filter using Powershell commands:

dir | select { $_.Name }

Stephan
  • 53,940
  • 10
  • 58
  • 91
freethinker6
  • 301
  • 3
  • 12
1

dir /s/d/a:-d "folderpath*.*" > file.txt

And, lose the /s if you do not need files from subfolders