如何通过windows xp中的命令行检查给定文件是否位于给定目录下的任何位置?

时间:2012-05-10 09:00:43

标签: command-line batch-file windows-xp cmd

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

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

1 个答案:

答案 0 :(得分: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,不带路径
  • “〜”make也确保扩展变量永远不会包含 前缀/后缀doublequte,以便您可以添加自己的 有意想不到的双重引用(这显然会弄乱东西 向上)

以下是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