如何使用cmd组合“ if”和“ else”代码?

时间:2019-05-04 08:27:12

标签: batch-file cmd

我想为下面提供的代码创建一个ifelse语句。该代码应如下工作: 运行批处理代码时,它将查找路径是否已写入addon.cfg文件中。如果尚未写入,则批处理文件将写入路径。如果存在该路径,则它将完全删除该路径。

批处理代码如下:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Set "Key=HKLM\SOFTWARE\Lockheed Martin\Prepar3D v4"
Set "Val=SetupPath"

For %%A In (Cmd Reg)Do Set "%%A=%SystemRoot%\System32\%%A.exe"
If Defined PROCESSOR_ARCHITEW6432 Set "Cmd=%SystemRoot%\SysNative\cmd.exe"
Set "Dir="
For /F "Tokens=2*" %%A In ('
    ""%Cmd%" /D /C ""%Reg%" Query "%Key%" /V "%Val%" 2^>Nul""')Do Set "Dir=%%~B"
If Not Defined Dir GoTo :EOF
If "%Dir:~-1%"=="\" Set "Dir=%Dir:~,-1%"
REM VARMA Scenery Designs Batch Script for Prepar3D V4
REM Add the current Folder to the Addon Library (add-on.cfg)
set args=-Configure: Category=Add-on Package, Operation=Add, FileLocation=ProgramData, Path=%cd%
start "" "%Dir%\Prepar3D.exe" "%args%"

ifelse代码应如下所示:

if  addon == ""
   set args=-Configure: Category=Add-on Package, Operation=Add, FileLocation=ProgramData, Path=%cd%
else
   set args=-Configure: Category=Add-on Package, Operation=Remove, FileLocation=ProgramData, Path=%cd%

但是,我不确定""

上的 if addon == "" 中应包含哪些值/代码

[编辑/]

addon.cfg文件位于C:\ProgramData\Lockheed Martin\Prepar3D v4上。 批处理文件将在放置批处理文件的位置查找addon.xml文件(此文件将包含一些细节,Prepar3D将从中查找风景文件)。这就是在上面的代码中将路径写为Path=%cd%的原因。

addon.cfg文件中的示例代码:

[Package.0]
PATH=F:\Flight Simulators\Addon\Scenery Making Tools\WMKM Model\WMKM Airport Design\Melaka International Airport (WMKM)\P3D V4\Melaka International Airport (WMKM)
ACTIVE=true
REQUIRED=false

addon.xml文件中的示例代码:

<SimBase.Document Type="AddOnXml" version="4,0" id="add-on">
  <AddOn.Name>VARMA Scenery Designs-(WMKM) Melaka International Airport</AddOn.Name>
  <AddOn.Description>Scenery for (WMKM) Melaka International Airport by VARMA Scenery Designs</AddOn.Description>

  <!--
  add-on.xml will work "first read first in" such that the last component read will be on top.
  -->  

  <AddOn.Component>
    <Category>Scenery</Category>
    <Path>scenery</Path>
    <Name>VARMA Scenery Designs-WMKM scenery</Name>
  </AddOn.Component>

</SimBase.Document>

您可以通过一些示例向here咨询add-on configuration file的详细信息。

2 个答案:

答案 0 :(得分:0)

这是给您的代码示例:

@Echo Off
If Defined PROCESSOR_ARCHITEW6432 (
    Start "" /D "%__CD__%" "%SystemRoot%\SysNative\cmd.exe" /C "%~f0"&Exit /B)
SetLocal EnableExtensions DisableDelayedExpansion
Set "Key=HKLM\SOFTWARE\Lockheed Martin\Prepar3D v4"
Set "Val=SetupPath"
Set "Dir="
For /F "Tokens=2*" %%A In ('Reg Query "%Key%" /V "%Val%" 2^>Nul'
)Do Set "Dir=%%~B"
If Not Defined Dir GoTo :EOF
If "%Dir:~-1%"=="\" Set "Dir=%Dir:~,-1%"
Set "Cfg=Lockheed Martin\Prepar3D v4\add-ons.cfg"
Set "Pad=%~dp0"
Set "Pth=%Pad:~,-1%"
Set "Arg=-Configure: Category=Add-on Package, Path=%Pth:,=&comma;%"
Set "Act=Add"
Rem Enter your FileLocation below. [ProgramData|Local|Roaming]
Set "Lcn=ProgramData"
If /I "%Lcn%"=="ProgramData" Set "Cfg=%ProgramData%\%Cfg%"
If /I "%Lcn%"=="Local" Set "Cfg=%LocalAppData%\%Cfg%"
If /I "%Lcn%"=="Roaming" Set "Cfg=%AppData%\%Cfg%"
If Exist "%Cfg%" Find /I "PATH=%Pth%"<"%Cfg%">Nul&&Set "Act=Remove"
Set "Arg=%Arg%, FileLocation=%Lcn%, Operation=%Act%"
Start "" "%Dir%\Prepar3D.exe" "%Arg%"

请注意,我在说明中添加了Rem粗线,尽管对于此特定任务而言不是必需的,但它为您提供了机会来调整其他两个可能选项的位置

答案 1 :(得分:0)

@Echo Off
REM Batch Script for Prepar3D V4
REM Search for the installation path of Prepar3D v4 in registry
If Defined PROCESSOR_ARCHITEW6432 (
    Start "" /D "%__CD__%" "%SystemRoot%\SysNative\cmd.exe" /C "%~f0"&Exit /B)
SetLocal EnableExtensions DisableDelayedExpansion
Set "Key=HKLM\SOFTWARE\Lockheed Martin\Prepar3D v4"
Set "Val=SetupPath"
Set "Dir="
For /F "Tokens=2*" %%A In ('Reg Query "%Key%" /V "%Val%" 2^>Nul'
)Do Set "Dir=%%~B"
If Not Defined Dir GoTo :EOF
If "%Dir:~-1%"=="\" Set "Dir=%Dir:~,-1%"

REM 'Add' or 'Remove' the current Folder 'To' or 'From' the Addon Library (add-on.cfg)
find /c "%cd%" "C:\ProgramData\Lockheed Martin\Prepar3D v4\add-ons.cfg"
if %errorlevel% equ 1 goto notfound
echo found
Set args="-Configure: Category=Add-on Package, Operation=Remove, FileLocation=ProgramData, Path=%cd%"
goto done
:notfound
echo notfound
Set args="-Configure: Category=Add-on Package, Operation=Add, FileLocation=ProgramData, Path=%cd%"
goto done
:done
start "" "%Dir%\Prepar3D.exe" "%args%"