0

因此,例如我需要检查文件 C:\windows\system32\whatever.dll 是否位于 c:\windows 中。是否有可能以类似的方式if not exists ...,但在这里if %file% lies in %directory%

编辑:我知道我正在寻找的文件的路径。问题归结为比较字符串,即检查目录的路径是否包含在文件路径的开头。

4

1 回答 1

2

你可以做类似的事情

if not exists %directory%\%file% 

通过这种方式,您可以创建一个完整的文件路径名,例如“c:\myfolder\yourfolder\myfile.txt”并检查它是否存在

更新
这应该可以工作(但未经测试)

:: starting folder
set RootPath=c:\myfolder\yourfolder\

::check all subfolder
for /R "%RootPath%" %%d IN (.) DO ( 
    echo %%d

    :: check all file in each subfolder
    for %%f IN ("%%~d\*.*") DO (

        :: check if your file exist 
        IF "%%~nxf"=="filenameImLookingFor.txt" (
              echo Found file here "%%~f"
        )
    )
)
  • %%~nxf 将扩展为带有扩展名的 filname,没有路径
  • “〜”还确保扩展变量永远不会包含前缀/后缀双引号,这样您就可以添加自己的双引号而不会出现意外的双引号(这显然会搞砸)

这是 FOR 变量的有用语法列表(来自 FOR 命令的帮助)

您现在可以使用以下可选语法:

%~I         - expands %I removing any surrounding quotes (")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file
%~$PATH:I   - searches the directories listed in the PATH
               environment variable and expands %I to the
               fully qualified name of the first one found.
               If the environment variable name is not
               defined or the file is not found by the
               search, then this modifier expands to the
               empty string

可以组合修饰符以获得复合结果:

%~dpI       - expands %I to a drive letter and path only
%~nxI       - expands %I to a file name and extension only
%~fsI       - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
               environment variable for %I and expands to the
               drive letter and path of the first one found.
%~ftzaI     - expands %I to a DIR like output line
于 2012-05-10T09:14:47.193 回答