.bat搜索可能具有不同文件名的文件

时间:2019-01-15 21:13:28

标签: windows batch-file cmd

我正在创建一个批处理文件,如果程序存在,它将结束。但是如果它不存在,它将开始安装该程序。

在安装程序时,文件路径中会包含一个由它创建并驻留在其中的4个随机数的一部分。我不确定我的脚本是否会对此进行查找。到目前为止,我已经创建了:

@echo off 
REM === Agent already deployed on the computer? 
if exist "C:\Program Files (x86)\Citrix\Remote Support Customer\????\g2ax_comm_customer.*" goto END 
REM === Deploying agent in silent mode 
"\\location.exe" --mode unattended 
REM === Script done 
:END

我知道安装有效,所以我省略了。我对Windows命令提示符并没有做很多事情,也不确定我是否可以运行。

3 个答案:

答案 0 :(得分:0)

@echo off
setlocal
set "supportdir=C:\Program Files (x86)\Citrix\Remote Support Customer"

REM === Agent already deployed on the computer?
for /d %%A in ("%supportdir%\*") do if exist "%%~A\g2ax_comm_customer.*" exit /b
REM === Deploying agent in silent mode
"\\location.exe" --mode unattended

????路径段将无法解析,因此您可能需要迭代 使用for /d循环的未知子目录。通配符对最后一个段有效。

%%~A具有以前????的绝对路径。 现在可以测试%%~A\g2ax_comm_customer.*是否存在。

答案 1 :(得分:0)

您可以简单地递归搜索父目录。

@echo off
for /R "C:\Program Files (x86)\Citrix\Remote Support Customer" %i in (g2ax_comm_customer.*) do if not exist %%i "\\location.exe" --mode unattended

答案 2 :(得分:0)

dir可以使用/s开关搜索子文件夹。将其输出重定向到NUL,并在成功/失败时使用conditional execution

dir /s /b "C:\Program Files (x86)\Citrix\Remote Support Customer\g2ax_comm_customer.*"  >nul 2>&1 && (
  echo found the file
) || (
  echo didn't find the file
)