Many thanks for some excellent code! I've improved it a bit with a couple of optimisations.
[code]
Public Function GetParam(intCount As Integer) As String
Dim blnInside As Boolean
Dim blnQuoted As Boolean
Dim intParamNum As Integer
Dim intPos As Integer
Dim strChar As String
intParamNum = 1
For intPos = 1 To Len(Command)
strChar = Mid$(Command, intPos, 1)
If blnInside Then
If (blnQuoted And strChar = """") Or (Not blnQuoted And strChar = " ") Then
intParamNum = intParamNum + 1
blnInside = False
blnQuoted = False
End If
Else
If strChar = """" Then
If intParamNum > intCount Then Exit Function
blnInside = True
blnQuoted = True
ElseIf strChar <> " " Then
If intParamNum > intCount Then Exit Function
blnInside = True
End If
End If
If blnInside And intParamNum = intCount And strChar <> """" Then
GetParam = GetParam & strChar
End If
Next
End Function
Public Function GetParamCount() As Integer
Dim blnInside As Boolean
Dim blnQuoted As Boolean
Dim intPos As Integer
Dim strChar As String
For intPos = 1 To Len(Command)
strChar = Mid$(Command, intPos, 1)
If blnInside Then
If (blnQuoted And strChar = """") Or (Not blnQuoted And strChar = " ") Then
GetParamCount = GetParamCount + 1
blnInside = False
blnQuoted = False
End If
Else
If strChar = """" Then
blnInside = True
blnQuoted = True
ElseIf strChar <> " " Then
blnInside = True
End If
End If
Next
If blnInside Then GetParamCount = GetParamCount + 1
End Function
[/code]
Reply
I need the same funcionality in my wsh scripts, anyone?
ReplyOriginally posted by: simon eduamah
great!
Originally posted by: kumar
this site vb command very useful me. So u can send more command my e-mail ID.
ReplyOriginally posted by: Dom
After a long time trying to write my own functions, and then searching for code to do this, I finally found your code - and it works perfectly!
Thank you!
ReplyOriginally posted by: Retype
i need to do more with the command line !!
i have to extract variables out out of it
for example -o c:\ -i c:\windows -p *.mp3
so i can use them in my programm as variables
can s.o. help me ??
You can try following logic to achieve what you want
'-o indicates output Directory
'-i indicates input directory
'-p indicates file category
' for example -o c:\ -i c:\windows -p *.mp3
Sub Main()
Dim stringArgs() As String
Dim outDirectory As String
Dim InDirectory As String
Dim fileCategory As String
Dim n As Integer
stringArgs= Split(Trim(Command$), "-")
For n = LBound(stringArgs) To UBound(stringArgs)
Select Case Mid(LCase(stringArgs(n)), 1, 1)
Case ""
' continue
Case "o"
' output Directory
outDirectory = Trim(Mid(stringArgs(n), 3))
Case "i"
' input directory
InDirectory = Trim(Mid(stringArgs(n), 3))
Case "p"
' file category
fileCategory = Trim(Mid(stringArgs(n), 3))
End Select
Next n
End Sub
Reply
Originally posted by: Steven
It is really useful! Thank you.
Reply
Originally posted by: Kenneth
Very Helpful!!!
Reply
Originally posted by: Sergey Podalov
Thanks a lot! ;)
ReplyOriginally posted by: Nathan Fowler
Dim params As Integer
Dim x As Integer
params = GetParamCount
MsgBox params
For x = 1 To params
MsgBox GetParam(x)
Next
This simply displays the parameter count and each individual command-line parameter entered. From this snippet you can easily adapt it to your application.
Kudos to the author of these two fuctions, they are quite useful.
Reply