views:

347

answers:

8

How can I run a windows batch file but hiding the command window? I dont want cmd.exe to be visible on screen when the file is being executed. Is this possible?

A: 

Set the window to hidden?

Matthijs Bierman
+1  A: 

Check this link for details on how to make the cmd hidden -

http://www.codeproject.com/Questions/74684/Hiding-Command-prompt-while-executing-an-bat-file.aspx

Sachin Shanbhag
A: 

You could write a windows service that does nothing but execute your batch file. Since services run in their own desktop session, the command window won't be visible by the user.

Chris O
+11  A: 

If you write an unmanaged program and use CreateProcess API then you should initialize lpStartupInfo parameter of the type STARTUPINFO so that wShowWindow field of the struct is SW_HIDE and not forget to use STARTF_USESHOWWINDOW flag in the dwFlags field of STARTUPINFO. Another method is to use CREATE_NO_WINDOW flag of dwCreationFlags parameter. The same trick work also with ShellExecute and ShellExecuteEx functions.

If you write a managed application you should follows advices from http://blogs.msdn.com/b/jmstall/archive/2006/09/28/createnowindow.aspx: initialize ProcessStartInfo with CreateNoWindow = true and UseShellExecute = false and then use as a parameter of . Exactly like in case of you can set property WindowStyle of ProcessStartInfo to ProcessWindowStyle.Hidden instead or together with CreateNoWindow = true.

You can use a VBS script which you start with wcsript.exe. Inside the script you can use CreateObject("WScript.Shell") and then Run with 0 as the second (intWindowStyle) parameter. See http://www.robvanderwoude.com/files/runnhide_vbs.txt as an example. I can continue with Kix, PowerShell and so on.

If you don't want to write any program you can use any existing utility like CMDOW /RUN /HID "c:\SomeDir\MyBatch.cmd", hstart /NOWINDOW /D=c:\scripts "c:\scripts\mybatch.bat", hstart /NOCONSOLE "batch_file_1.bat" which do exactly the same. I am sure that you will find much more such kind of free utilities.

In some scenario (for example starting from UNC path) it is important to set also a working directory to some local path (%SystemRoot%\system32 work always). This can be important for usage any from above listed variants of starting hidden batch.

Oleg
+1  A: 

Native C++ codified version of Oleg's answer -- this is copy/pasted from a project I work on under the Boost Software License.

BOOL noError;
STARTUPINFO startupInfo;
PROCESS_INFORMATION processInformation;
ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
startupInfo.dwFlags = STARTF_USESHOWWINDOW;
startupInfo.wShowWindow = SW_HIDE;
noError = CreateProcess(
    NULL,                                           //lpApplicationName
    //Okay the const_cast is bad -- this code was written a while ago.
    //should probably be &commandLine[0] instead. Oh, and commandLine is
    //a std::wstring
    const_cast<LPWSTR>(commandLine.c_str()),        //lpCommandLine
    NULL,                                           //lpProcessAttributes
    NULL,                                           //lpThreadAttributes
    FALSE,                                          //bInheritHandles
    CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT,  //dwCreationFlags
    //This is for passing in a custom environment block -- you can probably
    //just use NULL here.
    options.e ? environment : NULL,                 //lpEnvironment
    NULL,                                           //lpCurrentDirectory
    &startupInfo,                                   //lpStartupInfo
    &processInformation                             //lpProcessInformation
);

if(!noError)
{
    return GetLastError();
}

DWORD exitCode = 0;

if (options.w) //Wait
{
    WaitForSingleObject(processInformation.hProcess, INFINITE);
    if (GetExitCodeProcess(processInformation.hProcess, &exitCode) == 0)
    {
        exitCode = (DWORD)-1;
    }
}

CloseHandle( processInformation.hProcess );
CloseHandle( processInformation.hThread );
Billy ONeal
+2  A: 

For any executable file, you can run your program using cmd with "c" parametr:

             /c "your program address"\"YourFileNAme".bat  ->if it's a batch file!

As a final solution, I suggest that you create a .cmd file and put this command in it:

    cmd /c "your program address"\"YourFileNAme".bat
    exit

Now just run this .cmd file.

rain
So, it opens up a command window, that fires up a bat without a window?
BeowulfOF
The command prompt window is hidden, because of the "c" parameter, and after the execution, command prompt will be closed, and during execution it does not show any symptom that the command prompt is running, even if you run command prompt, it will run in a visible form, and I haven't met any problem yet.
rain
I've test this, Please tell me where I'm wrong?
rain
+4  A: 

Using C# it's very easy to start a batch command without having a window open. Have a look at the following code example:

        Process process = new Process();
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.FileName = "doSomeBatch.bat";
        process.Start();
BitKFu
A: 

This little VBScript from technet does the trick:

Const HIDDEN_WINDOW = 12

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")

Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = HIDDEN_WINDOW
Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
errReturn = objProcess.Create("mybatch.bat", null, objConfig, intProcessID)

Edit mybatch.bat to your bat file name, save as a vbs, run it.

Doc says it's not tested in Win7, but I just tested it, it works fine. Won't show any window for whatever process you run

badbod99