B4J Question [SOLVED] jShell and parameters with spaces in them

JackKirk

Well-Known Member
Licensed User
Longtime User
This is driving me balmy.

I'm using the jShell library to do some wifi massaging on a Windows 10 PC, one code snippet is:
B4X:
Private wrk_sh As Shell
Private wrk_args As List
wrk_args.Initialize

'Set up shell to disconnect WiFi interface on PC
wrk_args.Add("/c")
wrk_args.Add("netsh wlan disconnect interface=" & wrk_interface & " > d:\Swamp\BufferFTPClient-WiFi-Disconnect.txt")
wrk_sh.Initialize("Event_shell2", "cmd.exe", wrk_args)
wrk_sh.Run(10000)
If the variable wrk_interface contains no blanks (e.g. "Wi-Fi") then all works fine.

If the variable wrk_interface contains a blank (e.g. "Wi-Fi 4") then it dies with message:
One or more parameters for the command are not correct or missing.

Usage: disconnect [[interface=]<string>]

Parameters:

Tag Value
interface - Name of the interface from which disconnection is
attempted.

Remarks:

Disconnect from the wireless network on the specified interface.

Parameter interface is required if there are two or more available
interfaces on the system. Wildcard characters can be used in the
interface name to specify multiple interfaces.

Examples:

disconnect
disconnect interface="Wireless Network Connection"

If I try putting in double quotes like:

wrk_args.Add("netsh wlan disconnect interface=""" & wrk_interface & """ > d:\Swamp\BufferFTPClient-WiFi-Disconnect.txt")

then it dies with message:
Error after shell wait (WiFi Disconnect)...
Success: true
ExitCode: 1
StdOut:
StdErr: ''netsh' is not recognized as an internal or external command,
operable program or batch file.

I've scoured the forums for the last 2 hours and didn't find anything that worked.

Any pointers to the probably incredibly stupid thing I am/am not doing greatly appreciated...
 
Last edited:

TnP

Member
Licensed User
Longtime User
One potential work around is to put what you need in a .bat file then use Shell to run the .bat instead. I've been working on something similar recently and it worked for me.
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
TnP, I thought of that but things are messy enough already, I'm hoping there is a cleaner solution.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Shouldn't the wrk_args be
B4X:
wrk_args.AddAll(Array("/C","netsh","wlan","disconnect",$"interface="${wrk_interface}""$,">","d:\Swamp\BufferFTPClient-WiFi-Disconnect.txt"))
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
I am overwhelmed by the support that these forums provide - 3 useful responses in 5 hours.
Try to use Shell.InitializeDoNotHandleQuotes.
Erel's suggestion works a treat, the code snippet in post #1 becomes:
B4X:
Private wrk_sh As Shell
Private wrk_args As List
wrk_args.Initialize

'Set up shell to disconnect WiFi interface on PC
wrk_args.Add("/c")
wrk_args.Add("netsh wlan disconnect interface=""" & wrk_interface & """ > ""d:\Swamp\BufferFTPClient WiFi Disconnect.txt""")
wrk_sh.InitializeDoNotHandleQuotes("Event_shell2", "cmd.exe", wrk_args)
wrk_sh.Run(10000)
Which is the way I would have thought it should work with wrk_sh.Initialize

I can even name my pipe file as d:\Swamp\BufferFTPClient WiFi Disconnect.txt which is what I wanted originally but could not get to work because of the embedded blanks.

Which all raises a further question - why is wrk_sh_Initialize the "default" when it is doing crazies with the parameters?
 
Last edited:
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Your usage is not standard.
Erel, you may be right but I have only ever used jShell to run cmd.exe - admittedly with much simpler arguments in the past.
 
Upvote 0
Top