AutoHotkey Community

It is currently May 27th, 2012, 1:53 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 28 posts ]  Go to page Previous  1, 2
Author Message
 Post subject:
PostPosted: August 22nd, 2007, 5:39 pm 
Thank you Skan as the code you have given above truly works. I cannot make head or tail of your script as my level of coding is quite primitive but all I know is that it works. I simply copy-pasted your code into my script and bingo! it worked. The only change I made was in the line - Wscript.Sleep 15000, I changed it to Wscript.Sleep 150 and it works quicker and as effectively. If you advice leaving it to the original Wscript.Sleep 15000 (for whatever reason), I will revert back to that.

Having said that – There is one problem that I have noticed whilst experimenting with this code – namely –

Say, if the user sets the read-only attribute to the compiled exe file before it is run – not only does it not delete itself as intended but it also creates a file named selfDelete.VBS (which I reckon is part of the script) in the temporary directory, which remains there. This in effect means that if you were to change the file attribute (removing the read-only attribute of the exe file) and once again run the exe file, because the selfDelete.VBS is already present in the temp directory it does not delete the exe file.

I get the following errors when I tried it –

Instance One

Window Name – Windows Script Host
Script – Full Path of the Temporary Directory on C Drive followed by the filename selfDelete.VBS – in my case – (C:\DOCUME~1\Freddie\LOCALS~1\Temp\selfDelete.VBS)
Line – 5
Char – 1
Error – Permission Denied
Code – 800A0046
Source - Microsoft VBScript runtime error

Instance Two

Window Name – Windows Script Host

Script – Full Path of the Temporary Directory on C Drive followed by the filename selfDelete.VBS – in my case – (C:\DOCUME~1\Freddie\LOCALS~1\Temp\selfDelete.VBS)
Line – 11
Char – 35
Error – Expected end of statement
Code – 800A0401
Source - Microsoft VBScript runtime error

Then, when I deleted the selfDelete.VBS from the Temporary Directory (plus removed the read-only attribute) and ran the exe file once again, it worked as intended.

What I gather from this is that if the selfDelete.VBS file (for whatever reason) still exists in the Temporary Directory the script doesn’t function as intended.

Is there a solution to this problem? Firstly, can a code be inserted that would first remove all the attributes of the exe file (something like FileSetAttrib, -R-A-S-H-N-O-T,) so even if it is a read-only file it can be deleted and also, can we assign a name of the directory where the selfDelete.VBS file will function. Perhaps create a directory, let the selfDelete.VBS do its job and then delete the selfDelete.VBS file and directory; else if it can only work in the temporary directory at least delete the selfDelete.VBS file come what may. Thereby even if there is an initial unforeseen error, at least once the error is identified the next time around there isn’t a problem because the selfDelete.VBS file is still in the Temp Directory.

Please do let me know if this possible and once again, I earnestly request you to please offer me the code akin to the way you previously did – something that I can simply cut and paste into my script.

Sincerely Thanking You…

P.S. - Thank You daonlyfreez for giving me the link to the post that deals with the similar situation - it's just that I have been working around the code given by Skan and hence haven't even had the chance to check out the other post. Will try and browse through that soon...


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 22nd, 2007, 6:57 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Freddie wrote:
The only change I made was in the line - Wscript.Sleep 15000, I changed it to Wscript.Sleep 150 and it works quicker and as effectively. If you advice leaving it to the original Wscript.Sleep 15000 (for whatever reason), I will revert back to that.


:shock: :oops: I did not see that ... and I had complained that the method was slow :lol:

Code:
If A_IsCompiled {

FileSetAttrib, -R-A-S-H-N-O-T, %A_ScriptFullPath%
FileDelete, %A_Temp%\selfDelete.VBS

FileAppend,
(
Wscript.Sleep 2000
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.GetFile("%A_ScriptFullPath%")
MyFile.Delete

'Delete the currently executing script
Dim objFSO    'Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile WScript.ScriptFullName
Set objFSO = Nothing
)
, %A_Temp%\selfDelete.VBS

Run, %A_Temp%\selfDelete.VBS
ExitApp

}


:)


Report this post
Top
 Profile  
Reply with quote  
PostPosted: August 23rd, 2007, 7:01 am 
Offline

Joined: July 26th, 2004, 9:07 pm
Posts: 62
simple write at the end of file.exe:

FileAppend,del file.exe`n,delete.bat
FileAppend,del delete.bat`n,delete.bat
sleep,30 (is better sleep a bit , even it work whitout)
run,delete.bat
exit

loriss

_________________
Loriss


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 9th, 2008, 1:44 pm 
Offline

Joined: November 24th, 2005, 8:16 am
Posts: 851
Hey guys, thanks for this thread. Very helpful.

I am doing my own installer in AHK, and reached the stage where my Uninstall.exe needs to delete itself.

The Comspec /c del trick works nicely assuming your exit the self-deleting exe as soon as you call the comspec /c del.

It is similar to how windows behaves when you try to delete a file in use. It will give you a couple of grace seconds to close it, before it jumps with an error that the "file may be in use"

