Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 02/28/2014 in all areas

  1. Having used the _SelfDelete() function before, I knew it could be improved upon with adding more functionality to compensate for when an AutoIt error might occur. The version I updated includes the ability to monitor when the process is closed and/or the timer is reached, therefore if an error was to occur with your program and the _SelfDelete() function was called before, it will still delete the executable even with an AutoIt error (due to bad coding of course!) This can't be said for the old version of _SelfDelete() as you'll never have chance to call the function! I also included the option to check either the process name or PID, depending on your preference. Thanks. Function: Using a Batch script. Save as _SelfDelete.au3 #include-once #include <FileConstants.au3> #include <StringConstants.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: _SelfDelete ; Description ...: Delete the current executable after it's finished processing and/or the timer has been reached. ; Syntax ........: _SelfDelete([$iDelay = 5[, $fUsePID = False[, $fRemoveDir = False]]]) ; Parameters ....: $iDelay - [optional] An integer value for the delay to wait (in seconds) before stopping the process and deleting the executable. ; If 0 is specified then the batch will wait indefinitely until the process no longer exits. Default is 5 (seconds). ; $fUsePID - [optional] Use the process name (False) or PID (True). Default is False. ; $fRemoveDir - [optional] Remove the script directory as well (True) or only the running executable (False). Default is False. ; Return values .: Success - Returns the PID of the batch file. ; Failure - Returns 0 & sets @error to non-zero ; Author ........: guinness ; Modified ......: ; Remarks .......: The idea for removing the directory came from: http://www.autoitscript.com/forum/topic/137287-delete-scriptdir/ ; Example .......: Yes ; =============================================================================================================================== Func _SelfDelete($iDelay = 5, $fUsePID = Default, $fRemoveDir = Default) If @Compiled = 0 Then Return SetError(1, 0, 0) EndIf Local $sTempFileName = @ScriptName $sTempFileName = StringLeft($sTempFileName, StringInStr($sTempFileName, '.', $STR_NOCASESENSEBASIC, -1) - 1) While FileExists(@TempDir & '\' & $sTempFileName & '.bat') $sTempFileName &= Chr(Random(65, 122, 1)) WEnd $sTempFileName = @TempDir & '\' & $sTempFileName & '.bat' Local $sDelay = '' $iDelay = Int($iDelay) If $iDelay > 0 Then $sDelay = 'IF %TIMER% GTR ' & $iDelay & ' GOTO DELETE' EndIf Local $sRemoveDir = '' If $fRemoveDir Then $sRemoveDir = 'RD /S /Q "' & FileGetShortName(@ScriptDir) & '"' & @CRLF EndIf Local $sAppID = @ScriptName, $sImageName = 'IMAGENAME' If $fUsePID Then $sAppID = @AutoItPID $sImageName = 'PID' EndIf Local Const $iInternalDelay = 2, _ $sScriptPath = FileGetShortName(@ScriptFullPath) Local Const $sData = 'SET TIMER=0' & @CRLF _ & ':START' & @CRLF _ & 'PING -n ' & $iInternalDelay & ' 127.0.0.1 > nul' & @CRLF _ & $sDelay & @CRLF _ & 'SET /A TIMER+=1' & @CRLF _ & @CRLF _ & 'TASKLIST /NH /FI "' & $sImageName & ' EQ ' & $sAppID & '" | FIND /I "' & $sAppID & '" >nul && GOTO START' & @CRLF _ & 'GOTO DELETE' & @CRLF _ & @CRLF _ & ':DELETE' & @CRLF _ & 'TASKKILL /F /FI "' & $sImageName & ' EQ ' & $sAppID & '"' & @CRLF _ & 'DEL "' & $sScriptPath & '"' & @CRLF _ & 'IF EXIST "' & $sScriptPath & '" GOTO DELETE' & @CRLF _ & $sRemoveDir _ & 'GOTO END' & @CRLF _ & @CRLF _ & ':END' & @CRLF _ & 'DEL "' & $sTempFileName & '"' Local Const $hFileOpen = FileOpen($sTempFileName, $FO_OVERWRITE) If $hFileOpen = -1 Then Return SetError(2, 0, 0) EndIf FileWrite($hFileOpen, $sData) FileClose($hFileOpen) Return Run($sTempFileName, @TempDir, @SW_HIDE) EndFunc ;==>_SelfDeleteFunction: Using a VBScript file. Save as _SelfDelete.au3 #include-once #include <FileConstants.au3> #include <StringConstants.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: _SelfDelete ; Description ...: Delete the current executable after it's finished processing and/or the timer has been reached. ; Syntax ........: _SelfDelete([$iDelay = 5[, $fUsePID = False[, $fRemoveDir = False]]]) ; Parameters ....: $iDelay - [optional] An integer value for the delay to wait (in seconds) before stopping the process and deleting the executable. ; If 0 is specified then the script will wait indefinitely until the process no longer exits. Default is 5 (seconds). ; $fUsePID - [optional] Use the process name (False) or PID (True). Default is False. ; $fRemoveDir - [optional] Remove the script directory as well (True) or only the running executable (False). Default is False. ; Return values .: Success - Returns the PID of the batch file. ; Failure - Returns 0 & sets @error to non-zero ; Author ........: guinness ; Modified ......: ; Remarks .......: The idea for removing the directory came from: http://www.autoitscript.com/forum/topic/137287-delete-scriptdir/ ; Example .......: Yes ; =============================================================================================================================== Func _SelfDelete($iDelay = 5, $fUsePID = Default, $fRemoveDir = Default) If @Compiled = 0 Then Return SetError(1, 0, False) EndIf Local $sTempFileName = @ScriptName $sTempFileName = StringLeft($sTempFileName, StringInStr($sTempFileName, '.', $STR_NOCASESENSEBASIC, -1) - 1) While FileExists(@TempDir & '\' & $sTempFileName & '.bat') $sTempFileName &= Chr(Random(65, 122, 1)) WEnd $sTempFileName = @TempDir & '\' & $sTempFileName & '.vbs' Local $sDelay = '' $iDelay = Int($iDelay) If $iDelay > 0 Then $sDelay = @TAB & 'iTimeOut = iTimeOut + 1' & @CRLF _ & @TAB & 'If iTimeOut > ' & $iDelay & ' Then' & @CRLF _ & @TAB & @TAB & 'For Each oProcess in oWMIQuery' & @CRLF _ & @TAB & @TAB & @TAB & 'oProcess.Terminate()' & @CRLF _ & @TAB & @TAB & 'Next' & @CRLF _ & @TAB & @TAB & 'iExit = 2' & @CRLF _ & @TAB & 'End If' & @CRLF EndIf Local $sRemoveDir = '' If $fRemoveDir Then $sRemoveDir = 'oFileSystem.DeleteFolder "' & @ScriptDir & '", True' & @CRLF EndIf Local $sAppID = @ScriptName, $sImageName = 'Name' If $fUsePID Then $sAppID = @AutoItPID $sImageName = 'ProcessId' EndIf Local Const $iInternalDelay = 10, _ $sScriptPath = @ScriptFullPath Local Const $sData = 'Option Explicit' & @CRLF _ & 'Dim iExit, iTimeOut, oFileSystem, oProcess, oWMIQuery, oWMIService, sComputer, sFilePath, sWMIQuery' & @CRLF _ & @CRLF _ & 'sFilePath = "' & $sScriptPath & '"' & @CRLF _ & @CRLF _ & 'iExit = 0' & @CRLF _ & 'iTimeOut = 0' & @CRLF _ & 'sComputer = "."' & @CRLF _ & @CRLF _ & 'Set oWMIService = GetObject("winmgmts:" _' & @CRLF _ & @TAB & @TAB & '& "{impersonationLevel=impersonate}!\\" _' & @CRLF _ & @TAB & @TAB & '& sComputer & "\root\cimv2")' & @CRLF _ & @CRLF _ & 'sWMIQuery = "Select * From Win32_Process Where ' & $sImageName & ' = ''' & $sAppID & '''"' & @CRLF _ & @CRLF _ & 'While (iExit = 0)' & @CRLF _ & @TAB & 'Set oWMIQuery = oWMIService.ExecQuery(sWMIQuery)' & @CRLF _ & @TAB & 'If oWMIQuery.Count = 0 Then' & @CRLF _ & @TAB & @TAB & 'iExit = 1' & @CRLF _ & @TAB & 'End If' & @CRLF _ & @TAB & 'WScript.Sleep(1000)' & @CRLF _ & $sDelay _ & 'Wend' & @CRLF _ & @CRLF _ & 'WScript.Sleep(1000)' & @CRLF _ & 'iExit = 0' & @CRLF _ & 'iTimeOut = 0' & @CRLF _ & 'Set oFileSystem = CreateObject("Scripting.FileSystemObject")' & @CRLF _ & 'While (iExit = 0)' & @CRLF _ & @TAB & 'iTimeOut = iTimeOut + 1' & @CRLF _ & @TAB & 'If oFileSystem.FileExists(sFilePath) Then' & @CRLF _ & @TAB & @TAB & 'oFileSystem.DeleteFile sFilePath, True' & @CRLF _ & @TAB & 'End If' & @CRLF _ & @TAB & 'If oFileSystem.FileExists(sFilePath) <> True Then' & @CRLF _ & @TAB & @TAB & 'iExit = 1' & @CRLF _ & @TAB & 'End If' & @CRLF _ & @TAB & 'If iTimeOut > ' & $iInternalDelay & ' Then' & @CRLF _ & @TAB & @TAB & 'iExit = 2' & @CRLF _ & @TAB & 'End If' & @CRLF _ & 'Wend' & @CRLF _ & @CRLF _ & $sRemoveDir _ & 'oFileSystem.DeleteFile WScript.ScriptFullName, True' & @CRLF _ Local Const $hFileOpen = FileOpen($sTempFileName, $FO_OVERWRITE) If $hFileOpen = -1 Then Return SetError(2, 0, False) EndIf FileWrite($hFileOpen, $sData) FileClose($hFileOpen) Return ShellExecute($sTempFileName, @TempDir, @TempDir, '', @SW_HIDE) EndFunc ;==>_SelfDeleteExample use of Function: #include <MsgBoxConstants.au3> #include '_SelfDelete.au3' _SelfDelete(30, 0) ; Start the SelfDelete batch file with a 30 second timer and using the prcocess name rather than the PID. If @error Then Exit MsgBox($MB_SYSTEMMODAL, '_SelfDelete()', 'The script must be a compiled exe to work correctly.') ; Display a warning if the script isn't compiled. EndIf While 1 Sleep(100) ; Wait or manually close the application via the traymenu icon. WEndWARNING: This will delete your executable if the function is called, so take the proper precautions and make sure you backup.
    1 point
  2. Have you ever asked yourself the question(s) What does AutoItWinGetTitle() or AutoItWinSetTitle() do? Is there a simple way to communicate between two AutoIt scripts other than WM_COPYDATA (see in my signature)? How can I store data/text other than saving it to a file? If you have then please read on... I first came across this little trick via KaFu (couple of years ago) who was using it in his example for an alternative of >_Singleton(). I couldn't understand why he was reading the text in an edit box until I decided to delve a little deeper. Turns out the hidden AutoIt window contains an edit box, so with that in mind I decided to post an example of how useful it could be. In this example I add data from the first instance of the program e.g. PID, location, working directory & then grab this data in the second instance. Just to prove I'm not cheating I've also included data of the second instance too so you can see the PID's are totally different. Example 1: Compile this example and run it! Once the hidden window is shown run the executable again. MsgBox's will be displayed during the process explaining what is happening. #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> Example() Func Example() ; Display AutoIt's Hidden Window. See AutoItWinGetTitle and AutoItWinSetTitle for more details. AutoItWinShow() ; Add a text string to AutoIt's Hidden Window. AutoItWinSetText('Welcome to AutoIt V' & @AutoItVersion & @CRLF) ; Add a text string to AutoIt's Hidden Window. AutoItWinSetText('Windows Type: ' & @OSType & @CRLF) ; Display the text stored in AutoIt's Hidden Window. MsgBox($MB_SYSTEMMODAL, '', AutoItWinGetText()) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd EndFunc ;==>Example ; Retrieve the text in AutoIt's Hidden Window. Func AutoItWinGetText() Local Const $hWnd = WinGetHandle(AutoItWinGetTitle()) ; Get the handle of the AutoIt Hidden Window by finding out the title of the AutoIt Hidden Window. Return ControlGetText($hWnd, '', ControlGetHandle($hWnd, '', 'Edit1')) EndFunc ;==>AutoItWinGetText ; Add text to AutoIt's Hidden Window. Func AutoItWinSetText($sString) Local Const $hWnd = WinGetHandle(AutoItWinGetTitle()) ; Get the handle of the AutoIt Hidden Window by finding out the title of the AutoIt Hidden Window. Return ControlSetText($hWnd, '', ControlGetHandle($hWnd, '', 'Edit1'), AutoItWinGetText() & $sString) EndFunc ;==>AutoItWinSetText ; Display AutoIt's Hidden Window. Returns the handle of the window. Func AutoItWinShow() Local Const $hWnd = WinGetHandle(AutoItWinGetTitle()) ; Get the handle of the AutoIt Hidden Window by finding out the title of the AutoIt Hidden Window. WinMove($hWnd, '', (@DesktopWidth / 2) - 250, (@DesktopHeight / 2) - 250, 500, 500) ; Move the AutoIt Hidden Window and re-size for a better view of the data that will be set. WinSetState($hWnd, '', @SW_SHOW) ; Show the AutoIt Hidden Window, normally this is hidden, but in the interest of this example I'm displaying it. Return $hWnd EndFunc ;==>AutoItWinShowExample 2: Compile this example and run it. #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> If @Compiled Then Example() Else MsgBox($MB_SYSTEMMODAL, 'AutoIt Hidden Window', 'This example needs to be compiled before you can run it.') ; Show an error if the script isn't compiled. EndIf Func Example() Local Const $sData = 'PID: ' & @AutoItPID & @CRLF & _ ; Create a data string about the running executable. 'Version: ' & @AutoItVersion & @CRLF & _ 'Script Directory: ' & @ScriptDir & @CRLF & _ 'Working Directory: ' & @WorkingDir Local $hAutoIt = WinGetHandle('AutoIt_Hidden_Window') ; Check if the AutoIt Hidden Window exists and if it does then read the details set in the edit box. If IsHWnd($hAutoIt) Then Local Const $sRead = AutoItWinGetText($hAutoIt) ; Retrieve the information of the inital AutoIt Hidden Window. AutoItWinSetText('Now you can close this window.') ; Set the text of the edit box to show it can now be closed. Return MsgBox($MB_SYSTEMMODAL, 'AutoIt Hidden Window', 'Previous PID:-' & @CRLF & _ $sRead & @CRLF & @CRLF & _ 'Current PID:-' & @CRLF & _ $sData & @CRLF & @CRLF & _ 'This will now Exit. Thanks.') EndIf AutoItWinSetTitle('AutoIt_Hidden_Window') ; Set the title of the AutoIt Hidden Window with some custom text, the more unqiue the better. AutoItWinShow() AutoItWinSetText($sData) ; Set the text of the edit box using the data string created at the start of the script. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ; Just hit the close button to Exit the application. ExitLoop EndSwitch WEnd EndFunc ;==>Example ; Retrieve the text in AutoIt's Hidden Window. Func AutoItWinGetText($hWnd = Default) If IsHWnd($hWnd) = 0 Or $hWnd = Default Then $hWnd = WinGetHandle(AutoItWinGetTitle()) ; Get the handle of the AutoIt Hidden Window by finding out the title of the AutoIt Hidden Window. EndIf Return ControlGetText($hWnd, '', ControlGetHandle($hWnd, '', 'Edit1')) EndFunc ;==>AutoItWinGetText ; Add text to AutoIt's Hidden Window. Func AutoItWinSetText($sString, $hWnd = Default) If IsHWnd($hWnd) = 0 Or $hWnd = Default Then $hWnd = WinGetHandle(AutoItWinGetTitle()) ; Get the handle of the AutoIt Hidden Window by finding out the title of the AutoIt Hidden Window. EndIf Return ControlSetText($hWnd, '', ControlGetHandle($hWnd, '', 'Edit1'), AutoItWinGetText() & $sString) EndFunc ;==>AutoItWinSetText ; Display AutoIt's Hidden Window. Returns the handle of the window. Func AutoItWinShow() Local Const $hWnd = WinGetHandle(AutoItWinGetTitle()) ; Get the handle of the AutoIt Hidden Window by finding out the title of the AutoIt Hidden Window. WinMove($hWnd, '', (@DesktopWidth / 2) - 250, (@DesktopHeight / 2) - 250, 500, 500) ; Move the AutoIt Hidden Window and re-size for a better view of the data that will be set. WinSetState($hWnd, '', @SW_SHOW) ; Show the AutoIt Hidden Window, normally this is hidden, but in the interest of this example I'm displaying it. Return $hWnd EndFunc ;==>AutoItWinShow Example 3: >Send text to a 2nd instance.
    1 point
  3. jguinch

    Run WBAdmin.exe Help

    It's because you're running a x64 Windows and your script is runnig in x86 mode. The wbadmin.exe command is stored in c:windowssystem32, and the x86 cmd maps the @SystemDir to C:WindowsSysWOW64 folder. If you want to access to the x64 commands from a x86 script, you can use @WindowsDir & "sysnative" instead of @SystemDir, or change the PATH environment variable before calling the command, like this EnvSet("PATH", @WindowsDir & "\sysnative;" & EnvGet("PATH") ) RunWait('"' & @ComSpec & '" /k ' & $CMD, @SystemDir)
    1 point
  4. Picorico, We do not support any script to automate software able to download YouTube streaming video - as explained in the annoucement to which you have been linked above. Thread closed. M23
    1 point
  5. Do you read this ? '?do=embed' frameborder='0' data-embedContent>>
    1 point
  6. Tlem

    Using resource DLLs

    Can you share your solution ... And close the french topic.
    1 point
  7. Ops, forgot the reg type... Br, UEZ
    1 point
  8. SO, i was playing around with adding a C preprocessor into the SciTE IDE and i thought it became kinda cool. A package is attached, which hold a small installer and some binary files, that integrates the preprocessor into SciTE along with some syntax stuff, so you can run it right away through F5. It can also uninstall what was installed. This uses the open source mcpp C preprocessor, which can be found here: http://mcpp.sourceforge.net/ Example of usage (this gets converted to perfect valid au3 code): In case anybody doesn't know what the C preprocessor is: It allows for inclusion of other source files (already supported in autoit through #include)It allows for symbolic constants and small function-like macroesMost importantly, it allows for conditional inclusion through the use of #ifdef, #ifndef and #ifIt has some other tricks too, some of them explained in above picture.Known incompatibilities: Empty escape characters ("\") causes the engine to crash, i will look into this... can be disabled in \Autoit\scite\autoitwrapper\pre.ini (remove argument -a, others can btw be added here). So does anybody feel this is useful? Au3 PP.zip
    1 point
  9. MHz

    need to execute cmd commands

    The code below is perhaps worth a try as it attempts to do all 5 commands in a single CMD instance. ; Use to run the command following && only if the ; command preceding the symbol is successful. ; Cmd.exe runs the first command, and then runs ; the second command only if the first command ; completed successfully. ; Reference: ; http://technet.microsoft.com/en-us/library/bb490954.aspx $CMD = 'ipconfig /flushdns && ' & _ 'net stop dnscache && ' & _ 'ipconfig /flushdns && ' & _ 'net start dnscache && ' & _ 'ipconfig /flushdns' ; /k keeps the CMD window open. ; /c closes the CMD window once the command is complete. ; Reference: ; Type CMD /? in a command prompt window for help ; with CMD options. RunWait('"' & @ComSpec & '" /k ' & $CMD, @SystemDir)
    1 point
×
×
  • Create New...