Macro Help: Run a batch file (.bat) in Windows

I’m having a hard time figuring out how to run a batch file using a macro. I’m using the most recent version of Libreoffice (portable).

This code opens the batch file in notepad as expected.

Dim Path as string, EXE as string
EXE = "C:\Windows\System32\notepad.exe"
Path = """D:\Test.bat"""
Shell(EXE,  1, Path) 

This code doesn’t work and I have no idea why. The terminal windows doesn’t even open. I tried putting the full path to cmd after just using “cmd” didn’t work.

Dim Path as string, EXE as string
EXE = "C:\Windows\System32\cmd.exe"
Path = """D:\Test2.bat"""
Shell(EXE,  1, Path) 

Help would be appreciated.

Try Path = "/C START ""D:\Test2.bat"""

What interesting questions you ask! Either dangerous file hyperlink warning , or running a batch file… Tell me honestly, which bank’s security system are you going to hack?

1 Like

Lol yes, I gave up on my first question about disabling hyperlink warning. Macro warnings can be disabled, so this is fine for a workaround.

Using START does work. However I’ve run into another odd issue. The quotations around the file path is very important if there’s any spaces in the file path. But it appears the shell command is stripping out the quotations which prevents me passing any file paths that include spaces. So if my file is named “Test 2.bat”, the code no longer works. Is there a workaround for this?

Path = """D:\Test 2.bat"""
Shell(EXE,  1, "/C START "" "" " & Path) 

And what to run bat directly?

Sub runShell
	dim exe$
	exe="""D:\Test 2.bat"""
	Shell(exe, 1)
End Sub
1 Like

Yes. Rename the batch file. :wink:

1 Like

Maybe using convertToUrl function you can solve this.

Then cmd.exe would have to handle %20 etc. ?? Until now I have not tried, but out of curiosity I will do later…
.
As cmd is/was for batch files I suggest (like @Zizi64 ) to avoid space in names, if possible.