如何在AutoIt中编写这个批处理脚本?

时间:2018-05-27 10:01:42

标签: batch-file autoit ini

我正在尝试在AutoIt中重写一个批处理文件。我如何在AutoIt中执行以下操作?

:BeginLangMenu
if "%OptNum%" gtr "0" goto ShowLangMenu

::Next Section code based on code supplied by Aacini from stackoverflow.com
:DefineLangMenu
for /L %%D in (1,1,99) do (
   if exist Common\Settings\Data%%D.ini for /F "eol=# tokens=1,2 delims==" %%a in (Common\Settings\Data%%D.ini) do (

      set line=%%a
      if "!line:~2,5!" neq "clude" (
         REM Define "normal" variables, i.e. Compressor, Method, etc.
         set %%a=
      ) else if "!line:~7!" neq "" (

         REM Define the base array elements, i.e. D1IncludeAR=%%b, D1ExcludeAR=%%b, ...
         set D%%D%%a=%%b

         REM Set Show?? array elements with value equal 1, i.e. ShowAR=1, ...
         REM when anyone of DiInclude?? or DiExclude?? corresponding elements was given
         if defined D%%D%%a set Show!line:~7!=1

      )
   )
)

REM Define a list of language abbreviations, i.e. "langs=AR CZ DE ..."
REM and the corresponding language names array, i.e. lang[AR]=Arabic, ...
REM At same time, calculate original OptNum

for %%a in ("AR=Arabic" "CZ=Czech" "DE=German" "EN=English" "ES=Spanish" "ESMX=Spanish(Mexico)"
            "FR=French" "HU=Hungarian" "IT=Italian" "JP=Japanese" "KR=Korean" "PL=Polish"
            "PR=Portuguese" "PRBR=Portuguese(Brazil)" "RU=Russian" "ZH=Chinese") do (
   for /F "tokens=1,2 delims==" %%b in (%%a) do (
      set "langs=!langs! %%b"
      set "lang[%%b]=%%c"
      set /A "OptNum+=Show%%b"

   )
)

