英文:
Bat file cannot get the file location
问题
以下是您提供的代码段的翻译:
set "inputFolder=%~dp0temp"
set "outputFolder=%~dp0temp"
set "outputFile=list.txt"
if not exist temp (
echo 输入文件夹不存在!
pause
exit /b 1
)
pushd temp
del temp\list.txt 2>nul
for %%F in (*) do (
echo 处理中: "%%F"
echo 文件 '%%~fF'>> temp\list.txt
)
popd
echo 完成!
pause
这段代码的目标是创建一个文本文件,其中列出了位于temp文件夹中的所有文件,格式如下:
文件 'temp文件夹路径\文件名.ext'
但是,实际上没有创建txt文件,并且出现以下错误:
处理中: "文件名"(文件名是正确的,存在于temp文件夹中)
系统找不到指定的路径。
您尝试过~dp0
(已设置)这种方法,但在if语句中失败了!然后,您尝试了直接方法,而不是使用设置的值,而是使用目录路径。
简而言之:
您试图创建一个自动列出文件夹中所有文件的文本文件。
英文:
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.
答案1
得分: 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](https://stackoverflow.com/a/60686543/3074564).
请阅读[此答案](https://stackoverflow.com/a/60686543/3074564)中的__问题__章节。
__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.
__注意:__ 在批处理文件目录的子目录“temp”中创建的文件“list.txt”也会在“list.txt”中列出。甚至可能发生另一个文件缺失,或者如果驱动器使用FAT32或exFAT而不是NTFS文件系统,则循环无限运行。输出文件应该创建在不同的目录中,或者可以使用不同的__FOR__循环,首先获取子目录“temp”中所有文件名,加载到正在处理批处理文件的“cmd.exe”的内存中,然后__FOR__处理捕获的文件名列表,如下面的两个批处理文件解决方案所示。
See also: https://stackoverflow.com/questions/31975093/
还请参阅:https://stackoverflow.com/questions/31975093/
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:
第一个批处理文件将`list.txt`创建在输入文件夹中,以一种安全的方式,不会在列表文件中列出`list.txt`:
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.
具有隐藏属性的文件也包括在列表中。将`/A-D`修改为`/A-D-H`以排除像__FOR__那样的隐藏文件。
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:
第二个批处理文件将`list.txt`创建在输入文件夹中,以一种安全的方式,不会在列表文件中列出`list.txt`:
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](https://www.howtogeek.com/235101/10-ways-to-open-the-command-prompt-in-windows-10/) 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](https://stackoverflow.com/a/25344009/3074564) which explains the conditional command operator `&&`.
要了解使用的命令及其工作原理,请打开[命令提示符](https://www.howtogeek.com/235101/10-ways-to-open-the-command-prompt-in-windows-10/)窗口,在那里执行以下命令,并完整而仔细地阅读每个命令显示的帮助页面。
+ call /?
+ del /?
+ dir /?
+ echo /?
+ endlocal /?
+ exit /?
+ for /?
+ goto /?
+ if /?
+ pause /?
+ set /?
+ setlocal /?
还请
<details>
<summary>英文:</summary>
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](https://stackoverflow.com/a/60686543/3074564).
__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: https://stackoverflow.com/questions/31975093/
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](https://www.howtogeek.com/235101/10-ways-to-open-the-command-prompt-in-windows-10/) 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](https://stackoverflow.com/a/25344009/3074564) which explains the conditional command operator `&&`.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论