0

I'm writing a batch file that runs a command via git-cmd.exe but it doesn't run the command(s) after it.

I've tried to use CALL, START /WAIT, and START /B /WAIT. All have the same behavior. Maybe there is a parameter should be sent to git-cmd.exe to execute the command and exit but I didn't find any guide explaining how to use git-cmd.exe.

This is a sample batch file:

@ECHO OFF
SET "PATH=C:\Ruby26-x64\bin;C:\Program Files\nodejs;%PATH%"
SET "CurrentDirectory=%CD%"
CD /D "%UserProfile%\AppData\Local\GitHub\PortableGit_*\"
SET "GitDirectory=%CD%"
CD /D "%CurrentDirectory%"
"%GitDirectory%/git-cmd.exe" CALL rake build
PAUSE

The command passed to git-cmd.exe is executed but the PAUSE command doesn't execute until I type EXIT command manually in the 'Command Prompt' window.

I've also tried a simple DIR command instead of rake build but the same issue still occurs:

"%GitDirectory%/git-cmd.exe" DIR
PAUSE
M. Hamdi
  • 1
  • 3
  • Try `EXIT` in the command line: `"git-cmd.exe" "DIR; EXIT"` – phd Sep 17 '19 at 14:41
  • @phd the `;` doesn't work. I think it's parsed as a parameter to `dir` command. However, `"git-cmd.exe" "DIR & EXIT"` or `"git-cmd.exe" "DIR && EXIT"` works but this doesn't work with `rake build` command as it needs `call` before it to exit. Even if after adding it to be `"git-cmd.exe" "CALL rake build && EXIT"`, it doesn't work (`EXIT` is not executed). – M. Hamdi Sep 18 '19 at 09:15
  • Thanks all, the issue has been resolved. I'll add the resolution as an answer. There was a conversation here between me and someone else (I think mony) but all his comments has been deleted, don't know why?! – M. Hamdi Sep 22 '19 at 12:13

1 Answers1

0

Thanks to the suggestions, the issue was resolved by passing EXIT command to git-cmd.exe as follows:

@ECHO OFF
SET "PATH=C:\Ruby26-x64\bin;C:\Program Files\nodejs;%PATH%"
SET "CurrentDirectory=%CD%"
CD /D "%UserProfile%\AppData\Local\GitHub\PortableGit_*\"
SET "GitDirectory=%CD%"
CD /D "%CurrentDirectory%"
"%GitDirectory%/git-cmd.exe" "CALL rake build & EXIT"
PAUSE

There was a useful conversation between me and someone else (I think his name was mony) but it was deleted, don't know why?! He sent me this link with the difference between & and && between commands: https://stackoverflow.com/a/25344009/9586127

The & before the EXIT command in order to execute both commands independent on result of the first one. If && is used, the EXIT command will be executed only if the first command succeeded (exit code 0).

In addition, he told me a way to replace the lines 3:6 by one line to be:

FOR /D %%I IN ("%LocalAppData%\GitHub\PortableGit_*") DO SET "GitDirectory=%%I"
M. Hamdi
  • 1
  • 3