如果else块在批处理文件中无法正常工作 [英] if else block is not working properly in batch file

查看:44
本文介绍了如果else块在批处理文件中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@echo off
set /p filename=please enter name of file..:

echo "%filename%"

(
 echo %filename% 
 if exist { C:\Windows\System32\%filename% }
 (
 wmic datafile where name="C:\\Windows\\System32\\%filename%" get version 
 wmic datafile where name="C:\\Windows\\System32\\%filename%" get path
 )
else
 (
 wmic datafile where name="C:\\Windows\\System32\\drivers\\%filename%" get version
 wmic datafile where name="C:\\Windows\\System32\\drivers\\%filename%" get path
 )



) >output.txt

start notepad.exe output.txt

我已经使该批处理文件搜索文件,命名文件,获取文件的版本并在文本文件中打印现有文件的路径.但是,否则块无法正常工作.帮帮我,谢谢.:-)

I have made this batch file to search files,name the files,get the version of files and print the path of the existing file in a text file.but if else block is not working properly. Help me and thanks in advance. :-)

推荐答案

A) Set/p 带有引号允许长文件名,这是一种很好的做法.
B) if exist 行中的引号由于相同的原因,加上括号在行的末尾
C))else(必须在一行上

A) Set /p has quotes to allow long filenames, as good practice.
B) if exist line has quotes for the same reason, plus the parenthesis is on the end of the line
C) ) else ( must be on one line

@echo off
set /p "filename=please enter name of file..: "

echo "%filename%"

(
 echo %filename% 
 if exist "C:\Windows\System32\%filename%" (
      echo block 1
   wmic datafile where name="C:\\Windows\\System32\\%filename%" get version 
   wmic datafile where name="C:\\Windows\\System32\\%filename%" get path
 ) else (
      echo block 2
   wmic datafile where name="C:\\Windows\\System32\\drivers\\%filename%" get version
   wmic datafile where name="C:\\Windows\\System32\\drivers\\%filename%" get path
 )

) >output.txt

start notepad.exe output.txt

这篇关于如果else块在批处理文件中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