Link to home
Start Free TrialLog in
Avatar of Júlio
JúlioFlag for Brazil

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.

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);
}

Open in new window


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;
	}

Open in new window


I want to delete the actual program and self-delete the bath after.

Ty for you attention and help.

bye
Avatar of Júlio
Júlio
Flag of Brazil image

ASKER

I changed to this:

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::ofstream file(filename, std::ios::out);
	if (file.is_open()){
		file << ":Repeat" << std::endl;
		file << "del " + std::string(myApp) << std::endl;
		file << "if exist " + std::string(myApp) + " goto Repeat " << std::endl;
		file << "del " + finalDir << std::endl;
		file.close();
	}

	ShellExecute(NULL, "open", finalDir.c_str(), NULL, NULL, SW_HIDE);
}

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.
Avatar of Júlio

ASKER

Now it's working:

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);
}

Open in new window


If someone have a better solution, please tell me.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial