【发布时间】:2018-03-13 21:31:09
【问题描述】:
我的目标是列出 PC 上的本地硬盘并在每个硬盘上搜索特定文件。如果找到,将文件的路径输出到名为“PCNAME.txt”的文本文件。我的期望如下:
set filename="abc.exe"
set uncpath="%userprofile%\desktop\"
set fullpath=%uncpath%\%computername%.txt
for /f %%a in ('wmic logicaldisk where "description='Local Fixed Disk'" get Caption ^| find ":"') do (
set letter=%%a
cd %letter%\
for /R %%f in (%filename%) do @IF EXIST %%f @echo File Location^(s^) Identified: >> %fullpath% & goto :search
:search
for /R %%f in (%filename%) do @IF EXIST %%f set filenew=%%f & call :trim
echo. >> %fullpath%
exit /b
:trim
set filenew=%filenew:"=%
@echo %filenew% >> %fullpath%
)
当删除第一个 FOR 循环并将当前工作目录更改为 C:\ 驱动器时,上述脚本可以正常工作。但我希望它在所有本地硬盘驱动器上运行。经过大量的谷歌搜索,我想这种方法可能是使用 DelayedExpansion。我也明白 FOR 循环内的 goto 正在取消循环。但我无法完全修复脚本。
我还想知道是否可以完全消除“修剪”标签,而是使用 DelayedExpansion 或其他方法直接在“搜索”标签下删除双引号。任何帮助将不胜感激。
【问题讨论】:
-
Dir /b /s C:\filename.exe搜索硬盘。Dir /b /s C:\filename.exe > "%userprofile%\desktop\%computername%.txt"。您只需要循环来获取驱动器号。
标签: windows batch-file for-loop nested-loops