Sign in to follow this  
Followers 0
Revolter

Self delete folder [SOLVED]

14 posts in this topic

#1 ·  Posted (edited)

i want the selfdelete func to delete a folder with the file that the func is running from inside it. i tried this but it doesn't works:

$dir = DirGetSize( @ScriptDir , 1 )

If $dir[1] = 1 And FileExists( @ScriptDir & "\Uninstall.exe" ) Then
    FileSetAttrib( @ScriptFullPath , "-R" )
    _SelfDeleteFolder()
EndIf


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

any idea?

really need your help!

Edited by Revolter

[center]Sorry for my bad english. Trying my best :Dhttp://iulianonofrei.comhttp://www.last.fm/user/Revolt666 [/center]

Share this post


Link to post
Share on other sites



type this in a Command Prompt box:

help rmdir


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

it still doesn't work and i dond't know why ...

i changed the "del" line with

& 'rmdir "' & @ScriptDir & '" /S /Q' & @CRLF _

I expect because rd or remdir will not remove a directory which has any files in it.

Maybe DelTree would work .

This worked for me ( I haven't tried DelTree)

[autopit]

_SelfDeleteFolder()

Func _SelfDeleteFolder( $iDelay = 10 )

Local $sCmdFile_folder

FileWrite( @TempDir & "\ans.txt" , "y" & @CRLF )

FileDelete( @TempDir & "\scratch.bat" )

$sCmdFile_folder = 'ping -n ' & $iDelay & ' 127.0.0.1 > nul' & @CRLF _

& ':loop' & @CRLF _

& 'del "' & @ScriptDir & '\*.au3"' & @CRLF _

& 'del "' & @ScriptDir & '\*.exe"' & @CRLF _

& 'rd "' & @ScriptDir & @CRLF _

& 'if exist "' & @ScriptDir & '\." goto loop' & @CRLF _

& 'del %0'

FileWrite( @TempDir & "\scratch.bat" , $sCmdFile_folder )

Run( @TempDir & "\scratch.bat" , @TempDir , @SW_HIDE )

EndFunc

[/autoit]


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Your problem is probably that you can't delete a folder if it contains open files. The batch file is open when it runs, so it can't delete itself.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Your problem is probably that you can't delete a folder if it contains open files. The batch file is open when it runs, so it can't delete itself.

I don't think that's correct. A batch file can delete itself so I assume it's loaded into memory and the file is closed then the batch file runs. Anyway, the batch file is not in the folder that's being deleted, or at least it wasn't in the example I used. I compiled the exe in a sub folder of @Temp. so the au3 script and the exe were in the subfolder but the bat file was in @Temp. Then I ran the exe and it worked, although Windows was totally confused for a while because it still showed the deleted folder in an explorer window, but after closing the window and refreshing the explorer window I also had open showing @Temp, the foder disappeared.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Revolter,

My stab at it ... using your script and a few corrections

Look at first post: --> FileWrite(@TempDir & "scratch.bat" , $sCmdFile_folder)

Should be: ------------> FileWrite(@TempDir & "\scratch.bat" , $sCmdFile_folder)

RMDIR [/Q] [drive:]path

RD [/Q] [drive:]path

/S Removes all directories and files in the specified directory

in addition to the directory itself. Used to remove a directory

tree.

/Q Quiet mode, do not ask if ok to remove a directory tree with /S

Local $dir = DirGetSize(@ScriptDir, 1)
If $dir[1] = 1 And FileExists(@ScriptDir & "\Uninstall.exe") Then
    FileSetAttrib(@ScriptFullPath, "-R")
    _SelfDeleteFolder()
EndIf

Func _SelfDeleteFolder($iDelay = 3)
    If FileExists(@TempDir & "\scratch.bat") Then FileDelete(@TempDir & "\scratch.bat")
    Local $sCmdFile_folder = ':loop' & @CRLF _
             & 'ping -n ' & $iDelay & ' 127.0.0.1 > nul' & @CRLF _
             & 'rmdir /S /Q "' & @ScriptDir & '"' & @CRLF _
             & 'if exist "' & @ScriptDir & '" goto loop' & @CRLF _
             & 'rmdir %0' & @CRLF & 'exit'
    FileWrite(@TempDir & "\scratch.bat", $sCmdFile_folder)
    Run(@TempDir & "\scratch.bat", @TempDir, @SW_HIDE)
EndFunc

- EDIT -

WARNING! - Do not execute this script in a folder that you do NOT want deleted!

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

You might want to try something like this - in case you still have trouble ...

; Once the process closes -- it will exit the loop and go to the next line.
While ProcessExists("Uninstall.exe")
    Sleep(1000)
WEnd

FileChangeDir(@TempDir); in case Windows has lock on the Uninstall folder. (sometimes works)

Local $dir = DirGetSize(@ScriptDir, 1)
If $dir[1] = 1 And FileExists(@ScriptDir & "\Uninstall.exe") Then
    FileSetAttrib(@ScriptFullPath, "-R")
    _SelfDeleteFolder()
EndIf
Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

AdmiralAlkex,

I had a similar problem when a folder wouldn't delete. Even when there was nothing in it.

FileChangeDir fixed that problem.

Windows is quirky like that -- especially if a file was previously executed from it.

Experiment .. you'll see.

Like I said ... "(sometimes works)"

-Edit-

While I'm thinking ...

Most of the time, I can be in a folder working on a script or a compiled exe,

viewing it with an Explorer window. I can delete the folder from another Explorer

window ... and the prevoius window disappears. That is as it should be ... so long

as a file is not in use.

If there were other files in that folder ... some of them might get deleted.

That also .. is as it should be.

But .. If a program is executed in AutoIt (for example) ... and then deleted

when the file closes .. sometimes Windows won't release the empty folder.

Do I have an explaination? No.

I tried all kinds of things to make it work. The only thing that did in that

situation .. was FileChangeDir. My guess, is it loses focus on that folder and

focuses on another folder. Just a guess.

And to be totally honest ..

I've not experienced this particular problem in anything else except AutoIt.

Now .. there was this time when I couldn't delete a folder because it had strange characters in the folder name.

But .... thats a different story. <grin>

-Edit2-

Don't even get me started on Vista .. I've seen it do all kinds of strange things

without using AutoIt at all.

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

ripdad,

i tried your script but nothing happened. and after one minute the folder dissapeared. i hate this :graduated: ... (using win7)

-EDIT-

lol ... it was because of the $iDelay that was set to 10

anyway ... thanks a lot :(

Edited by Revolter

[center]Sorry for my bad english. Trying my best :Dhttp://iulianonofrei.comhttp://www.last.fm/user/Revolt666 [/center]

Share this post


Link to post
Share on other sites

How does changing the current dir unlock files? :D

Ask Bill Gates. But try the following: create a dir, open a dos prompt and 'cd' to that dir. Then try to delete the dir in question from Windows Explorer. Won't work!

By the way, I think "deltree" has been removed from Windows as a command since 2000 or XP, maybe even before :graduated: But rmdir should also do the job :(


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

Revolter - you're welcome.

SadBunny - very nice and reproducible.

cd..

CHANGE DIR - and now you can delete the folder.


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Revolter - you're welcome.

SadBunny - very nice and reproducible.

cd..

CHANGE DIR - and now you can delete the folder.

hey ... i added a msgbox at the end with the text "Uninstalled succesfully!" :graduated:

Local $sCmdFile , $szDrive , $szDir , $szFName , $szExt
_PathSplit( @TempDir , $szDrive, $szDir, $szFName , $szExt )
$sCmdFile = 'ping -n ' & $iDelay & '127.0.0.1 > nul' & @CRLF _
& ':loop' & @CRLF _
& 'del /F "' & @ScriptFullPath & '"' & @CRLF _
& 'if exist "' & @ScriptFullPath & '" goto loop' & @CRLF _
& 'del %0' _
& $szDrive & @CRLF _
& 'cd ' & $szDir & $szFName & @CRLF _
& 'start uninstallmsgbox.exe'
Edited by Revolter

[center]Sorry for my bad english. Trying my best :Dhttp://iulianonofrei.comhttp://www.last.fm/user/Revolt666 [/center]

Share this post


Link to post
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
Sign in to follow this  
Followers 0