微剋多資訊

 找回密碼
 註冊

Sign in with google

Google帳號登入

搜索

簽到天數: 8

該用戶今日未簽到

升級   20%

跳轉到指定樓層
主題
發表於 2022-9-26 00:44 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式




第一次發文。
這個是大約半年前的小工具
敝公司服務客戶眾多,也因權限鎖很死
故這個腳本工具是為了搭配我先前改良官方論壇Software Installer 工具而生
我是利用很多bat去執行提升權限安裝
而這個工具其實就只是方便我作業

也許 會有路過的人需要,本人AUTOIT也沒有研究太深
只能寫出這簡單功能


這CODE裡面主要亮點應該也就是引用前人的正則UDF下去寫
讓這工具能發揮最大效用


話說這裡沒有新文好久了
  1. #include <file.au3>
  2. #include <array.au3>
  3. #include <WindowsConstants.au3>
  4. #include <GUIConstantsEx.au3>
  5. #include <MsgBoxConstants.au3>
  6. #include <ButtonConstants.au3>
  7. #include <EditConstants.au3>
  8. #include <WinAPIIcons.au3>

  9. #pragma compile(Out, Edit_Bat_Tool.exe)
  10. #pragma compile(Icon, 'icoEdit_bat.ico'); 設定 icon 路徑
  11. #pragma compile(x64, True)
  12. #pragma compile(UPX, True)
  13. #pragma compile(ProductName, Edit Bat Tool For )
  14. #pragma compile(ProductVersion, 1.1)
  15. #pragma compile(FileVersion, 1.1)
  16. #pragma compile(FileDescription, Edit Bat Tool For  BAT快速修改新增程式))
  17. #pragma compile(ExecLevel, highestavailable)
  18. #pragma compile(LegalCopyright, c paician or paicianer)
  19. #pragma compile(LegalTrademarks, '"by paician or paicianer')
  20. #pragma compile(CompanyName, '')


  21. $user = @SystemDir & "user32.dll"
  22. $shell = @SystemDir & "shell32.dll"
  23. $icoD = -4
  24. Global $sVersion = "V1.1"
  25. Global $iLogOnFlag = 0
  26. Global $sParameters = ""
  27. Global $Msg
  28. _CheckUser()
  29. Func _CheckUser()

  30. Local $sMsg = ""

  31. Switch @error
  32. Case 0
  33.    ;簡單用當前使用者帳號來區別才可使用
  34.           Switch @UserName
  35.                   Case "d.duke" To "d.duke"
  36.                           $sMsg = "OK"
  37.                           Sleep(500)

  38.                   Case Else
  39.                           $sMsg = MsgBox(262144,"提示","未經許可使用者無法使用此功能")
  40.                           Exit
  41.           EndSwitch
  42.   Case Else
  43.           $sMsg = "Something went horribly wrong."
  44. EndSwitch
  45. MsgBox($MB_SYSTEMMODAL, "提示", $sMsg)
  46. Configure()
  47. EndFunc
  48. ;$lnk_file = ""

  49. Func Configure()
  50. $Form1 = GUICreate("快捷修改bat工具" & $sVersion, 742, 490, 717, 158)
  51. $Input1 = GUICtrlCreateInput("", 150, 40, 256, 21);檔案名稱輸入框
  52. GUICtrlSetTip($Input1, "輸入要儲存的bat檔名稱(不含.bat)")

  53. $btn1 = GUICtrlCreateButton ( "(Browse)", 128, 80, 20, 20,$BS_ICON);目標程式圖示
  54. GUICtrlSetTip($btn1, "請選擇目標程式")
  55. GUICtrlSetImage($btn1, $shell, $icoD, 0)
  56. $Input2 = GUICtrlCreateInput("", 150, 80, 256, 21) ;目標程式框
  57. GUICtrlSetTip($Input2, "選擇要安裝的程式")
  58. $btnsaved = GUICtrlCreateButton ( "(Browse)", 152, 120, 20, 20,$BS_ICON);要放置BAT路徑資料夾圖示按鈕
  59. GUICtrlSetTip($btnsaved, "請選擇編輯存放位置")
  60. GUICtrlSetImage($btnsaved, $shell, $icoD, 0)

  61. $OK = GUICtrlCreateButton("新BAT存檔", 432, 79, 75, 25, $WS_GROUP);新BAT存檔按鈕
  62. GUICtrlSetTip($OK, "確認內容無誤後會提示是否要覆蓋,如全新檔案則提示存檔成功")
  63. $hInput3 = GUICtrlCreateInput("", 153, 160, 250, 18);SHOW出詳細路徑輸入框
  64. GUICtrlSetTip(-1, "這裡會秀出上面的欄位")
  65. $btn2 = GUICtrlCreateButton("生成code", 437, 32, 56, 34);要選定檔案名稱、目標程式、詳細路徑才會show預設bat程式碼於下方編輯
  66. GUICtrlSetTip($btn2, "按此按鈕會生成預設程式碼在下方文字框裡")
  67. $btn_check = GUICtrlCreateButton("檢查存檔是否存在", 589, 17, 110, 26);純粹檢查是否與檔案名稱相衝突bat檔
  68. GUICtrlSetTip($btn_check, "這裡只是檢查當前是否有同名檔案存在")
  69. $hButton4 = GUICtrlCreateButton("選要編輯的bat檔", 218, 215, 110, 30);選要編輯的bat檔按鈕
  70. GUICtrlSetTip(-1, "按我選擇你要編輯的bat檔")
  71. $hButton5 = GUICtrlCreateButton("舊存檔", 418, 217, 70, 30);舊存檔按鈕
  72. $checkbat = GUICtrlCreateButton("檢查BAT執行語法",528,380,130,26)
  73. GUICtrlSetTip($checkbat, "檢查編輯區相對路徑是否有問題--請注意,這個適用於新編輯的,不適合用於舊存檔區")

  74. $Label1 = GUICtrlCreateLabel("檔案名稱", 8, 40, 99, 27)
  75. $Label2 = GUICtrlCreateLabel("目標程式", 8, 80, 65, 27)
  76. $Label3 = GUICtrlCreateLabel("要放置的BAT路徑資料夾", 8, 120, 132, 19)
  77. ;_ClearAndSetInputs($Input2)
  78. $hEdit = GUICtrlCreateEdit("", 183, 273, 333, 151);新舊BAT存檔共用顯示編輯框
  79. GUICtrlSetTip($hEdit, "按生成code後這裡會顯示出來,你可以在這文字框中編輯任何程式行,然後再去按新bat存檔按鈕")

  80. $hLabel5 = GUICtrlCreateLabel("詳細路徑:", 8, 158, 102, 21)
  81. GUICtrlSetTip(-1, "請確認路徑是否正確,")

  82. $hGroup = GUICtrlCreateGroup("編輯區", 181, 197, 335, 60)
  83. $hGroup2 = GUICtrlCreateGroup("新BAT建立區", 4, 5, 723, 186)
  84. $hLabel5 = GUICtrlCreateLabel("編輯框為新bat和編輯舊bat共用區" & @CRLF & _
  85.                                                           "但要注意的是,新建立要選定框內的按鈕", 528, 275, 196, 106)
  86. $hInput4 = GUICtrlCreateInput("", 183, 430, 333, 19)
  87. $hLabel6 = GUICtrlCreateLabel("相對路徑輸出", 33, 430, 83, 19)
  88. GUICtrlSetTip(-1, "這裡會隨著點此產生選定完自動帶出來")
  89. $hButton7 = GUICtrlCreateButton("產生", 123, 430, 53, 19)
  90. GUICtrlSetTip(-1, "點此產生")
  91. $hInput5 = GUICtrlCreateInput("", 183, 460, 333, 19)
  92. GUICtrlSetTip($hInput5, "這裡會隨著產生按鈕自動帶資料,但要自行更改相對路徑位置,目前沒有另外寫出另一判定方法")
  93. $runtest = GUICtrlCreateButton("測試", 528, 460, 53, 19)

  94. GUICtrlSetTip(-1, "點此測試路徑是否能直接開啟")

  95. GUISetState(@SW_SHOW)
  96. $title = "Choose .exe or .msi file"

  97. Local $nMsg = 0


  98. While 1
  99. $nMsg = GUIGetMsg()
  100. Switch $nMsg
  101.    Case $GUI_EVENT_CLOSE
  102.                  ExitLoop

  103.    Case $OK;新bat存檔觸發
  104.            If GUICtrlRead($Input1) And $Input2 And $hEdit = "" Then
  105.                  MsgBox(0, "", "請填入上方欄位區")
  106.    Else

  107.           If FileExists(GUICtrlRead($hInput3) & "" & GUICtrlRead($Input1) & ".bat") Then
  108.                  $check_file = MsgBox (4, "確認視窗", "是否要覆蓋?")
  109.                  If $check_file = 6 Then
  110.                  ;MsgBox(4096,"Prompt",GUICtrlRead($hInput3) & "" & GUICtrlRead($Input1) & ".bat")

  111.                  $fh = FileOpen(GUICtrlRead($hInput3) & "" & GUICtrlRead($Input1) & ".bat", 2 + 512)
  112.                  FileWriteLine($fh,GUICtrlRead($hEdit))
  113.                  FileClose($fh)
  114.                  MsgBox(4096,"Prompt",GUICtrlRead($hInput3) & "" & GUICtrlRead($Input1) & ".bat" & "已覆蓋")

  115.           ElseIf $check_file = 7 Then
  116.                  MsgBox(4096,"Prompt","取消存檔")
  117.           EndIf
  118.       Else;判斷未有同名bat情況下存檔
  119.                  $fh2 = FileOpen(GUICtrlRead($hInput3) & "" & GUICtrlRead($Input1) & ".bat", 2 + 512)
  120.                  FileWriteLine($fh2,GUICtrlRead($hEdit))
  121.                  FileClose($fh2)
  122.                  MsgBox(4096,"確認存檔","已存檔成功" & GUICtrlRead($hInput3) & "" & GUICtrlRead($Input1) & ".bat")
  123.           EndIf
  124.    EndIf
  125.    Case        $hButton5;存檔
  126.           MsgBox(64,"Prompt","舊BAT存檔:" & GUICtrlRead($hInput3))
  127.           $fh = FileOpen(GUICtrlRead($hInput3), 2 + 512);開啟BAT
  128.           FileWriteLine($fh,GUICtrlRead($hEdit));寫入行
  129.           Sleep(100)
  130.           FileClose($fh);關閉檔案
  131.    Case $btn1;這裡是選擇圖示icon事件
  132.                  $trag_file = FileOpenDialog($title, @ScriptDir & "..", "Applications/Batch (*.exe; *.msi; *.bat; *.rar; *.zip; *.7z)")
  133.                  GUICtrlSetData ( $Input2, $trag_file)

  134.                  If @error Then
  135.                          MsgBox(0,"","No file was chosen")
  136.                   EndIf
  137.         Case $btnsaved;這裡是選擇圖示icon事件
  138.           $sour_file = FileSelectFolder("請選定要放bat的路徑", @ScriptDir & "Software", 2)
  139.                  GUICtrlSetData ( $hInput3, $sour_file & "")

  140.           If @error Then
  141.                   MsgBox(0,"","No file was chosen")
  142.            EndIf
  143.         Case $hButton7;點此產生
  144.                  GUICtrlSetData($hInput4,"")
  145.                  GUICtrlSetData($hInput4,"""%~dp0",1)
  146.                  Local $string = GUICtrlRead($Input2)
  147.                  Local $findText = "D:TESTDES";"\server pathToolsxxx"
  148.                  Local $replaceText = ""
  149.                  Local $newString = StringReplace($string,WildCardFindText($findText,$string),$replaceText)


  150.                  Local $Str = StringReplace($string,WildCardFindText($findText,$string),$replaceText)

  151.                  Local $ret = StringSplit ( $Str, '', 2)
  152.                  Local $string2 = GUICtrlRead($hInput3)
  153.                  Local $replaceText = ""

  154.                  Local $findText2 = "D:TESTDES";"\server pathToolsxxx"
  155.                  Local $newString2 = StringReplace($string2,WildCardFindText2($findText2,$string2),$replaceText)

  156.                  Local $Str2 = StringReplace($string2,WildCardFindText2($findText2,$string2),$replaceText)
  157.                  Local $ret2 = StringSplit ( $Str2, '', 2)
  158.                  $Msg = ""
  159.                  For $i = 0 to UBound ($ret2)-2
  160.                                   $Msg = $Msg & $ret2[$i]
  161.                  ;MsgBox(0,"123",StringRegExpReplace($Msg, ".*(?i)[A-Z]", $msg ))
  162.                  ;MsgBox(0,"str2",$Str2)
  163.                  ;$path = StringRegExpReplace($Msg, ".*(?i)[A-Z][^0-9]", "..\")
  164.                  GUICtrlSetData($hInput4,StringRegExpReplace($Msg, ".*(?i)[A-Z].*", "..\"),1)
  165.                  GUICtrlSetData($hInput5,StringRegExpReplace($Msg, ".*(?i)[A-Z].*", "..\"),1)

  166.                  Next
  167.                  GUICtrlSetData($hInput4,$newString & """",1)
  168.                  GUICtrlSetData($hInput5,$newString & """",1)
  169.    Case $btn2;生成編輯框裡的code
  170.                  GUICtrlSetData($hEdit,"");
  171.                  Local $string = GUICtrlRead($Input2)
  172.                  Local $findText = "D:TESTDES";"\server pathToolsxxx"
  173.                  Local $replaceText = ""
  174.                  Local $newString = StringReplace($string,WildCardFindText($findText,$string),$replaceText)


  175.                  Local $Str = StringReplace($string,WildCardFindText($findText,$string),$replaceText)
  176.                  Local $ret = StringSplit ( $Str, '', 2)

  177.                  Local $string2 = GUICtrlRead($hInput3)
  178.                  Local $findText2 = "D:TESTDES";"\server pathToolsxxx"
  179.                  Local $newString2 = StringReplace($string2,WildCardFindText2($findText2,$string2),$replaceText)


  180.                  Local $Str2 = StringReplace($string2,WildCardFindText2($findText2,$string2),$replaceText)

  181.                  Local $ret2 = StringSplit ( $Str2, '', 2)

  182.                  $Msg = ""
  183.                  GUICtrlSetData($hEdit,"@echo off" & @CRLF & "echo 切換cmd語系中.." & @CRLF & _
  184. "chcp 437" & @CRLF & "echo Installing...please wait a program running" & @CRLF & _
  185. """" & "%~dp0",1)

  186.                  For $i = 0 to UBound ($ret2)-2
  187.                                   $Msg = $Msg & $ret2[$i]
  188.                  GUICtrlSetData($hEdit,StringRegExpReplace($Msg, ".*(?i)[A-Z].*", "..\"),1)
  189.                  Next
  190.                  If GUICtrlRead($Input2) = "" Then
  191.                    MsgBox(0, "", "請填入目標程式路徑")
  192.    Else
  193.                  GUICtrlSetData($hEdit,$newString & """" & @CRLF & "echo Install has been done" & @CRLF & _
  194. "timeout /t 5" & @CRLF & "exit",1)
  195.    EndIf
  196.    Case $btn_check;檢查存檔是否存在
  197.           If GUICtrlRead($hInput3) = "" Then
  198.                 MsgBox(0, "", "未填入/選擇目標bat路徑")

  199.           ElseIf FileExists(GUICtrlRead($hInput3) & "" & GUICtrlRead($Input1) & ".bat") Then
  200.                  MsgBox(4096,"Check FileExists","已有與檔案名稱相同檔案")

  201.           Else
  202.                  MsgBox(4096,"Check FileExists","沒有與檔案名稱相同檔案")
  203.           EndIf
  204.    Case $hButton4;選要編輯的bat檔
  205.                  $edit_file = FileOpenDialog($title, @ScriptDir & "Software", "Applications/Batch (*.bat)")
  206.                  GUICtrlSetData ( $hInput3, $edit_file)
  207.                  GUICtrlSetData ( $Input2, "")
  208.                  GUICtrlSetData ( $Input1, "")
  209.                  GUICtrlSetData ( $hInput4, "")
  210.                  GUICtrlSetData ( $hInput5, "")
  211.                  GUICtrlSetData($hEdit,FileRead($edit_file))
  212.                  If @error Then
  213.                          MsgBox(0,"","No file was chosen")
  214.                   EndIf
  215.    Case $checkbat;檢查BAT執行語法
  216.                  $sText = GUICtrlRead($hEdit)

  217.                  $aText = StringSplit($sText, @CRLF, 1)
  218.                  For $i =5 To $aText[0]
  219.                          $replace = StringReplace($aText[$i],"%~dp0",GUICtrlRead($hInput3) & "")

  220.                          If FileExists(GUICtrlRead($hInput3) & "" & GUICtrlRead($Input1) & ".bat") Then
  221.                                   MsgBox(4096, "檢查相對路徑", "bat檔存在,接著會測試是否能執行第五行code,路徑為:" & $replace)
  222.                                   Runwait($replace)
  223.                            Else
  224.                                 MsgBox(4096,"",GUICtrlRead($newString) & "NONO")
  225.                          EndIf
  226.                          ExitLoop
  227.                  Next
  228.                  Case $runtest

  229.                            MsgBox(64,"檢查路徑", """" & @ScriptDir & GUICtrlRead($hInput5))

  230.                            ShellExecuteWait("""" & @ScriptDir & GUICtrlRead($hInput5))
  231. EndSwitch
  232. WEnd

  233. EndFunc
  234. ;正則處理
  235. Func WildCardFindText($findText, $string)
  236. If StringInStr($findText,"*") > 0 Then
  237.   $WildCardPosition = StringInStr($findText,"*")
  238.   If $WildCardPosition = StringLen($findText) Then
  239.    $findText = StringLeft($findText,(StringLen($findText)-1))
  240.    $findText = StringRight($string,(StringLen($string)-StringInStr($string,$findText))+1)
  241.   Else
  242.    $findText = StringRight($findText,(StringLen($findText)-1))
  243.    $findText = StringLeft($string,(StringLen($string) - (StringLen($string)-StringInStr($string,$findText))))
  244.   EndIf
  245. EndIf
  246. Return $findText
  247. EndFunc
  248. Func WildCardFindText2($findText2, $string2)
  249. If StringInStr($findText2,"*") > 0 Then
  250.   $WildCardPosition = StringInStr($findText2,"*")
  251.   If $WildCardPosition = StringLen($findText2) Then
  252.    $findText = StringLeft($findText2,(StringLen($findText2)-1))
  253.    $findText = StringRight($string2,(StringLen($string2)-StringInStr($string2,$findText2))+1)
  254.   Else
  255.    $findText = StringRight($findText2,(StringLen($findText2)-1))
  256.    $findText = StringLeft($string2,(StringLen($string2) - (StringLen($string2)-StringInStr($string2,$findText2))))
  257.   EndIf
  258. EndIf
  259. Return $findText2
  260. EndFunc
複製代碼
樓主熱門主題
您需要登入後才可以回帖 登入 | 註冊

本版積分規則

小黑屋|Archiver|微剋多資訊(MicroDuo)

GMT+8, 2024-11-21 17:34

Discuz! X

© 2009-2023 Microduo

快速回覆 返回頂部 返回列表