There could be used following in the batch file:
set "inputFolder=%~dp0temp"
set "outputFolder=%~dp0temp"
set "outputFile=%outputFolder%\list.txt"
pushd "%inputFolder%" 2>nul && goto ProcessFiles
echo Input folder does not exist!
pause
exit /b 1
:ProcessFiles
del /A /F "%outputFile%" 2>nul
for %%G in (*) do (
echo Processing: "%%G"
echo file '%%~fG'>>"%outputFile%"
)
popd
echo Completed!
pause
It is also possible to use:
set "inputFolder=%~dp0temp"
set "outputFolder=%~dp0temp"
set "outputFile=%outputFolder%\list.txt"
if exist "%inputFolder%\" goto ProcessFiles
echo Input folder does not exist!
pause
exit /b 1
:ProcessFiles
del /A /F "%outputFile%" 2>nul
for %%G in ("%inputFolder%\*") do (
echo Processing: "%%~nxG"
echo file '%%G'>>"%outputFile%"
)
echo Completed!
pause
The main difference between the two variants is that the second one does not change the current working directory at all.
Please read the issue chapters in this answer.
Note: The file list.txt
created also in subdirectory temp
of the batch file directory will be also listed inside list.txt
. It can even happen that another file is missing instead in list.txt
or the loop is endless running if the drive is using FAT32 or exFAT instead of NTFS as file system. The output file should be created in a different directory or a different FOR loop is used which first gets all files names in the subdirectory temp
loaded into memory of cmd.exe
processing the batch file and then FOR processes this captured list of file names as shown below with the next two batch file solutions.
See also: At which point does `for` or `for /R` enumerate the directory (tree)?
The first batch file with list.txt
created also in input folder in a fail-safe manner not listing list.txt
in the list file:
set "inputFolder=%~dp0temp"
set "outputFolder=%~dp0temp"
set "outputFile=%outputFolder%\list.txt"
pushd "%inputFolder%" 2>nul && goto ProcessFiles
echo Input folder does not exist!
pause
exit /b 1
:ProcessFiles
del /A /F "%outputFile%" 2>nul
for /F "eol=| delims=" %%G in ('dir /A-D /B /ON 2^>nul') do (
echo Processing: "%%G"
echo file '%%~fG'>>"%outputFile%"
)
popd
echo Completed!
pause
Files with hidden attribute are also included in the list. Modify /A-D
to /A-D-H
to exclude hidden files like FOR does it.
The second batch file with list.txt
created also in input folder in a fail-safe manner not listing list.txt
in the list file:
set "inputFolder=%~dp0temp"
set "outputFolder=%~dp0temp"
set "outputFile=%outputFolder%\list.txt"
if exist "%inputFolder%\" goto ProcessFiles
echo Input folder does not exist!
pause
exit /b 1
:ProcessFiles
del /A /F "%outputFile%" 2>nul
for /F "eol=| delims=" %%G in ('dir "%inputFolder%\*" /A-D /B /ON 2^>nul') do (
echo Processing: "%%~nxG"
echo file '%%G'>>"%outputFile%"
)
echo Completed!
pause
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
call /?
del /?
dir /?
echo /?
endlocal /?
exit /?
for /?
goto /?
if /?
pause /?
set /?
setlocal /?
See also: Single line with multiple commands using Windows batch file which explains the conditional command operator &&
.