Batch File; List files in directory, only filenames?
Solution 1
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.
Solution 2
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
Solution 3
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.
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
Solution 4
- Why not use
where
insteaddir
?
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
-
Some further reading:
[√] Where
[√] Where sample
Solution 5
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.
Related videos on Youtube
user3558570
Updated on January 05, 2022Comments
-
user3558570 over 2 years
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
-
Helen almost 7 yearsPossible duplicate of Suppress directory names being listed with DIR
-
-
Stéphane GRILLON over 7 yearsIs OK for me :)
dir /b /a-d > tmp.txt
-
Farhan Ghumra about 6 yearsIt doesn't work with
/S
to get file names of sub-directories also. -
Stephan about 6 years@xyroid did you read the seltene
/a-d will the exclude directory names
? -
Farhan Ghumra about 6 yearsIt's not excluding if I want to print names of sub-directories' files.
-
EmpathicSage over 5 yearsThis 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
-
Behnam about 4 yearsWhat if I do not want the output file "fileslist.txt" to be included in the list?
-
afifi over 3 years@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
-
Stephan over 3 years@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), usefor /r %a in (*%yy%%mm%%dd%_*_P*.tgz) do @echo %~nxa
(some other answers here also use similarfor
loops). -
Stephan over 3 yearsJust 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). -
Io-oI almost 3 years@kakyo Thanks for comment... current path == . And all files == *
-
Io-oI almost 3 yearsThanks too, for test!
-
CAD bloke almost 3 years... above the
dir
line usedel fileslist.txt
-
Gerhard over 2 years@Behnam then exclude it using
findstr
i.edir /b /s | findstr /VI "filelist.txt" > filelist.txt"
-
Gerhard over 2 yearswhy? This is unwanted overhead. just do
for /R %%i in (*) do echo (%%~nxi)>list.txt