196

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
1

10 Answers 10

356

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.

Sign up to request clarification or add additional context in comments.

11 Comments

Is OK for me :) dir /b /a-d > tmp.txt
It doesn't work with /S to get file names of sub-directories also.
@xyroid did you read the seltene /a-d will the exclude directory names?
It's not excluding if I want to print names of sub-directories' files.
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
|
46

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

Comments

19
  • 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

2 Comments

@kakyo Thanks for comment... current path == . And all files == *
Thanks too, for test!
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

4 Comments

What if I do not want the output file "fileslist.txt" to be included in the list?
... above the dir line use del fileslist.txt
@Behnam then exclude it using findstr i.e dir /b /s | findstr /VI "filelist.txt" > filelist.txt"
this is incorrect as an answer to the question, because it will include the directory names as well, unlike dir /b /a-d
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.

1 Comment

why? This is unwanted overhead. just do for /R %%i in (*) do echo (%%~nxi)>list.txt
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

Comments

4

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

dir | select { $_.Name }

1 Comment

I am not sure what to do now. You helped me but this question is targetting Batch not Powershell.
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

Comments

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

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

Comments

0

For some reason Premkumar's answer did not work on windows 7. Here's a batch file variant that does (by combining ~nxi from Premkumar's answer and the command from npocmaka's answer):

@echo off
for /r %%i in (.\*.mkv) do echo %%~nxi

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.