_DosCmd$ Function, Arguments Property (App)

Purpose

Returns the MS-DOS or MS-Windows command string (from the command line).

Syntax

$ = _DosCmd$

$ = App.Arguments

Description

These functions return the arguments of the program without the name of the program.

Example

Global Const __argmax = 50

Global __argc As Int

Global __argv() As String

Local Int32 n

ConvertCMDLine()

Print __argc

Print __argv(0)

For n = 1 To __argc : Print __argv(n) : Next n

Do : Sleep : Until Me Is Nothing

 

Procedure ConvertCMDLine()

// The global variable __argc holds the

// number of commandline arguments after executing ConvertCMDLine().

// Arguments are separated by space(s)

// __argv() is an Array with the split arguments.

// Only __argmax arguments are returned.

// __argv(0) holds the complete path, filename included.

// Note: This routine can not differentiate between spaces in filenames

//       and spaces separating arguments.

Local i As Int = 0, j As Int = 0

Local cmd$

Local LargeArg As Boolean = False

Local a$

ReDim __argv(__argmax)

__argv(0) = App.FileName

// Remove quotes

If Left$(__argv(0), 1) = #34

__argv(0) = Mid(__argv(0), 2)

EndIf

If Right$(__argv(0), 1) = #34

__argv(0) = Left(__argv(0), Len(__argv(0)) - 1)

EndIf

cmd$ = Trim(_DosCmd$) + #32

If Left$(cmd$, 1) <> """"

i = InStr(cmd$, #32)

Else

Debug.Print InStr(cmd$, """", 2)

i = InStr(cmd$, #34, 2) : LargeArg = True

cmd$ = Mid$(cmd$, 2) // remove space at start

EndIf

While i > 0

j++

If LargeArg

// remove space at end

a$ = Left$(cmd$, i - 2)

If Len(a)

__argv(j) = Left$(cmd$, i - 2)

Else

j--

EndIf

LargeArg = False

Else

// only remove space at end

a$ = Left$(cmd$, i - 1)

If Len(a)

__argv(j) = Left$(cmd$, i - 1)

Else

j--

EndIf

EndIf

Exit If (i + 1) > Len(cmd$)

cmd$ = Mid$(cmd$, i + 1)

If Left$(cmd$, 1) <> """"

i = InStr(cmd$, #32)

Else

i = InStr(cmd$, #34, 2) : LargeArg = True

cmd$ = Mid$(cmd$, 2) // remove space at beginning

EndIf

Wend

// Return number of arguments

__argc = j

EndProcedure

The above routine only works if the path does not contain spaces.

Remarks

_DosCmd and App.Arguments only provide the command line parameters. In contrast, _CmdLine also includes the program's full path name.

See Also

_CmdLine

{Created by Sjouke Hamstra; Last updated: 20/09/2014 by James Gaite}