Delete All Files
-
this function will be called in exe. When I run exe and call this function, could it be deleted ? or this function can delete runing exe ?
I want to explain my self to you.
My QtWidget App = MyApp.exe
it's running, no problem
Button = call delete() funtion
It can be delete itself ?
@Joe-von-Habsburg On Windows you cannot delete files which are open (this includes executable files). On Linux you can.
-
I tried the
QDir removeRecursivelycommand, and it deleted some folders and files, which worked. However, when I try again withwindeploy, this problem might occur. I shouldn't be able to redeploy.void delete() { QString password = QInputDialog::getText(nullptr, "Delete", tr("Password"), QLineEdit::Password); qDebug() << "password" << password; if(password == "test"){ QDir dir(qApp->applicationDirPath()); bool status = dir.removeRecursively(); qDebug() << "deleted...."; } }@jsulm said in Delete All Files:
On Windows you cannot delete files
There need to be some solutions here.
-
I tried the
QDir removeRecursivelycommand, and it deleted some folders and files, which worked. However, when I try again withwindeploy, this problem might occur. I shouldn't be able to redeploy.void delete() { QString password = QInputDialog::getText(nullptr, "Delete", tr("Password"), QLineEdit::Password); qDebug() << "password" << password; if(password == "test"){ QDir dir(qApp->applicationDirPath()); bool status = dir.removeRecursively(); qDebug() << "deleted...."; } }@jsulm said in Delete All Files:
On Windows you cannot delete files
There need to be some solutions here.
@Joe-von-Habsburg said in Delete All Files:
There need to be some solutions here.
There aren't. Just because you want to delete an open file doesn't mean that Windows will let you do so.
If you really want to delete a currently open file, such as your executable when it is running, the Windows approach is: you may add the file path to a Windows list of files to be deleted on next reboot. The delete will then happen automatically but only after user next reboots. This is typically what installers use. I don't know about windeploy or under Windows. It perhaps has its own installer way of allowing you to mark for deletion on next reboot.
If by any chance you are trying to use this to do something like "hide" your executable from people who want to see it, forget it....
-
I tried the
QDir removeRecursivelycommand, and it deleted some folders and files, which worked. However, when I try again withwindeploy, this problem might occur. I shouldn't be able to redeploy.void delete() { QString password = QInputDialog::getText(nullptr, "Delete", tr("Password"), QLineEdit::Password); qDebug() << "password" << password; if(password == "test"){ QDir dir(qApp->applicationDirPath()); bool status = dir.removeRecursively(); qDebug() << "deleted...."; } }@jsulm said in Delete All Files:
On Windows you cannot delete files
There need to be some solutions here.
@Joe-von-Habsburg said in Delete All Files:
However, when I try again with windeploy, this problem might occur.
What problem? What do you try with windeploy? It is hard to understand what you mean.
An alternative to what @JonB suggested: from your first app (which you want to delete) start another one. The first one then terminates. The second one waits a bit and then deletes the folder where the first one is located.
-
@Joe-von-Habsburg said in Delete All Files:
There need to be some solutions here.
There aren't. Just because you want to delete an open file doesn't mean that Windows will let you do so.
If you really want to delete a currently open file, such as your executable when it is running, the Windows approach is: you may add the file path to a Windows list of files to be deleted on next reboot. The delete will then happen automatically but only after user next reboots. This is typically what installers use. I don't know about windeploy or under Windows. It perhaps has its own installer way of allowing you to mark for deletion on next reboot.
If by any chance you are trying to use this to do something like "hide" your executable from people who want to see it, forget it....
@JonB said in Delete All Files:
f by any chance you are trying to use this to do something like "hide" your executable from people who want to see it, forget it....
No I want to delete for all of them which would like to.
I can give example.
You use my app ok? You have to drop computer and call delete function. When this happens, Joe or anyone won't be able to come and use the app and access the files.
@JonB said in Delete All Files:
Windows approach is: you may add the file path to a Windows list of files to be deleted on next reboot. The delete will then happen automatically but only after user next reboots
I will look and research thanks
@jsulm said in Delete All Files:
An alternative to what @JonB suggested: from your first app (which you want to delete) start another one. The first one then terminates. The second one waits a bit and then deletes the folder where the first one is located.
Yes, it may solve my problem thank you guys
-
@JonB said in Delete All Files:
f by any chance you are trying to use this to do something like "hide" your executable from people who want to see it, forget it....
No I want to delete for all of them which would like to.
I can give example.
You use my app ok? You have to drop computer and call delete function. When this happens, Joe or anyone won't be able to come and use the app and access the files.
@JonB said in Delete All Files:
Windows approach is: you may add the file path to a Windows list of files to be deleted on next reboot. The delete will then happen automatically but only after user next reboots
I will look and research thanks
@jsulm said in Delete All Files:
An alternative to what @JonB suggested: from your first app (which you want to delete) start another one. The first one then terminates. The second one waits a bit and then deletes the folder where the first one is located.
Yes, it may solve my problem thank you guys
@Joe-von-Habsburg said in Delete All Files:
You use my app ok? You have to drop computer and call delete function. When this happens, Joe or anyone won't be able to come and use the app and access the files.
I don't know what this means. If you want to get rid of an installed application use an uninstaller. I cannot imagine why you wish to delete your own executable (or other things of yours) from your own executable.
-
You could open terminal or powershell via QProcess , define a short wait period, in which time you close your current running application and than execute Remove-Item "C:\path\to\folder" -Recurse -Force
that might work in most cases?
Start-Sleep -Seconds 2 Remove-Item "C:\path\to\folder" -Recurse -Forcevery fragile setup, I have to add/say!
-
You could open terminal or powershell via QProcess , define a short wait period, in which time you close your current running application and than execute Remove-Item "C:\path\to\folder" -Recurse -Force
that might work in most cases?
Start-Sleep -Seconds 2 Remove-Item "C:\path\to\folder" -Recurse -Forcevery fragile setup, I have to add/say!
-
The appropriate way to handle self-termination in a platform independent manner is with a batch system. Submit a request to spawn a background job to do it...and no, I'm not going to elaborate on how to create a batch processing system.
-
@J.Hilk said in Delete All Files:
Remove-Item "C:\path\to\folder" -Recurse -Force
It's working, Thank you..
void delete() { const QString appDir = QDir::toNativeSeparators(QCoreApplication::applicationDirPath()); const QString ps = QString( "Start-Sleep -Seconds 2; " "Remove-Item -LiteralPath \"%1\" -Recurse -Force" ).arg(appDir); QString program = "powershell.exe"; QStringList args = { "-NoProfile", "-ExecutionPolicy", "Bypass", "-WindowStyle", "Hidden", "-Command", ps }; const QString workingDir = QDir::tempPath(); qint64 pid = 0; const bool ok = QProcess::startDetached(program, args, workingDir, &pid); if (!ok) { return; } QTimer::singleShot(0, qApp, &QCoreApplication::quit); } -
J Joe von Habsburg has marked this topic as solved