Júlio
asked on
C++ Bat file to self-delete (update)
Hello!
How are you?
I'm trying to do a self-update function.
I already did my copy with other name.
I'm trying to use a bath file when i close my app to delete this one.
I'm not understand i need to do. I'm not good with bath files.
I'm executing it here:
I want to delete the actual program and self-delete the bath after.
Ty for you attention and help.
bye
How are you?
I'm trying to do a self-update function.
I already did my copy with other name.
I'm trying to use a bath file when i close my app to delete this one.
void Window::Win::SelfDelete(){
std::string filename = "bat.bat";
//my app
char myApp[MAX_PATH];
GetModuleFileName(0, myApp, MAX_PATH);
//bat
char dir[MAX_PATH];
GetCurrentDirectory(MAX_PATH, dir);
std::string finalDir = std::string(dir) + "\\bat.bat";
std::string bat;
bat += ":Repeat ";
bat += "del " + std::string(myApp);
bat += " if exist " + std::string(myApp) + " goto Repeat ";
bat += " del " + finalDir;
std::ofstream file(filename, std::ios::out);
if (file.is_open()){
file << bat;
file.close();
}
ShellExecute(NULL, "open", finalDir.c_str(), NULL, NULL, SW_HIDE);
}
I'm not understand i need to do. I'm not good with bath files.
I'm executing it here:
case WM_CLOSE:{
Window::Win::Inst().SelfDelete();
DestroyWindow(hWnd);
break;
}
I want to delete the actual program and self-delete the bath after.
Ty for you attention and help.
bye
ASKER
Now it's working:
If someone have a better solution, please tell me.
void Window::Win::SelfDelete(){
std::string filename = "bat.bat";
//my app
char myApp[MAX_PATH];
GetModuleFileName(0, myApp, MAX_PATH);
std::string::size_type pos = std::string(myApp).find_last_of("\\");
std::string myAppFileName = std::string(myApp).substr(pos+1, std::string(myApp).length());
////bat
char dir[MAX_PATH];
GetCurrentDirectory(MAX_PATH, dir);
std::string finalDir = std::string(dir) + "\\bat.bat";
std::ofstream file(filename, std::ios::out);
if (file.is_open()){
file << ":Repeat" << std::endl;
file << "del " + myAppFileName << std::endl;
file << "if exist " + myAppFileName + " goto Repeat " << std::endl;
file << "del ""%0"" ";
file.close();
}
ShellExecute(NULL, "open", finalDir.c_str(), NULL, NULL, SW_HIDE);
}
If someone have a better solution, please tell me.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Open in new window
The problem is that now the bat file not self-deleting.
i worked with bath files like 15 years ago, i'm reading everything again to try to do it.
the "del %0" is not working for me too.