所以例如我需要检查文件C:\ windows \ system32 \ whatever.dll是否位于c:\ windows中。是否有可能以if not exists ...
的方式,但if %file% lies in %directory%
?
编辑:我知道我正在寻找的文件的路径。问题归结为比较字符串,即检查目录的路径是否包含在文件路径的开头。
答案 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"
)
)
)
以下是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