Run .bat file or cmd line from c++

I’m trying to perform an ImageMagick operation on some images I’m exporting from the engine. I’ve written a .bat file to do this, but I can also achieve the same result through a commandline operation so either would be a soution.

I’ve tried using FPlatformProcess::CreateProc to run the .bat itself but this seems to do nothing. I’ve also been through many different iterations trying to get CreateProc to open a cmd window to then run the relevant commands, I have more luck with the second, as I see the cmd window open for a split second but it instantly closes (I’ve provided a write and read pipe). Is there something I’m doing wrong? I’ve also looked into using system() and ShellExecute but to no avail,does anyone have any alternate solutions?

Here’s the code I’m using at the moment.

	FString cmdPath = "C:\\Windows\\System32\\cmd.exe";
	FString cmdParams = "cmd /k \"cd C:\\Users\\Admin\\Pictures && magick *T_* out.pdf\"";

	//readPipe and writePipe are void* declared at class level
	FPlatformProcess::CreatePipe(readPipe, writePipe);

	cmdProcHandle = FPlatformProcess::CreateProc(*cmdPath, *cmdParams, true, false, false, &processIDPtr, 0, nullptr, writePipe, readPipe);

	if (!cmdProcHandle.IsValid())
	{
		ensure(false);
	}

For strictly what I need I’ve managed to work out the solution. I was being dumb and thinking about it wrong, I cut out the middle man and just used CreateProc on ImageMagick and provided the parameters to perform the operation I wanted, and it worked a charm. This doesn’t answser the actual problem of using command line or batch files, but if anyone is trying to do specifically what I was trying to do, that might be useful information.

Here is a solution to the command line problem: Open cmd.exe window in UE4 C++ - #3 by AntiGravity

Here is a plugin which allows you to do .bat stuff:
.bat Extension

1 Like