::NEXT 2 SECTIONS DISPLAYS THE LANGUAGE SELECTION MENU IF APPLICABLE
:ShowLangMenu
set /a step=%step%+1
:LangMenu
if "%OptNum%"=="0" Goto checksplit
echo %TIME:~0,2%:%TIME:~3,2%:%TIME:~6,2% - Showing Language Selection Menu >> "%workdir%Conversion.log"
REM Show the language menu
set #=0
for %%a in (%langs%) do (
   if defined Show%%a (
      set /A #+=1
rem       echo [!#!] !lang[%%a]!
      echo !lang[%%a]! >> %b2eincfilepath%\Lang.txt
      set "option[!#!]=%%a" 
   )
)


%MYFILES1%\DROPDOWNBOX.exe /F:"%b2eincfilepath%\Lang.txt" "Choose Which Language to Compress" "STEP %step%: Language Selection"  /W:280 /RI /C:13 >nul > %b2eincfilepath%\LangAnswer.txt
if %ERRORLEVEL% EQU 0 del %b2eincfilepath%\LangAnswer.txt
if exist %b2eincfilepath%\LangAnswer.txt set /p "SelectLang="<%b2eincfilepath%\LangAnswer.txt
if not defined SelectLang goto LangError
if %SelectLang%==Arabic Set LangOpt=AR
if %SelectLang%==Czech set LangOpt=CZ
if %SelectLang%==German set LangOpt=DE
if %SelectLang%==English set LangOpt=EN
if %SelectLang%==Spanish set LangOpt=ES
if %SelectLang%==Spanish(Mexico) set LangOpt=ESMX
if %SelectLang%==French set LangOpt=FR
if %SelectLang%==Hungarian set LangOpt=HU
if %SelectLang%==Italian set LangOpt=IT
if %SelectLang%==Japanese set LangOpt=JP
if %SelectLang%==Korean set LangOpt=KR
if %SelectLang%==Polish set LangOpt=PL
if %SelectLang%==Portuguese set LangOpt=PR
if %SelectLang%==Portuguese(Brazil) set LangOpt=PRBR
if %SelectLang%==Russian set LangOpt=RU
if %SelectLang%==Chinese set LangOpt=ZH
if defined SelectLang Goto LangSet

::SETS THE LANGUAGE SELECTION ACCORDING TO USER INPUT IN LANGUAGE MENU
:LangSet
set "LangIs=%LangOpt%"

到目前为止我的代码:

For $D = 1 To 99 Step +1
    if FileExists (@ScriptDir & "\Common\Settings\Data" & $D & ".ini") Then
        $aLangs = IniReadSection (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangInclude")
        If Not @error Then
            for $i = 1 to $aLangs[0][0]

问题是它还从INI文件读入数组键。我需要IncludeARIncludeEN等等-keys并相应地在下拉菜单GUI中设置语言列表。

然后它应该将选定的语言设置为变量,并将另一个相应的变量设置为INI文件中所选语言的值。

2 个答案:

答案 0 :(得分:1)

好吧,我已经设法最终创造了我想要的东西,它很粗糙但是它有效,我只是不得不避免使用IniReadSection和Arrays,我使用IniRead代替了很多If ...然后...... Else语句,这里是代码:

For $D = 1 To 99 Step +1
    if FileExists (@ScriptDir & "\Common\Settings\Data" & $D & ".ini") Then
        $ARi = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangInclude", "IncludeAR", "")
        $CZi = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangInclude", "IncludeCZ", "")
        $DEi = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangInclude", "IncludeDE", "")
        $ENi = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangInclude", "IncludeEN", "")
        $ESi = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangInclude", "IncludeES", "")
        $ESMXi = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangInclude", "IncludeESMX", "")
        $FRi = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangInclude", "IncludeFR", "")
        $HUi = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangInclude", "IncludeHU", "")
        $ITi = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangInclude", "IncludeIT", "")
        $JPi = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangInclude", "IncludeJP", "")
        $KRi = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangInclude", "IncludeKR", "")
        $PLi = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangInclude", "IncludePL", "")
        $PRi = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangInclude", "IncludePR", "")
        $PRBRi = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangInclude", "IncludePRBR", "")
        $RUi = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangInclude", "IncludeRU", "")
        $ZHi = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangInclude", "IncludeZH", "")
        $ARe = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangExclude", "ExcludeAR", "")
        $CZe = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangExclude", "ExcludeCZ", "")
        $DEe = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangExclude", "ExcludeDE", "")
        $ENe = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangExclude", "ExcludeEN", "")
        $ESe = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangExclude", "ExcludeES", "")
        $ESMXe = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangExclude", "ExcludeESMX", "")
        $FRe = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangExclude", "ExcludeFR", "")
        $HUe = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangExclude", "ExcludeHU", "")
        $ITe = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangExclude", "ExcludeIT", "")
        $JPe = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangExclude", "ExcludeJP", "")
        $KRe = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangExclude", "ExcludeKR", "")
        $PLe = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangExclude", "ExcludePL", "")
        $PRe = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangExclude", "ExcludePR", "")
        $PRBRe = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangExclude", "ExcludePRBR", "")
        $RUe = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangExclude", "ExcludeRU", "")
        $ZHe = IniRead (@ScriptDir & "\Common\Settings\Data" & $D & ".ini", "LangExclude", "ExcludeZH", "")

Global $OptNum
Global $LangOptions

If Not IsDeclared ("AR") Then
    If Not $ARe = "" Or Not $ARi = "" Then $AR = "Arabic"
EndIf

If Not IsDeclared ("CZ") Then
    If Not $CZe = "" Or Not $CZi = "" Then $CZ = "Czech"
EndIf
If Not IsDeclared ("DE") Then
    If Not $DEe = "" Or Not $DEi = "" Then $DE = "German"
EndIf
If Not IsDeclared ("EN") Then
    If Not $ENe = "" Or Not $ENi = "" Then $EN = "English"
EndIf
If Not IsDeclared ("ES") Then
    If Not $ESe = "" Or Not $ESi = "" Then $ES = "Spanish"
EndIf
If Not IsDeclared ("ESMX") Then
    If Not $ESMXe = "" Or Not $ESMXi = "" Then $ESMX = "Spanish(Mexico)"
EndIf
If Not IsDeclared ("FR") Then
    If Not $FRe = "" Or Not $FRi = "" Then $FR = "French"
EndIf
If Not IsDeclared ("HU") Then
    If Not $HUe = "" Or Not $HUi = "" Then $HU = "Hungarian"
EndIf
If Not IsDeclared ("IT") Then
    If Not $ITe = "" Or Not $ITi = "" Then $IT = "Italian"
EndIf
If Not IsDeclared ("JP") Then
    If Not $JPe = "" Or Not $JPi = "" Then $JP = "Japanese"
EndIf
If Not IsDeclared ("KR") Then
    If Not $KRe = "" Or Not $KRi = "" Then $KR = "Korean"
EndIf
If Not IsDeclared ("PL") Then
    If Not $PLe = "" Or Not $PLi = "" Then $PL = "Polish"
EndIf
If Not IsDeclared ("PR") Then
    If Not $PRe = "" Or Not $PRi = "" Then $PR = "Portuguese"
EndIf
If Not IsDeclared ("PRBR") Then
    If Not $PRBRe = "" Or Not $PRBRi = "" Then $PRBR = "Portuguese(Brazil)"
EndIf
If Not IsDeclared ("RU") Then
    If Not $RUe = "" Or Not $RUi = "" Then $RU = "Russian"
EndIf
If Not IsDeclared ("ZH") Then
    If Not $ZHe = "" Or Not $ZHi = "" Then $ZH = "Chinese"
EndIf
    EndIf
    Next
if IsDeclared ("AR") then
    $OptNum+=1
    If $OptNum = 1 Then
        $LangOptions = "Arabic"
    EndIf
EndIf
if IsDeclared ("CZ") then
    $OptNum+=1
    If $OptNum > 1 Then
        $LangOptions = $LangOptions & "|Czech"
    Else
        $LangOptions = "Czech"
    EndIf
EndIf
if IsDeclared ("DE") then
    $OptNum+=1
    If $OptNum > 1 Then
        $LangOptions = $LangOptions & "|German"
    Else
        $LangOptions = "German"
    EndIf
EndIf
if IsDeclared ("EN") then
    $OptNum+=1
    If $OptNum > 1 Then
        $LangOptions = $LangOptions & "|English"
    Else
        $LangOptions = "English"
    EndIf
EndIf
if IsDeclared ("ES") then
    $OptNum+=1
    If $OptNum > 1 Then
        $LangOptions = $LangOptions & "|Spanish"
    Else
        $LangOptions = "Spanish"
    EndIf
EndIf
if IsDeclared ("ESMX") then
    $OptNum+=1
    If $OptNum > 1 Then
        $LangOptions = $LangOptions & "|Spanish(Mexico)"
    Else
        $LangOptions = "Spanish(Mexico)"
    EndIf
EndIf
if IsDeclared ("FR") then
    $OptNum+=1
    If $OptNum > 1 Then
        $LangOptions = $LangOptions & "|French"
    Else
        $LangOptions = "French"
    EndIf
EndIf
if IsDeclared ("HU") then
    $OptNum+=1
    If $OptNum > 1 Then
        $LangOptions = $LangOptions & "|Hungarian"
    Else
        $LangOptions = "Hungarian"
    EndIf
EndIf
if IsDeclared ("IT") then
    $OptNum+=1
    If $OptNum > 1 Then
        $LangOptions = $LangOptions & "|Italian"
    Else
        $LangOptions = "Italian"
    EndIf
EndIf
if IsDeclared ("JP") then
    $OptNum+=1
    If $OptNum > 1 Then
        $LangOptions = $LangOptions & "|Japanese"
    Else
        $LangOptions = "Japanese"
    EndIf
EndIf
if IsDeclared ("KR") then
    $OptNum+=1
    If $OptNum > 1 Then
        $LangOptions = $LangOptions & "|Korean"
    Else
        $LangOptions = "Korean"
    EndIf
EndIf
if IsDeclared ("PL") then
    $OptNum+=1
    If $OptNum > 1 Then
        $LangOptions = $LangOptions & "|Polish"
    Else
        $LangOptions = "Polish"
    EndIf
EndIf
if IsDeclared ("PR") then
    $OptNum+=1
    If $OptNum > 1 Then
        $LangOptions = $LangOptions & "|Portuguese"
    Else
        $LangOptions = "Portuguese"
    EndIf
EndIf
if IsDeclared ("PRBR") then
    $OptNum+=1
    If $OptNum > 1 Then
        $LangOptions = $LangOptions & "|Portuguese(Brazil)"
    Else
        $LangOptions = "Portuguese(Brazil)"
    EndIf
EndIf
if IsDeclared ("RU") then
    $OptNum+=1
    If $OptNum > 1 Then
        $LangOptions = $LangOptions & "|Russian"
    Else
        $LangOptions = "Russian"
    EndIf
EndIf
if IsDeclared ("ZH") then
    $OptNum+=1
    If $OptNum > 1 Then
        $LangOptions = $LangOptions & "|Chinese"
    Else
        $LangOptions = "Chinese"
    EndIf
EndIf

之后检查$ OptNum,如果它大于1,它会显示带有DropDown框的GUI,该框是使用$ LangOptions创建的。

On按OK确定将所选语言转换为我选择的变量,然后我可以操作它,因为我认为需要继续前进。

答案 1 :(得分:0)

<强> main.au3

; Create test GUI with a combo and a button
GUICreate('test', 300, 300)
$iCombo = GUICtrlCreateCombo('', 5, 5, 150)
$iButton = GUICtrlCreateButton('Set Language', 5, 40)
GUISetState()

; Read languages from ini file.
$aLang = IniReadSection('test.ini', 'lang')
If @error Then
    ; Use default language on error.
    Global $aLang[][] = [[1, ''], ['EN', 'English']]
EndIf

; Prepare combo list as string.
$sComboList = ''
For $i1 = 1 To UBound($aLang) -1
    $sComboList &= $aLang[$i1][1] & '|'
Next

; Trim last | from combo string.
$sComboList = StringTrimRight($sComboList, 1)

; Update combo ctrl and set default language.
GUICtrlSetData($iCombo, $sComboList, 'English')
$sLangShortname = 'EN'

While 1
    Switch GUIGetMsg()
        Case -3 ; $GUI_EVENT_CLOSE
            Exit
        Case $iButton
            ; Read from combo ctrl.
            $sLangFullname = GUICtrlRead($iCombo)
            ; Set short name from full name.
            For $i1 = 1 To UBound($aLang) -1
                If $aLang[$i1][1] = $sLangFullname Then
                    $sLangShortname = $aLang[$i1][0]
                    ExitLoop
                EndIf
            Next
            ; Show result of current of short name and full name.
            MsgBox(0, 'Lang', _
             'Fullname: ' & $sLangFullname & @CRLF & _
             'Shortname: ' & $sLangShortname _
            )
    EndSwitch
WEnd

<强> test.ini

[lang]
AR=Arabic
CZ=Czech
DE=German
EN=English
ES=Spanish
ESMX=Spanish(Mexico)
FR=French
HU=Hungarian
IT=Italian
JP=Japanese
KR=Korean
PL=Polish
PR=Portuguese
PRBR=Portuguese(Brazil)
RU=Russian
ZH=Chinese

我不明白处理ini的所有复杂性 所以我创建了test.ini尽可能简单。

main.au3[lang]的ini部分读入2d数组。 该数组包含短名称和全名。

组合采用由|分隔的一系列项目,因此for loop创建字符串。组合使用字符串更新 将项目添加到组合中以供选择。

单击该按钮将使用短名称设置2个变量 和所选语言的长名称。 消息框将会 显示2个变量的值。