Executing a "batch script" (.bat) in Windows

2,168 views
Skip to first unread message

Philipp Schumann

unread,
Jun 9, 2013, 7:11:47 PM6/9/13
to golan...@googlegroups.com
So, my %PATH% has a directory containing the sass.bat script:

----

1. Using this..

os/exec.Command("sass.bat")

..returns an error --- exec: "sass.bat": executable file not found in %PATH%

Same with just "sass" instead of sass.bat. (Running both sass and sass.bat directly from the command-line works fine.)


2. Now, I try this alternative..

os/exec.Command("cmd", "/C", "sass.bat")

..returns an error --- exit status 1

Again, same with just "sass" instead of sass.bat. (Again, running both "cmd /C sass" and "cmd /C sass.bat" from the command-line works fine.)

----

So, calling all Windows gophers out there: how can I exec a batch file where its full file path isn't known but is known to be in %PATH% (which is usually enough for cmd /C)?

Philipp Schumann

unread,
Jun 9, 2013, 7:18:26 PM6/9/13
to golan...@googlegroups.com
Pre-emptive:

1. of course, it isn't the exec.Command() func that returns the error, but Run() or CombinedOutput()

2. regardless of "exit code 1", I can tell the sass.bat isn't properly called as it doesn't create a file it is expected to create. When I call it identically but manually from the command-line via cmd /C sass -args, it does create said output file, however.

peterGo

unread,
Jun 9, 2013, 9:00:59 PM6/9/13
to golang-nuts
Philipp,

Put sass.bat in one of the directories that appears in the PATH output
from the set path command. Also, check that .BAT appears in PATHEXT.

sass.bat:

dir

sass.go

package main

import (
"fmt"
"os/exec"
)

func main() {
out, err := exec.Command("sass.bat").CombinedOutput()
if err != nil {
fmt.Println(err)
}
fmt.Println(string(out))
}

Peter

On Jun 9, 7:18 pm, Philipp Schumann <philipp.schum...@gmail.com>
wrote:

brainman

unread,
Jun 9, 2013, 9:48:43 PM6/9/13
to golan...@googlegroups.com
Show us your program and batch file.


...
To run a batch file, you must start the command interpreter; set lpApplicationName to cmd.exe and set lpCommandLine to the following arguments: /c plus the name of the batch file.
...

If you need to find batch file in the PATH, perhaps http://golang.org/pkg/os/exec/#LookPath will help.

Alex

Philipp Schumann

unread,
Jun 10, 2013, 5:46:51 AM6/10/13
to golan...@googlegroups.com
Hi Peter, thanks! Now this is VERY interesting:

1. if I run the program directly from the command-line, it works fine (path and pathext have both been correct from the beginning regarding sass.bat)

2. but the actual use-case is that this program is ITSELF being os/exec.Run() from yet another Go program! And this doesn't work and yields:

exec: "sass.bat": executable file not found in %PATH%

What a mystery!

Philipp Schumann

unread,
Jun 10, 2013, 6:22:17 AM6/10/13
to golan...@googlegroups.com
Issue resolved. The path was recently added and the "outer process" invoking my Go processes -- Sublime Text 2 -- sadly never seems to refresh its PATH information. (More specifically, seems like no running Windows processes' ENV variables are ever refreshed by Windows?!?!) Restarting ST2 made my stuff work.

Ibrahim M. Ghazal

unread,
Jun 10, 2013, 7:24:50 AM6/10/13
to Philipp Schumann, golang-nuts
On Mon, Jun 10, 2013 at 1:22 PM, Philipp Schumann
<philipp....@gmail.com> wrote:
> Issue resolved. The path was recently added and the "outer process" invoking
> my Go processes -- Sublime Text 2 -- sadly never seems to refresh its PATH
> information. (More specifically, seems like no running Windows processes'
> ENV variables are ever refreshed by Windows?!?!) Restarting ST2 made my
> stuff work.
>

I've never heard of any operating system that "refreshes" environment
variables of running processes. You can change them *before* running
the process, but once it's running, you can't modify them from outside
the process.

chris dollin

unread,
Jun 10, 2013, 7:59:21 AM6/10/13
to Ibrahim M. Ghazal, Philipp Schumann, golang-nuts
On 10 June 2013 12:24, Ibrahim M. Ghazal <img...@gmail.com> wrote:

I've never heard of any operating system that "refreshes" environment
variables of running processes. You can change them *before* running
the process, but once it's running, you can't modify them from outside
the process.

I seem to recall that Risc OS's system variables, which are/were its equivalent
of environment variables, were system-global.

Chris

--
Chris "allusive" Dollin
Reply all
Reply to author
Forward
0 new messages