OK. I found this, but now the question is how to delete the folder where the executable itself is/was situated. I tried but in all cases using RemoveDirectory didn't yield any result. In MSDN it is written that the folder must be empty, so I copy my executable elsewhere delete all files in the folder, but I can't delete the folder itself. Using SHFileOperation gives good results - it deletes the folder, but if there are files in there it deletes them too. I want to delete the folder only if it is empty. Any suggestions? I know I can first check if it is empty and so on, but I think that there should exist a way to do it simpler. I just don't know why RemoveDirectory won't work on empty folder...

Posted on 2004-12-20 05:36:14 by siddhartha
Anyone on this subject? f0dder, I know you can help! :)

Posted on 2004-12-24 19:20:36 by siddhartha
you first much check if there are files in it. theres no other way.
Posted on 2004-12-25 01:35:29 by Qages
I think you can a simple way, that is: a empty folder will have size zero (0 byte)
Posted on 2004-12-25 03:27:53 by TQN
If I remember correctly this site http://www.catch22.net/ had a big article on the topic. Unfortunatly the site seems to be down at the moment but hopefully its just a temporary thing.
Posted on 2004-12-25 04:28:22 by Eóin
Embed an "D3L3T3.exe" as a resource in your app.

In your app: copy the data of the D3L3T3.exe resource into a file in the temporary folder. Execute this file. Exit immediately.

In D3L3T3.exe - which is now in the temporary folder, sleep 5 to 10 seconds - to make sure your main app has exited. Then, use my proc ForEachFileIn to delete all files and subfolders where your main app is. Finally, use RemoveDirectory to remove the main folder of your app. Exit

ForEachFileIn can be found in the sourcecode forum of the board.
Posted on 2004-12-25 04:57:32 by Ultrano
Ultrano: Isn't it much simpler to use SHFileOperation with filemask set to files you want to delete. If it's installer/unistaller type of application than setting the correct filemask wont be a problem as you know exactly which file were copied. Also this way any newly added files wont be deleted. Than you could check if directory empty with FindFirstFile or check for size of directory to be zero (note that even if the directory can be of zero size, there still can be zero length files) . And finally the main directory can be removed if it's empty.
Posted on 2004-12-25 08:37:29 by arafel
Aha, track your algo and see it's more steps ;)
Yes, it is useful to not delete some files, so your approach should be preferred in cases you have made sure all files have the same mask and want to keep user's data.
But if an user wants some app deleted, he'll want all damn savestates removed too, in many cases. I hate seeing such leftover files that I have to delete myself.
Posted on 2004-12-25 08:49:11 by Ultrano
I use to read about this here at the board but loss interest because it was SO much more going on here at the time. Have you done a search. I hope so. It's here somewhere and it is very informative. Use some simple AND creative key words and go from there. If you can't find it i search my hard drive for saved posts. It's not organized but will be before Wed, hopefully.

Bottom line it can be done but it leave a EMPTY .bat file or something on your hard drive that can be deleted at a latter time. That was the best that you could end up with as far as i remeber.
Posted on 2004-12-25 09:03:23 by cmax
inject into explorer.exe via createremotethread()/debugactiveprocess() kernel internal function in 9x, and DeleteFile.
Posted on 2004-12-25 09:32:29 by Drocon
One easy way to make it possible for you to delete your executable while it's still running is to migrate the view that has been mapped of the executable away from a file-backed section mapping to a pagefile backed memory region. This can be done by first allocating a temporary region of memory that is the same size as the executable view and then copying contents of the executable's view into said region. After this occurs the original region that is a view of the file-backed section should be unmapped (thus dropping the reference to the executable file and making it deletable). Finally, the region that the executable was original mapped at should be allocated with VirtualAlloc (or using a pagefile-backed section) and populated from the contents of the temporary buffer after which point the temporary region can be deallocated. After the executable has been migrated to run entirely from memory you will be able to delete the file on disk assuming there are no other outstanding references to it.

An important thing to note is that the code that does the migration must not be a part of the executable's mapped memory region as said region will be unmapped half way through the migration. Instead, the migration code should be copied into another temporary memory region and be executed from there.

The risks you have with this approach are primarily related to threads. If your application is multithreaded you must suspend all of your threads (minus the thread that is doing the migration) else risk crashes due to referencing invalid memory during the course of the migration.
Posted on 2004-12-25 12:44:20 by nohaven
I'd go with the "drop small deinstall finalization app in user temp dir and have it clean up". It works, it's clean, requires no fancy code, and it won't break on future versions of windows.
Posted on 2004-12-25 20:30:40 by f0dder
So, f0dder, you want this executable that will do the job to stay forever in temp? I don't like this way of doing things, but now I'm too high to think about such kind of things. Peace!

Posted on 2004-12-25 23:04:20 by siddhartha
Hi siddhartha !
I think you need to write a small exe. It receive a directory path through command line, scan and delete all files, subdirs in that directory.
You add that small exe to your main uninstall exe resource. When the uninstall exe run, it will extract the small exe to temp directory, mark it is as Delete as next boot (by win.ini, registry or MoveFile(Ex)), call it and terminate.
Posted on 2004-12-26 04:26:23 by TQN

you want this executable that will do the job to stay forever in temp?

why cant you use PendingFileRename key in registry to delete the exe
during next boot why should it remain in there for ever ??

well i just adddressed this query i have not read through the thread
so sorry if it was addressed or you have posted some adverse comments against using it

the key is in hklm/system/current control set/control/session manger
Posted on 2004-12-26 06:24:17 by bluffer

So, f0dder, you want this executable that will do the job to stay forever in temp?

Nope, you add it to whatever INI file or registry entry or MoveFileEx to have it deleted on next windows boot (as already mentioned by other people). Also, the exe file can be *very* small, and rather have a <=4kb exe floating around in a *temp* dir, than having an app that might break on future windows versions.
Posted on 2004-12-26 09:17:25 by f0dder
Yeah, you're right. Thanks, folks, you helped a lot!

Posted on 2004-12-26 22:54:18 by siddhartha