Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 如果存在';路径';不';不要在室内工作_Batch File_Cmd - Fatal编程技术网

Batch file 如果存在';路径';不';不要在室内工作

Batch file 如果存在';路径';不';不要在室内工作,batch-file,cmd,Batch File,Cmd,下面列举了驱动器号,如果它们存在,它将按预期打印和工作: echo off echo test 1 for /f "skip=1 delims=" %%x in ('wmic logicaldisk get caption ^| findstr "."') do ( cmd /c "if exist %%x echo %%x" ) 现在,我想在%%x中测试现有文件,例如C:\test.txt: echo off echo test 2 for /f "skip=1 delims=" %

下面列举了驱动器号,如果它们存在,它将按预期打印和工作:

echo off
echo test 1
for /f "skip=1 delims=" %%x in ('wmic logicaldisk get caption ^| findstr "."') do (
    cmd /c "if exist %%x echo %%x"
)
现在,我想在
%%x
中测试现有文件,例如
C:\test.txt

echo off
echo test 2
for /f "skip=1 delims=" %%x in ('wmic logicaldisk get caption ^| findstr "."') do (
    cmd /c "if exist %%x\test.txt echo %%x"
)
但它不起作用,而是直接打开文件

问题:

如何使用
if exist
测试
for/f
循环中的现有文件

@Squashman

我根据你的建议将以下内容粘贴到一个新的批处理文件中,但没有打印出来

@echo off
for /f "skip=1 delims=" %%x in ('wmic logicaldisk get caption ^| findstr "."') do (
    if exist %%x\test.txt echo %%x
)

问题来自WMIC输出。您在屏幕上看不到它,但输出是Unicode,还输出额外的CRLF。因此,您需要将输出输入到另一个命令中,以便命令从本质上清理它

@echo off
echo test 2
for /f "skip=1 delims=" %%x in ('wmic logicaldisk get caption') do (
    for /F %%H in ("%%x") do if exist %%H\test.txt echo %%H
)

摆脱
CMD/C
。这是不必要的。应该是
if exist“%%x\test.txt”echo%%x
我已经尝试过了,它不起作用,什么都不会打印出来。您的代码不会重现您描述的问题。如果它正在打开文件而不是将文件名打印到屏幕上,则您的代码缺少执行命令的
echo
。我已根据您的评论更新了我的问题,但没有效果。不,它确实重现了我描述的问题。这非常有效,非常感谢您的时间!