批处理 - 在 for 循环搜索中传递带空格的文件名
Batch - Passing filename with spaces, in for loop search
为什么在七层地狱中这对单个单词文件名有效,但对多个单词文件名无效? ...
echo.
set /p "file=Please enter the filename, including extension: "
echo.
echo Searching for %file%, please wait...
setlocal
for %%i in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
for /f "tokens=*" %%a in ('dir /b /s /a-d %%i:\%file% 2^>nul') do (
set "found=%%a" & goto FOUND
))
echo.
echo File not found
echo.
echo Press any key to exit
pause>nul
goto :EOF
:FOUND
echo.
echo File found in %found%
for %%A in ("%found%") do (
Set folder=%%~dpA
Set name=%%~nxA
)
cd %folder%
explorer .
echo.
echo Press any key to exit
pause>nul
代码已修改,来源于网上各种来源...感谢您的帮助!
我不太确定你在这里试图完成什么(在每个驱动器的根目录下寻找指定的文件?),但你的问题是这一行:
for /f "tokens=*" %%a in ('dir /b /s /a-d %%i:\%file% 2^>nul') do (
您需要在周围加上引号:%%i:\%file%
给定一个名为 my file.txt
的文件,循环中 dir
命令的输出将是这样的:
dir /b /s /a-d c:\my file.txt 2^>nul
,这显然是无效的,因为文件名中有 space。
这将使您的脚本工作:
for /f "tokens=*" %%a in ('dir /b /s /a-d "%%i:\%file%" 2^>nul') do (
为什么在七层地狱中这对单个单词文件名有效,但对多个单词文件名无效? ...
echo.
set /p "file=Please enter the filename, including extension: "
echo.
echo Searching for %file%, please wait...
setlocal
for %%i in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
for /f "tokens=*" %%a in ('dir /b /s /a-d %%i:\%file% 2^>nul') do (
set "found=%%a" & goto FOUND
))
echo.
echo File not found
echo.
echo Press any key to exit
pause>nul
goto :EOF
:FOUND
echo.
echo File found in %found%
for %%A in ("%found%") do (
Set folder=%%~dpA
Set name=%%~nxA
)
cd %folder%
explorer .
echo.
echo Press any key to exit
pause>nul
代码已修改,来源于网上各种来源...感谢您的帮助!
我不太确定你在这里试图完成什么(在每个驱动器的根目录下寻找指定的文件?),但你的问题是这一行:
for /f "tokens=*" %%a in ('dir /b /s /a-d %%i:\%file% 2^>nul') do (
您需要在周围加上引号:%%i:\%file%
给定一个名为 my file.txt
的文件,循环中 dir
命令的输出将是这样的:
dir /b /s /a-d c:\my file.txt 2^>nul
,这显然是无效的,因为文件名中有 space。
这将使您的脚本工作:
for /f "tokens=*" %%a in ('dir /b /s /a-d "%%i:\%file%" 2^>nul') do (