我需要运行一个实用程序,只有当某个文件存在。如何在Windows批处理中做到这一点?
当前回答
if exist <insert file name here> (
rem file exists
) else (
rem file doesn't exist
)
或者在一行中(如果只需要发生一个动作):
if exist <insert file name here> <action>
例如,如果autoexec.bat文件存在,这将打开记事本:
if exist c:\autoexec.bat notepad c:\autoexec.bat
其他回答
if exist <insert file name here> (
rem file exists
) else (
rem file doesn't exist
)
或者在一行中(如果只需要发生一个动作):
if exist <insert file name here> <action>
例如,如果autoexec.bat文件存在,这将打开记事本:
if exist c:\autoexec.bat notepad c:\autoexec.bat
试试下面的例子,引用自IF /?在Windows XP上:
IF EXIST filename.txt ( del filename.txt ) ELSE ( echo filename.txt missing. )
你也可以用IF NOT EXIST检查丢失的文件。
IF命令非常强大。IF /?将奖励仔细阅读。在这个问题上,试试/?选项上的许多其他内置命令为许多隐藏的宝石。
C:\>help if
在批处理程序中执行条件处理。
IF [NOT] ERRORLEVEL number命令 IF [NOT] string1==string2命令 IF [NOT] EXIST文件名命令
推荐文章
- 在批处理文件中处理子字符串的最佳方法是什么?
- 在Sublime Text 2中使用Ctrl+D进行多个选择时,我如何跳过匹配?
- #定义WIN32_LEAN_AND_MEAN具体排除了什么?
- 如何添加一个设置路径只执行批处理文件?
- 最好的轻量级web服务器(只有静态内容)的Windows
- npm犯错!错误:EPERM:操作不允许,重命名
- 崇高的文本从命令行
- 如何保持Python脚本输出窗口打开?
- 使用.bat文件检查文件夹是否存在
- BAT文件:打开新的cmd窗口并在其中执行命令
- 如何查看Windows库(*.lib)的内容
- 在Windows中删除大文件夹最快的方法是什么?
- NPM卡住给出相同的错误EISDIR:对目录的非法操作,读取错误(本机)
- Windows 10环境变量太大
- 如何使用Windows命令行更改目录