首页 文章

如何从批处理文件中检查文件是否存在[重复]

提问于
浏览
392

这个问题在这里已有答案:

我只有在存在某个文件时才需要运行实用程序 . 如何在Windows批处理中执行此操作?

3 回答

  • 74
    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
    
  • 633

    尝试类似以下示例,引自Windows XP上 IF /? 的输出:

    IF EXIST filename. (
        del filename.
    ) ELSE (
        echo filename. missing.
    )
    

    您还可以使用 IF NOT EXIST 检查缺少的文件 .

    IF 命令功能非常强大 . IF /? 的输出将奖励仔细阅读 . 就此而言,在许多其他内置命令中尝试使用 /? 选项来获取大量隐藏的宝石 .

  • 30
    C:\>help if
    

    在批处理程序中执行条件处理 .

    IF [NOT] ERRORLEVEL数字命令IF [NOT] string1 == string2命令IF [NOT] EXIST filename命令

相关问题