【问题标题】:CMD: Checking if a specific file is absent in a specific folderCMD:检查特定文件夹中是否缺少特定文件
【发布时间】:2017-05-14 07:42:06
【问题描述】:

如何检查文件夹是否缺少特定文件?我认为这将是if not exist X (execute Y) 和路径名的某种组合,但我无法提出任何解决方案。

来自if /?

EXIST filename      Specifies a true condition if the specified filename exists.

不确定我会将文件夹的路径放在哪里。

基本上我要问的是我如何Check if any type of files exist in a directory using BATCH script,但只有特定文件夹中的特定文件名? (然后在前面放一个not

提前谢谢你。

【问题讨论】:

    标签: batch-file cmd path directory


    【解决方案1】:

    要安全地检查文件,请使用以下命令(基于dir 命令和redirection):

    > nul 2>&1 dir /A:-D "D:\path\to\folder\file.ext" || echo File is missing!
    

    要检查文件夹,您可以使用:

    > nul 2>&1 dir /A:D "D:\path\to\folder" || echo Folder is missing!
    

    为了完整起见:

    要检查文件夹,请将尾部\附加到路径中,例如

    if not exist "D:\path\to\folder\" echo Folder is missing!
    

    要检查该文件夹中的特定文件,以下内容不安全...:

    if not exist "D:\path\to\folder\file.ext" echo File is missing!
    

    ...,因为这将 not 匹配名为 file.ext 的文件夹。

    【讨论】:

      【解决方案2】:
      if not exist "d:\path to\filename.ext" (dothis)
      

      如果我正确理解您的目的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-26
        • 2021-07-09
        • 1970-01-01
        • 2018-11-17
        • 1970-01-01
        • 2015-03-04
        相关资源
        最近更新 更多