What is the equivalent argument list% * or $ * for VBScript?

Does VBScript have a list of arguments %* (batch files) or $* (bash script)?

I want to get the exact command line call.

A simplified example:

 cscript //nologo script.vbs /arg1:a -s "ab" 1 c /arg2:"xy" "de" -l '3 4' 

should return:

 /arg1:a -s "ab" 1 c /arg2:"xy" "de" -l '3 4' 

(including quotation marks).

I looked at WScript.Arguments , but it does not return verbatim command line.

+7
source share
2 answers

There is no %* or $* equivalent in VBScript. The WScript.Arguments collection hides the input command line, providing access to arguments as elements within collections.

The only way to find out the necessary information is to request WMI for the current process and read the command line from the process information.

This will give you the full command line used to run the current script.

 Option Explicit ' We need a child process to locate the current script. Const FLAG_PROCESS = "winver.exe" ' WMI constants Const wbemFlagForwardOnly = 32 ' Generate a unique value to be used as a flag Dim guid guid = Left(CreateObject("Scriptlet.TypeLib").GUID,38) ' Start a process using the indicated flag inside its command line WScript.CreateObject("WScript.Shell").Run """" & FLAG_PROCESS & """ " & guid, 0, False ' To retrieve process information a WMI reference is needed Dim wmi Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") ' Query the list of processes with the flag in its command line, retrieve the ' process ID of its parent process ( our script! ) and terminate the process Dim colProcess, process, myProcessID Set colProcess = wmi.ExecQuery( _ "SELECT ParentProcessID From Win32_Process " & _ "WHERE Name='" & FLAG_PROCESS & "' " & _ "AND CommandLine LIKE '%" & guid & "%'" _ ,"WQL" , wbemFlagForwardOnly _ ) For Each process In colProcess myProcessID = process.ParentProcessID process.Terminate Next ' Knowing the process id of our script we can query the process list ' and retrieve its command line Dim commandLine set colProcess = wmi.ExecQuery( _ "SELECT CommandLine From Win32_Process " & _ "WHERE ProcessID=" & myProcessID _ ,"WQL" , wbemFlagForwardOnly _ ) For Each process In colProcess commandLine = process.CommandLine Next ' Done WScript.Echo commandLine 
+7
source

No. But this is pretty trivial.

 For each ag in wscript.arguments CMDLINE = CMDLINE & " " & ag Next wscript.echo mid(CMDLINE, 2) 

or

 For each ag in wscript.arguments If Instr(Ag, " ") = True then CMDLINE = CMDLINE & " " & Chr(34) & ag & Chr(34) Else CMDLINE = CMDLINE & " " & ag End if Next wscript.echo mid(CMDLINE, 2) 

and

 C:\Users\User>cscript //nologo "C:\Users\User\Desktop\New Text Document (3).vbs" cat dog "mouse and cat" cat dog mouse and cat 

This applies to VBScript and VBA.

Both of these basics are hosted by other programs. This is the host that collects information on the command line (if any). The host makes it available to vbs through the object in the wscript case, but not when it is hosted in IE / IIS. And VBA has a feature implemented by the host (implemented by Corel Office, Microsoft office, and VB6).

 Function Declaration Function Command() As Variant Function Command$() As String Runtime Semantics. ο‚§ο€ Returns the argument portion of the implementation dependent command used to initiate execution of the currently executing VBA program. ο‚§ο€ The runtime semantics of Command$ are identical to those of Command with the exception that the declared type of the return value is String rather than Variant. 

Under the hood (I did not delete the paragraphs of the parsing behavior) (and note the ANSI / Unicode differences)

CommandLineToArgvW Function


Parses a Unicode command line string and returns an array of zero-terminated Unicode strings containing the individual arguments found on this command line, as well as a number of arguments similar to the standard argv and argc runtime values ​​of C.

Syntax

 LPWSTR *CommandLineToArgvW( LPCWSTR lpCmdLine, int *pNumArgs ); 

Options

This function accepts command lines containing the name of the program, which is either enclosed in quotation marks or not enclosed in quotation marks.

CommandLineToArgvW has a special interpretation of backslash characters when followed by a quotation mark ("), as follows:

  • 2n backslashes followed by quotes produce n backslashes followed by quotes.

    (2n) + 1 backslash followed by quotes, again produce n backslashes followed by quotes.

    n backslashes not followed by quotation marks will simply produce n backslashes.

GetCommandLine

Gets the command line for the current process.

 LPTSTR WINAPI GetCommandLine(void); 

ANSI console processes written in C can use the argc and argv arguments of the main function to access command line arguments. ANSI GUI applications can use the lpCmdLine parameter of the WinMain function to access the command line, except for the program name. The reason main and WinMain cannot return Unicode strings is because argc, argv, and lpCmdLine use the LPSTR data type for parameters rather than the LPTSTR data type. The GetCommandLine function can be used to access Unicode strings because it uses the LPTSTR data type.

To convert the command line to an array of argv strings, call the CommandLineToArgvW function.

Note. The name of the executable on the command line that the operating system provides to the process does not necessarily match the name on the command line that the calling process provides the CreateProcess function. An operating system may provide a full path to an executable name that is provided without a full path.

+1
source

Source: https://habr.com/ru/post/989875/


All Articles