0

批处理文件:

@echo off

echo.
echo Verifying existence of File

for %%d 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 (
  if exist %%d:\dir1\dir2\dir3\file1 (
     set BDCPATH=%%d:\dir1\dir2\dir3\file1
    ) else if exist %%d:\dir1_2\dir2\dir3\file1 (
     set BDCPATH=%%d:\dir1_2\dir2\dir3\file1
    )
)

echo %BDCPATH%
echo %BDCPATH%

IF NOT EXIST %BCDPATH% echo %BCDPATH%

goto :eof

当我回显 '%BDCPATH% 变量时,它会取出驱动器号。你能解释一下为什么会发生这种情况并解决这个问题吗?

命令输出:

c:\Tools\KDNET_Helper>C:\Users\c_jamesp\Desktop\test1.bat

Verifying existence of BCD File
i:\dir1\dir2\dir3\file1
i:\dir1\dir2\dir3\file1
dir1\dir2\dir3\file1
4

2 回答 2

1

试试这个:引号解决了某些路径名的问题,括号改变了。

请注意,如果这些路径\文件都不存在,则不会设置该变量。

@echo off

echo.
echo Verifying existence of File
set "bcdpath="
for %%d 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 (
  if exist "%%d:\dir1\dir2\dir3\file1" (
     set "BDCPATH=%%d:\dir1\dir2\dir3\file1"
    ) else ( 
     if exist "%%d:\dir1_2\dir2\dir3\file1" set "BDCPATH=%%d:\dir1_2\dir2\dir3\file1"
   )
 )

echo "%BDCPATH%"
echo "%BDCPATH%"

IF NOT EXIST "%BCDPATH%" echo "%BCDPATH%"

if not defined bcdpath echo no files found
pause
goto :eof
于 2013-08-23T05:34:35.813 回答
0

不错的猜测,但是通过花更多时间玩耍,我发现了两件事:

@echo off

echo.
echo Verifying existence of File

for %%d 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 (
  if exist %%d:\dir1\dir2\dir3\file1 (
     set BDCPATH=%%d:\dir1\dir2\dir3\file1
    ) else if exist %%d:\dir1_2\dir2\dir3\file1 (
     set BDCPATH=%%d:\dir1_2\dir2\dir3\file1
    )
)



echo %BDCPATH%
echo %BDCPATH%

IF NOT EXIST %BDCPATH% echo %BDCPATH%


:eof
  1. 我有%BCDPATH%而不是%BDCPATH%
  2. 它应该只是goto somename不是goto :somename
于 2013-08-24T02:57:33.610 回答