Also know, that when I searched google for "exe file delete itself" - this thread came the first. :)

EDIT:
Addition, to those who also want to delete the folder where the file resides.
The trick is to call the batch file with a working directory other than the one the file is in, otherwise, the directory will be considered "in use"

Code:
FileDelete %A_Temp%\uninst.bat
FileAppend del "%Filename%"`n, %A_Temp%\uninst.bat
FileAppend rmdir "%Folder%"`n, %A_Temp%\uninst.bat
Run %Comspec% /c %A_Temp%\uninst.bat, %A_Temp%,HIDE

_________________
Sector-Seven - Freeware tools built with AutoHotkey


Report this post
Top
 Profile  
Reply with quote  
PostPosted: December 3rd, 2008, 3:30 am 
Right before my application shuts down, I start this as an external process

CMD.exe /C choice /c YNC /m delay /D Y /T 3 & Del DeleteSelf.exe

DeleteSelf.exe is the name of my test application that calls the line above. The cool thing about this is that the CMD /C part forces the command window to close immediately after it runs the command specified. Thus, there are no _visible_ traces left behind. Note that there are actually two commands being run.. choice and del.. they're chained together with the & character. Until just now, I didn't know about either the choice command or the &! :)

Note that the choice part could use some tweaking.. I was just happy it worked and wanted to post it. The important part about choice is the /D Y and /T 3 parts that cause choice to pause for 3 seconds and then automatically pick the choice "Y". This gives DeleteSelf.exe a bit of time to close before the command prompt tries to delete the file.


Report this post
Top
  
Reply with quote  
PostPosted: December 3rd, 2008, 7:27 am 
Offline

Joined: April 27th, 2008, 5:28 pm
Posts: 489
alexdresko wrote:
CMD.exe /C choice /c YNC /m delay /D Y /T 3 & Del DeleteSelf.exe
How do you start this as an external process?


Report this post
Top
 Profile  
Reply with quote  
PostPosted: December 3rd, 2008, 8:45 am 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3330
Location: Simi Valley, CA
DataLife wrote:
alexdresko wrote:
CMD.exe /C choice /c YNC /m delay /D Y /T 3 & Del DeleteSelf.exe
How do you start this as an external process?

That would be the Run command. i.e.:
Code:
OnExit: ; subroutine called when this script is ready to exit.
; delete this script file 3 seconds after it terminates.
Run CMD.exe /c ping -n 3 127.0.0.1>nul & Del "%A_ScriptFullPath%",, HIDE
Exitapp ; exit & close the script

This works with compiled scripts (obviously, because there's no point doing it for uncompiled scripts).

_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 3rd, 2008, 9:02 pm 
Offline

Joined: April 27th, 2008, 5:28 pm
Posts: 489
thanks VxE,

I did try it with the run command but not after OnExit:

I will try that later.

DataLife


Report this post
Top
 Profile  
Reply with quote  
PostPosted: December 4th, 2008, 12:08 am 
Offline

Joined: April 27th, 2008, 5:28 pm
Posts: 489
[VxE] wrote:
OnExit: ; subroutine called when this script is ready to exit.
; delete this script file 3 seconds after it terminates.
Run CMD.exe /c ping -n 3 127.0.0.1>nul & Del "%A_ScriptFullPath%",, HIDE
Exitapp ; exit & close the script[/code]


Works great

thanks


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Self-removing EXE
PostPosted: January 24th, 2010, 12:59 pm 
I use this compiled EXE to remove a folder structure containing this file, as well as the file itself. This happens to be an Uninstall.exe file contained within the "bin" folder of the program it is uninstalling. In this example, the uninstall file would be in C:\Test\bin, with your program files in C:\Test.

Code:
Install_Path = C:\Test
MsgBox, 4, Confirm, Are you sure you want to remove this program and all of it's components?
IfMsgBox, Yes
{
FileMove, %A_ScriptName%, C:\Temp
FileRemoveDir, %Install_Path%\, 1
Sleep, 1000
RemoveSelf = "C:\Temp\%A_ScriptName%"
Run, %comspec% /c del %RemoveSelf% /q,,hide

ExitApp
}
Else
MsgBox,,Cancelled, Uninstall operation cancelled.
ExitApp


Cheers! :lol:


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 24th, 2010, 2:38 pm 
Offline

Joined: July 17th, 2008, 9:46 am
Posts: 225
why do you push this thread?


Report this post
Top
 Profile  
Reply with quote  
 Post subject: a very useful thread
PostPosted: March 15th, 2010, 9:50 pm 
This is a very useful thread. Thnks everyone for their contribution!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 16th, 2010, 5:57 am 
Thanks "itjustworks".

This helps me alot. Thanks for sharing.


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 28 posts ]  Go to page Previous  1, 2

All times are UTC [ DST ]


Who is online

Users browsing this forum: Apollo, Bing [Bot], engunneer, JSLover, sjc1000, Yahoo [Bot] and 19 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group