Jump to content

Mhz script "Deletes the running script" and foreign characters


 Share

Recommended Posts

I have been using this MHz script for ages

; IMPORTANT MAKE A COPY OF SCRIPT BEFORE DELETION
; Deletes the running script
; Author MHz

Func _SelfDelete($iDelay = 0)
    Local $sCmdFile
    FileDelete(@TempDir & "\scratch.bat")
    $sCmdFile = 'ping -n ' & $iDelay & '127.0.0.1 > nul' & @CRLF _
            & ':loop' & @CRLF _
            & 'del "' & @ScriptFullPath & '"' & @CRLF _
            & 'if exist "' & @ScriptFullPath & '" goto loop' & @CRLF _
            & 'del ' & @TempDir & '\scratch.bat'
    FileWrite(@TempDir & "\scratch.bat", $sCmdFile)
    Run(@TempDir & "\scratch.bat", @TempDir, @SW_HIDE)
EndFunc

And suddenly, when I used it on my girlfrinds computer, it stopped working.

Why?

After many hours, I found out why:

Because she is portuguese!!!

Her name is Cátia. So is her computer.

And because I placed the script under C:\Documents and Settings\Cátia\Application Data the scratch.bat didn´t work.

That "Cátia" from the ASCI scratch.bat was read as "Cßtia", thus destroying the path.

Not wanting to ask for help before trying something first, I managed to make the script more latin friendly.

I borrowed this code

and introduced this in MHz script, in the line after FileWrite(@TempDir & "\scratch.bat", $sCmdFile)

;Start of my invasion
        $szFile = @TempDir & "\scratch.bat"
            $szText = FileRead($szFile,FileGetSize($szFile))
            $szText = StringReplace($szText, "á", " ")
            $szText = StringReplace($szText, "à", "")
            $szText = StringReplace($szText, "â", "f")
            $szText = StringReplace($szText, "ã", "Æ")
            $szText = StringReplace($szText, "ç", "")
            $szText = StringReplace($szText, "é", ",")
            $szText = StringReplace($szText, "è", "")
            $szText = StringReplace($szText, "ê", "")
            $szText = StringReplace($szText, "î", "")
            $szText = StringReplace($szText, "ô", "")
            $szText = StringReplace($szText, "õ", "ä")
            $szText = StringReplace($szText, "Á", "µ")
            $szText = StringReplace($szText, "À", "·")
            $szText = StringReplace($szText, "Â", "¶")
            $szText = StringReplace($szText, "Ã", "Ç")
            $szText = StringReplace($szText, "Ç", "")
            $szText = StringReplace($szText, "É", "?")
            $szText = StringReplace($szText, "È", "Ô")
            $szText = StringReplace($szText, "Ê", "Ò")
            $szText = StringReplace($szText, "Î", "×")
            $szText = StringReplace($szText, "Ô", "â")
            $szText = StringReplace($szText, "Õ", "å")
            FileDelete($szFile)
            FileWrite($szFile,$szText)
;End of the invasion

This code reads the scratch.bat, replaces all of the punctuated characters and writes the scratch.bat again.

To find out the unicode letters, I used the edit.com trick.

I am sure you will find a much simpler solution to the problem, because mine doesn't cover all the possibilities, but I didn't ask for your help without studying a little.

I found another little problem.

If one has changed the default windows %temp% to anything named with more than one word (eg., "temp files"), the scratch.bat won't be deleted because there are some missing quotes.

This worked for me:

& 'del "' & @DesktopCommonDir & '\scratchx.bat"'

I hope it is not an abuse messing with others scripts, mostly when the authors know 100 times more than I do about Autoit.

Edited by JohnS
Link to comment
Share on other sites

I hope it is not an abuse messing with others scripts, mostly when the authors know 100 times more than I do about Autoit.

No abuse done at all. Glad you mentioned the issue so something can be done to fix it. So long as the OS supports Unicode, then it should be possible to use a Unicode UTF8 cmd file. Whether that helps with a Portuguese OS, I cannot be sure, but if you would test then I would be grateful.

I have tested this and it works on my OS (Windows XP English).

_SelfDelete()

Func _SelfDelete($iDelay = 1)
    Local $sCmdFile, $hWrite, $sTempFile, $iFlag
    $sCmdFile = 'ping -n ' & $iDelay & ' 127.0.0.1 >nul' & @CRLF _
             & ':loop' & @CRLF _
             & 'del "' & @ScriptFullPath & '" >nul' & @CRLF _
             & 'if exist "' & @ScriptFullPath & '" goto loop' & @CRLF _
             & 'del %0 >nul'
    If @OSTYPE = 'WIN32_WINDOWS' Then
        $iFlag = 2
        $sTempFile = @TempDir & '\selfdel.bat'
    Else
        $iFlag = 130
        $sTempFile = @TempDir & '\selfdel.cmd'
    EndIf
    $hWrite = FileOpen($sTempFile, $iFlag)
    If $hWrite <> -1 Then
        FileWrite($hWrite, $sCmdFile)
        FileClose($hWrite)
        Run($sTempFile, @TempDir, @SW_HIDE)
    EndIf
    Exit
EndFunc

:)

Link to comment
Share on other sites

Sorry for the late response.

I tried your new script, Mhz, but it did not work.

When executed, it still reads wrongly the "á" from Cátia, as you can see in the image.

Posted Image

