Bat文件无法获取文件位置。

huangapple go评论115阅读模式
英文:

Bat file cannot get the file location

问题

以下是您提供的代码段的翻译:

  1. set "inputFolder=%~dp0temp"
  2. set "outputFolder=%~dp0temp"
  3. set "outputFile=list.txt"
  4. if not exist temp (
  5. echo 输入文件夹不存在!
  6. pause
  7. exit /b 1
  8. )
  9. pushd temp
  10. del temp\list.txt 2>nul
  11. for %%F in (*) do (
  12. echo 处理中: "%%F"
  13. echo 文件 '%%~fF'>> temp\list.txt
  14. )
  15. popd
  16. echo 完成!
  17. pause

这段代码的目标是创建一个文本文件,其中列出了位于temp文件夹中的所有文件,格式如下:
文件 'temp文件夹路径\文件名.ext'
但是,实际上没有创建txt文件,并且出现以下错误:

  1. 处理中: "文件名"(文件名是正确的,存在于temp文件夹中)
  2. 系统找不到指定的路径。

您尝试过~dp0(已设置)这种方法,但在if语句中失败了!然后,您尝试了直接方法,而不是使用设置的值,而是使用目录路径。

简而言之:
您试图创建一个自动列出文件夹中所有文件的文本文件。

英文:

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

  1. set "inputFolder=%~dp0temp"
  2. set "outputFolder=%~dp0temp"
  3. set "outputFile=list.txt"
  4. if not exist temp (
  5. echo Input folder does not exist!
  6. pause
  7. exit /b 1
  8. )
  9. pushd temp
  10. del temp\list.txt 2>nul
  11. for %%F in (*) do (
  12. echo Processing: "%%F"
  13. echo file '%%~fF'>> temp\list.txt
  14. )
  15. popd
  16. echo Completed!
  17. 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:

  1. Processing: "filename" (filename is correct which is present in temp folder)
  2. 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

