How to pass an argument that starts with "//" to a wsh script?
Asked Answered
P

2

6

If I have the following script (that just prints the first argument to the console):

@if (@X)==(@Y) @end /* JScript comment
    @echo off
    cscript //E:JScript //nologo "%~f0" %*
    exit /b %errorlevel%
@if (@X)==(@Y) @end JScript comment */

WScript.Echo(WScript.Arguments.Item(0));

And I try something like

C:\>test.bat "//test"

I get the following error

Input Error: Unknown option "//test" specified.

Despite the quotes.It is taken as an option to the the windows script host. How can pass an argument that starts with //. Named arguments?

Polythene answered 7/2, 2017 at 12:38 Comment(0)
L
13
cscript //E:JScript //nologo "%~f0" // %*

Pass a double slash to end the cscript own argument parsing.

note: I don't know if it is documented anywhere, but tested on windows 7 and 10

Test script:

Option Explicit

Dim argument

    For Each argument In WScript.Arguments
        WScript.Echo "argument: " & argument
    Next 

    For Each argument In WScript.Arguments.Named
        WScript.Echo "Named: " & argument
    Next 

    For Each argument In WScript.Arguments.UnNamed
        WScript.Echo "UnNamed: " & argument
    Next 

Output (sorry, spanish locale):

W:\>cscript //nologo test.vbs //test
Error de entrada: Opción desconocida "//test" especificada.

W:\>cscript //nologo test.vbs // //test /one two
argument: //test
argument: /one
argument: two
Named: /test
Named: one
UnNamed: two

W:\>cscript test.vbs // //nologo //test /one two
Microsoft (R) Windows Script Host versión 5.812
Copyright (C) Microsoft Corporation. Reservados todos los derechos.

argument: //nologo
argument: //test
argument: /one
argument: two
Named: /nologo
Named: /test
Named: one
UnNamed: two

W:\>
Lactiferous answered 7/2, 2017 at 13:5 Comment(1)
Wow. Great hack! I didn't saw this documented anywhere, but I'll check againPolythene
P
3

It works with named arguments after all.

WScript.Echo(WScript.Arguments.Named.Item("test"));

and

cscript myscript.wsf /test:"//test"
Polythene answered 7/2, 2017 at 12:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.