Vaccano
Vaccano

Reputation: 82341

Batch File works when run, but fails when run from app

I have a batch file that looks like this:

@echo off
REM Create the folder to put the converted files in
md PngConverted
REM Iterate all bitmap files
for /r %%F in (*.bmp) do (
     REM Convert the files to PNG.  Resize them so they are no bigger than 1300x1300
     REM Also limit to 250 colors.
     convert -resize 1300x1300 -colors 250 "%%~nF%%~xF" ".\PngConverted\%%~nF.png"
     REM Output to the user that we completed this file.
     echo converted %%~nF%%~xF to png
)

It uses ImageMagick to convert and resize a bunch of images.

If I just double click on it, it works great. Runs with no errors and outputs my converted images.

However, I tried to put it into a console application like this:

static void Main(string[] args)
{
    // Run the batch file that will convert the files.
    ExecuteCommand("ConvertImagesInCurrentFolder.bat");

    // Pause so the user can see what happened.
    Console.ReadLine();
}


static void ExecuteCommand(string command)
{
    int exitCode;
    ProcessStartInfo processInfo;
    Process process;

    processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
    processInfo.CreateNoWindow = true;
    processInfo.UseShellExecute = false;
    // *** Redirect the output ***
    processInfo.RedirectStandardError = true;
    processInfo.RedirectStandardOutput = true;

    process = Process.Start(processInfo);
    process.WaitForExit();

    // *** Read the streams ***
    string output = process.StandardOutput.ReadToEnd();
    string error = process.StandardError.ReadToEnd();

    exitCode = process.ExitCode;

    Console.WriteLine("output>>" + (String.IsNullOrEmpty(output) ? "(none)" : output));
    Console.WriteLine("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error));
    Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand");
    process.Close();
}

And I get the error:

Invalid Parameter - 1300x1300

Why would I get this error when running this from my console app, but not when I run it from the command line?

Upvotes: 0

Views: 47

Answers (1)

Chris du Preez
Chris du Preez

Reputation: 539

There is also a windows CMD command called convert. Make sure that ImageMagick is infact accessable to your application through environment variable PATH or maybe directly specifying your ImageMagick/convert executable path in your batchfile.

so in short i suspect your application is calling CMD convert (converting filesystem) instead of ImageMagick/convert .

Upvotes: 2

Related Questions