以下是批处理文件中的翻译部分:

  1. There could be used following in the batch file:
  2. 可以在批处理文件中使用以下内容:
  3. set "inputFolder=%~dp0temp"
  4. set "outputFolder=%~dp0temp"
  5. set "outputFile=%outputFolder%\list.txt"
  6. pushd "%inputFolder%" 2>nul && goto ProcessFiles
  7. echo Input folder does not exist!
  8. pause
  9. exit /b 1
  10. :ProcessFiles
  11. del /A /F "%outputFile%" 2>nul
  12. for %%G in (*) do (
  13. echo Processing: "%%G"
  14. echo file '%%~fG'>>"%outputFile%"
  15. )
  16. popd
  17. echo Completed!
  18. pause
  1. It is also possible to use:
  2. 也可以使用以下方式:
  3. set "inputFolder=%~dp0temp"
  4. set "outputFolder=%~dp0temp"
  5. set "outputFile=%outputFolder%\list.txt"
  6. if exist "%inputFolder%\" goto ProcessFiles
  7. echo Input folder does not exist!
  8. pause
  9. exit /b 1
  10. :ProcessFiles
  11. del /A /F "%outputFile%" 2>nul
  12. for %%G in ("%inputFolder%\*") do (
  13. echo Processing: "%%~nxG"
  14. echo file '%%G'>>"%outputFile%"
  15. )
  16. echo Completed!
  17. pause
  1. The main difference between the two variants is that the second one does not change the current working directory at all.
  2. 两个变种之间的主要区别是第二个不会更改当前工作目录。
  3. Please read the __issue__ chapters in [this answer](https://stackoverflow.com/a/60686543/3074564).
  4. 请阅读[此答案](https://stackoverflow.com/a/60686543/3074564)中的__问题__章节。
  5. __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.
  6. __注意:__ 在批处理文件目录的子目录“temp”中创建的文件“list.txt”也会在“list.txt”中列出。甚至可能发生另一个文件缺失,或者如果驱动器使用FAT32exFAT而不是NTFS文件系统,则循环无限运行。输出文件应该创建在不同的目录中,或者可以使用不同的__FOR__循环,首先获取子目录“temp”中所有文件名,加载到正在处理批处理文件的“cmd.exe”的内存中,然后__FOR__处理捕获的文件名列表,如下面的两个批处理文件解决方案所示。
  7. See also: https://stackoverflow.com/questions/31975093/
  8. 还请参阅:https://stackoverflow.com/questions/31975093/
  9. 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:
  10. 第一个批处理文件将`list.txt`创建在输入文件夹中,以一种安全的方式,不会在列表文件中列出`list.txt`
  11. set "inputFolder=%~dp0temp"
  12. set "outputFolder=%~dp0temp"
  13. set "outputFile=%outputFolder%\list.txt"
  14. pushd "%inputFolder%" 2>nul && goto ProcessFiles
  15. echo Input folder does not exist!
  16. pause
  17. exit /b 1
  18. :ProcessFiles
  19. del /A /F "%outputFile%" 2>nul
  20. for /F "eol=| delims=" %%G in ('dir /A-D /B /ON 2^>nul') do (
  21. echo Processing: "%%G"
  22. echo file '%%~fG'>>"%outputFile%"
  23. )
  24. popd
  25. echo Completed!
  26. pause
  1. 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.
  2. 具有隐藏属性的文件也包括在列表中。将`/A-D`修改为`/A-D-H`以排除像__FOR__那样的隐藏文件。
  3. 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:
  4. 第二个批处理文件将`list.txt`创建在输入文件夹中,以一种安全的方式,不会在列表文件中列出`list.txt`
  5. set "inputFolder=%~dp0temp"
  6. set "outputFolder=%~dp0temp"
  7. set "outputFile=%outputFolder%\list.txt"
  8. if exist "%inputFolder%\" goto ProcessFiles
  9. echo Input folder does not exist!
  10. pause
  11. exit /b 1
  12. :ProcessFiles
  13. del /A /F "%outputFile%" 2>nul
  14. for /F "eol=| delims=" %%G in ('dir "%inputFolder%\*" /A-D /B /ON 2^>nul') do (
  15. echo Processing: "%%~nxG"
  16. echo file '%%G'>>"%outputFile%"
  17. )
  18. echo Completed!
  19. pause
  1. 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.
  2. + call /?
  3. + del /?
  4. + dir /?
  5. + echo /?
  6. + endlocal /?
  7. + exit /?
  8. + for /?
  9. + goto /?
  10. + if /?
  11. + pause /?
  12. + set /?
  13. + setlocal /?
  14. See also: [Single line with multiple commands using Windows batch file](https://stackoverflow.com/a/25344009/3074564) which explains the conditional command operator `&&`.
  15. 要了解使用的命令及其工作原理,请打开[命令提示符](https://www.howtogeek.com/235101/10-ways-to-open-the-command-prompt-in-windows-10/)窗口,在那里执行以下命令,并完整而仔细地阅读每个命令显示的帮助页面。
  16. + call /?
  17. + del /?
  18. + dir /?
  19. + echo /?
  20. + endlocal /?
  21. + exit /?
  22. + for /?
  23. + goto /?
  24. + if /?
  25. + pause /?
  26. + set /?
  27. + setlocal /?
  28. 还请
  29. <details>
  30. <summary>英文:</summary>
  31. There could be used following in the batch file:
  32. set &quot;inputFolder=%~dp0temp&quot;
  33. set &quot;outputFolder=%~dp0temp&quot;
  34. set &quot;outputFile=%outputFolder%\list.txt&quot;
  35. pushd &quot;%inputFolder%&quot; 2&gt;nul &amp;&amp; goto ProcessFiles
  36. echo Input folder does not exist!
  37. pause
  38. exit /b 1
  39. :ProcessFiles
  40. del /A /F &quot;%outputFile%&quot; 2&gt;nul
  41. for %%G in (*) do (
  42. echo Processing: &quot;%%G&quot;
  43. echo file &#39;%%~fG&#39;&gt;&gt;&quot;%outputFile%&quot;
  44. )
  45. popd
  46. echo Completed!
  47. pause
  48. It is also possible to use:
  49. set &quot;inputFolder=%~dp0temp&quot;
  50. set &quot;outputFolder=%~dp0temp&quot;
  51. set &quot;outputFile=%outputFolder%\list.txt&quot;
  52. if exist &quot;%inputFolder%\&quot; goto ProcessFiles
  53. echo Input folder does not exist!
  54. pause
  55. exit /b 1
  56. :ProcessFiles
  57. del /A /F &quot;%outputFile%&quot; 2&gt;nul
  58. for %%G in (&quot;%inputFolder%\*&quot;) do (
  59. echo Processing: &quot;%%~nxG&quot;
  60. echo file &#39;%%G&#39;&gt;&gt;&quot;%outputFile%&quot;
  61. )
  62. echo Completed!
  63. pause
  64. The main difference between the two variants is that the second one does not change the current working directory at all.
  65. Please read the __issue__ chapters in [this answer](https://stackoverflow.com/a/60686543/3074564).
  66. __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.
  67. See also: https://stackoverflow.com/questions/31975093/
  68. 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:
  69. set &quot;inputFolder=%~dp0temp&quot;
  70. set &quot;outputFolder=%~dp0temp&quot;
  71. set &quot;outputFile=%outputFolder%\list.txt&quot;
  72. pushd &quot;%inputFolder%&quot; 2&gt;nul &amp;&amp; goto ProcessFiles
  73. echo Input folder does not exist!
  74. pause
  75. exit /b 1
  76. :ProcessFiles
  77. del /A /F &quot;%outputFile%&quot; 2&gt;nul
  78. for /F &quot;eol=| delims=&quot; %%G in (&#39;dir /A-D /B /ON 2^&gt;nul&#39;) do (
  79. echo Processing: &quot;%%G&quot;
  80. echo file &#39;%%~fG&#39;&gt;&gt;&quot;%outputFile%&quot;
  81. )
  82. popd
  83. echo Completed!
  84. pause
  85. 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.
  86. 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:
  87. set &quot;inputFolder=%~dp0temp&quot;
  88. set &quot;outputFolder=%~dp0temp&quot;
  89. set &quot;outputFile=%outputFolder%\list.txt&quot;
  90. if exist &quot;%inputFolder%\&quot; goto ProcessFiles
  91. echo Input folder does not exist!
  92. pause
  93. exit /b 1
  94. :ProcessFiles
  95. del /A /F &quot;%outputFile%&quot; 2&gt;nul
  96. for /F &quot;eol=| delims=&quot; %%G in (&#39;dir &quot;%inputFolder%\*&quot; /A-D /B /ON 2^&gt;nul&#39;) do (
  97. echo Processing: &quot;%%~nxG&quot;
  98. echo file &#39;%%G&#39;&gt;&gt;&quot;%outputFile%&quot;
  99. )
  100. echo Completed!
  101. pause
  102. 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.
  103. + `call /?`
  104. + `del /?`
  105. + `dir /?`
  106. + `echo /?`
  107. + `endlocal /?`
  108. + `exit /?`
  109. + `for /?`
  110. + `goto /?`
  111. + `if /?`
  112. + `pause /?`
  113. + `set /?`
  114. + `setlocal /?`
  115. See also: [Single line with multiple commands using Windows batch file](https://stackoverflow.com/a/25344009/3074564) which explains the conditional command operator `&amp;&amp;`.
  116. </details>

huangapple
  • 本文由 发表于 2023年7月27日 23:14:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781176.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定