0

Here's snippet of the code that's giving me error!

set "inputFolder=%~dp0temp"
set "outputFolder=%~dp0temp"
set "outputFile=list.txt"
if not exist temp (
    echo Input folder does not exist!
    pause
    exit /b 1
)
pushd temp
del temp\list.txt 2>nul
for %%F in (*) do (
    echo Processing: "%%F"
    echo file '%%~fF'>> temp\list.txt
)
popd
echo Completed!
pause

I've a Main folder in which .bat file runs, now the bat file creates temp folder and adds some files in it (done in code before this) this code should create a text file which has a list of files in temp folder in this format: file 'temp folder path\filename.ext' But instead no txt file is created and it gives me the following error:

Processing: "filename" (filename is correct which is present in temp folder)
The system cannot find the patch specified.

Can you guys tell me what's wrong?

I also tried ~dp0 (which is set) thing but by doing so it fails in if statement! Then I tried direct approach and instead of using set values I used dir paths.

In short: I was trying to create a text file that lists all the files in a folder automatically.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • 1
    Just `temp` is a relative path, relative to the *current working folder*, which might not be identical to the *location of your script* (`%~dp0`). To be sure you are where you think you are, set the working folder to the script location: `cd /d "%~dp0"` – Stephan Jul 27 '23 at 15:29
  • nope it didn't work. – BlackSpectrum Jul 28 '23 at 14:39

1 Answers1

0

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 &&.

Mofi
  • 46,139
  • 17
  • 80
  • 143