This tutorials explains about list files in a directory.
dir command used to list files and folders in a directory.
/b option bare format allows you to list only files and directories names.
It display file and directory names in a current directory, not subdirectories names.
src is a directory name, remaining all are file names.
To exclude a directories names, use
dir /b /a-d
Output only file names.
TO print file names inside a sub folder, use
dir /b /s
Batch script to print the file names
For writing a script file
- Open any text editor or VSCode
- Create a new file and name the file as info.bat
There are multiple ways we can print the file names
- using dir command
use the dir command and result of the filenames piped to filename.
dir /b /a-d > filenames.txt
dir /b "c:/work" /a-d > worknames.txt
On running the info.bat file,
@echo off: commands execution does not print the commands
first dir command, creates
filenames.txtcreated , contains file names in a current directory where script executes
second dir command,
worknames.txtcreated with filenames list in the directory specified(
using for loop
for loop in batch used to iterate each file in a directory and print the file name
for %%I in ("%folder%\*") do (
Following are steps
- Current directory is set to
.in a variable folder
- Iterate each file name in a folder give,
%%Iis a varialbe to points to file object
%%~nxIused to print the file name, n is the file name, x contains extension.
%%~nIto get only file name without extension.
- you can change folder variable point to any other folder that can be path to an directory.