简体   繁体   中英

Batch File; List files in directory, only filenames?

This is probably a very simple question, but I'm having trouble with it. Basically, 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

Thanks in advance!

The full command is:

dir /b /a-d

Let me break it up;

Basically the /b is what you look for.

/ad will exclude the directory names.


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

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

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: 在此处输入图片说明

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

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.

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 /ad > movie_list.txt

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM