6

When I am on the command line and do this:

"C:\Program Files\TortoiseGit\bin\TortoiseGitProc.exe" /command:log

a GUI dialog of TortoiseGit is opened and cmd.exe immediately returns, meaning that I can immediately run other commands like dir etc.

Because the aforementioned command is quite long, I created a helper batch file, tgit.cmd, that contains just this:

@echo off
"C:\Program Files\TortoiseGit\bin\TortoiseGitProc.exe" /command:%1 %*

I can now call just tgit log which is great, however, there is one difference: the command-line is blocked until I close the TortoiseGit dialog.

I have also tried

cmd /C "C:\Program Files\TortoiseGit\bin\TortoiseGitProc.exe" /command:%1 %*

but that doesn't make any difference. How to immediately return from the batch file?

Yue Lin Ho
  • 2,945
  • 26
  • 36
Borek Bernard
  • 50,745
  • 59
  • 165
  • 240
  • I don't think `start` is what I'm looking for - it returns an error, see below, and its task is to start a program in a new console window, which is not my intention. – Borek Bernard Jan 29 '15 at 10:26

2 Answers2

12

The answer from Oscar is almost correct, but needs a correction

start "" "C:\Program Files\TortoiseGit\bin\TortoiseGitProc.exe" /command:%1 %*

Why?

start command has a curious behaviour: the first quoted argument is used to determine the title of a new cmd window, and no, it doesn't matter that no cmd window will be started. First quoted argument is the title.

That is the reason for the empty double quotes in the previous code. Without it, what the start command sees is

start "title=c:\Progra..." /command:....

and as the start command does not include a /command switch, it fails.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • This works! Can you just please explain to me why `cmd /C ...` doesn't? From the description it seems like a more suitable command. – Borek Bernard Jan 29 '15 at 11:03
  • 1
    @Borek, `cmd /c` starts a new process `inside` of the current console. When the new `cmd` ends then the previous instance will gain control again. – MC ND Jan 29 '15 at 11:06
  • Understood, but the `TortoiseGitProc.exe /command:` takes about 100ms to execute (at least it does when typed manually in cmd.exe) so I don't quite understand why it's blocking the CLI if run from a batch file. Or the other way around - why is the CLI not blocked when this command is typed manually.. Strange, in my eyes. – Borek Bernard Jan 29 '15 at 11:11
  • 2
    @Borek, this is the standard behaviour. There are two kind of applications, console and gui. From command line, console applications block and gui don't block. From batch file both block. See `start /?` – MC ND Jan 29 '15 at 11:19
  • Thanks, that's a very important difference. – Borek Bernard Jan 29 '15 at 14:14
0

Use START to run the command.

start "C:\Program Files\TortoiseGit\bin\TortoiseGitProc.exe" "/command:log"
Oscar
  • 13,594
  • 8
  • 47
  • 75
  • Tried adding quotes around the whole thing but that doesn't help either. I don't think `start` is what I'm looking for. – Borek Bernard Jan 29 '15 at 10:27
  • Put the arguments inside the quotations. I had edited my answer. – Oscar Jan 29 '15 at 10:27
  • Sorry that just doesn't work - it opens a new console Windows which is not the same behavior as running the TortoseGitProc.exe with some parameters directly. – Borek Bernard Jan 29 '15 at 10:30
  • @Borek The START command do exactly what you asked. "Enables a user to start a separate window in Windows from the Windows command line." Maybe you should elaborate your question better. – Oscar Jan 29 '15 at 10:32
  • I suggest you try to install TortoiseGit and try for yourself. I asked about running a TortoiseGitProc.exe command which opens a **GUI** window (not a new command window) and how to move the line that is working fine when I type it on command line to a batch file, where it seems to block the CLI. If anything, CMD command (http://ss64.com/nt/cmd.html) seems to be what I'm after but that doesn't work either for some reason. – Borek Bernard Jan 29 '15 at 10:35
  • @Borek No. You asked, literally: "How to run a command from a batch file and immediately return?" without any mention to GUI. In fact, there is no GUI in a batch. Good luck. – Oscar Jan 29 '15 at 10:50