Br4m3000
Br4m3000

Reputation: 73

How to execute vbs file in java, which directory contains spaces

Im currently coding a program, but i need to make it execute a vbs file. TempDir.vbs. However, the directory to this file contains spaces. Unfortunally, all other topics dont work when the directory contains spaces. In my case: C:\\Users\\"the user"\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup

the code im currently using is:

Runtime.getRuntime().exec("wscript.exe " + "\"\"\"" + path + "\"\"\"" + "TempDir.vbs");

So, how can i execute the file TempDir.vbs.

Upvotes: 3

Views: 3277

Answers (1)

Andreas
Andreas

Reputation: 159165

Instead of using Runtime.exec(String), use Runtime.exec(String[]):

Runtime.getRuntime().exec(new String[] {
    "wscript.exe",
    path + "TempDir.vbs"
});

As mentioned in a comment to a now deleted answer by ziesemer, if the .vbs file is a console script, you might need to use cscript.exe. See this for explanation: Difference between wscript and cscript

Upvotes: 1

Related Questions