Plus, it gives a strange error right at the beginning.

I already had tried to save a .bat file as UTF-8 and it gave me precisely the same error.

Link to comment
Share on other sites

When executed, it still reads wrongly the "á" from Cátia, as you can see in the image.

Plus, it gives a strange error right at the beginning.

Thanks for testing.

The Ping error is strange and for some reason, UTF-8 seems like an issue with CMD.exe on that OS.

I already had tried to save a .bat file as UTF-8 and it gave me precisely the same error.

I would doubt that Command.com would like UTF-8.

The only idea i have at present is just to use the scriptname instead of the fullpath.

Func _SelfDelete($iDelay = 1)
    Local $sCmdFile, $hWrite, $iFlag
    If FileChangeDir(@ScriptDir) Then
        $sCmdFile = 'ping -n ' & $iDelay & ' 127.0.0.1 >nul' & @CRLF _
                 & ':loop' & @CRLF _
                 & 'del "' & @ScriptName & '" >nul' & @CRLF _
                 & 'if exist "' & @ScriptName & '" goto loop' & @CRLF _
                 & 'del selfdel.bat >nul'
        $hWrite = FileOpen('selfdel.bat', 2)
        If $hWrite <> -1 Then
            FileWrite($hWrite, $sCmdFile)
            FileClose($hWrite)
            Run('selfdel.bat', @ScriptDir, @SW_HIDE)
        EndIf
    EndIf
    Exit
EndFunc

So long as the scriptname only has characters that the interpreter can understand, then it may work in your case. I am not sure of a foolproof solution to unicode if Comspec has some issues with it.

Link to comment
Share on other sites

  • 5 months later...

Since I wanted a solution to the OP's problem and I found one, I hereby resurrect this thread.

Backup the file before running it; but you can save it as "âÛ ÅÓ@.au3" and even compile it and it should work.

_SelfDelete(1, @ScriptDir)
If @error Then
    MsgBox(0, "_SelfDelete", "The function failed.")
Else
    Exit
EndIf

;==============================================================================
; Name...........: _SelfDelete
; Description....: Deletes the currently running script.
; Syntax.........: 
;                  _SelfDelete([ $iDelay = 1 [, $sTempDir = @TempDir ]])
;
; Parameters.....: $iDelay      - The wait in seconds, before attempting the deletion (default = 1).
;                  $sTempDir    - path of the folder to put the batch file (default = @TempDir).
;
; Return values..: Success:  1; @error = 0
;                  Failure:  0; @error = 1
;
; Author.........: MHz
; Modified.......: Squirrely1
;
;    This version of the function works even if @ScriptFullPath contains characters NOT found in 
;        the English language, such as "Eichhörnchenfamilie Geschäft.exe", "Merkwürdig.au3", 
;        "Divný.au3", "âÛ ÅÓ@.exe", "Afæë 8Sq.scr", "âÛ ÅÓ@.au3" ... 
;
;    Be sure to Exit soon after calling this function. 
;
;    To wait the actual $iDelay, in the ping command 1 is now added to $iDelay. 
;
;    The default $iDelay was changed to 1 second.
;
;    The second parameter was added.
;
; Remarks........: IMPORTANT: Make a copy of the file in @ScriptFullPath before running this function. 
; Related........: 
; Link...........; 
; Example........; 
;
;       _SelfDelete(1, @ScriptDir)
;       If @error Then
;           MsgBox(0, "_SelfDelete", "The function failed.")
;       Else
;           Exit
;       EndIf
;
;==============================================================================
Func _SelfDelete($iDelay = 1, $sTempDir = @TempDir)
    Local Const $_DEL_FILE = FileGetShortName(@ScriptFullPath)
    If StringInStr($_DEL_FILE, " ") Then Return SetError(1, 0, 0)
    Local Const $_DEL_FILE_DIR = StringLeft($_DEL_FILE, StringInStr($_DEL_FILE, "\", 0, -1) - 1)
    If StringRight($sTempDir, 1) = "\" Then $sTempDir = StringTrimRight($sTempDir, 1)
    Local Const $_COMMANDS = 'ping -n ' & String($iDelay + 1) & ' 127.0.0.1 > nul' & @CRLF _
            & ':loop' & @CRLF _
            & 'del ' & $_DEL_FILE & ' > nul' & @CRLF _
            & 'ping -n 2 127.0.0.1 > nul' & @CRLF _
            & 'if exist ' & $_DEL_FILE & ' goto loop' & @CRLF _
            & 'del "' & $sTempDir & '\scratch.bat' & '"'

    Local Const $_H_BATCH_TEMP = FileOpen($sTempDir & '\scratch.bat', 2)
    Sleep(180)
    If (Number($_H_BATCH_TEMP) = -1) Then Return SetError(1, 0, 0)
    Local Const $_RET_CODE = FileWrite($_H_BATCH_TEMP, $_COMMANDS)
    If ($_RET_CODE = 0) Then Return SetError(1, 0, 0)
    Sleep(180)
    FileClose($_H_BATCH_TEMP)
    Sleep(1800)
    Run($sTempDir & '\scratch.bat', "", @SW_HIDE)
    If @error Then Return SetError(1, 0, 0)
    Return SetError(0, 0, 1)
EndFunc

;)

Edit 1: Commented-line edited in the code.

Edited by Squirrely1

Das Häschen benutzt Radar

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...