Soyez le premier à donner votre avis sur cette source.
Snippet vu 10 725 fois - Téléchargée 46 fois
Public Function RtnGetCommandLine(ArgArray() As String) _ As Integer 'Purpose: Save all command line arguments to an array and return ' Number of command line arguments. Assumes command line ' arguments are delimited by one space or a tab ' 'Parameters: ArgArray: Uninitialized String Array in which ' Command Line Arguments will be saved ' 'Returns: Number of Command Line Arguments ' 'Example: 'Dim sArray() As String 'Dim iCtr As Integer 'MsgBox RtnGetCommandLine(sArray) 'displays number of command ' line arguments 'For iCtr = 0 To UBound(sArray) ' Debug.Print sArray(iCtr) 'Outputs each argument 'Next 'NOTE: As written, will not work if there are more than 10 'Command line arguments, but you can change this easily if 'necessary. '************************************************************ 'Declare variables. Dim C As String Dim CmdLine As String Dim CmdLnLen As Integer Dim InArg As Boolean Dim I As Integer Dim NumArgs As Integer 'Initialise variables ReDim ArgArray(10) NumArgs = 0 InArg = False 'Get command line arguments. CmdLine = Command() CmdLnLen = Len(CmdLine) 'Go thru command line character at a time. For I = 1 To CmdLnLen C = Mid(CmdLine, I, 1) 'Test for space or tab. If (C <> " " And C <> vbTab) Then 'Neither space nor tab. 'Test if already in argument. If Not InArg Then 'New argument begins. 'Test for too many arguments. If NumArgs = 10 Then Exit For NumArgs = NumArgs + 1 InArg = True End If 'Concatenate character to current argument. ArgArray(NumArgs) = ArgArray(NumArgs) & C Else 'Found a space or tab. 'Set InArg flag to False. InArg = False End If Next I 'Resize array just enough to hold arguments. ReDim Preserve ArgArray(NumArgs) RtnGetCommandLine = NumArgs End Function
si j'ai ça en paramètres :
"E:\VB\TEST et Travail\TRUC.txt" "F:\Coucou.txt", le remplacement des espaces donnera :
"E:\VB\TEST,et,Travail\TRUC.txt","F:\Coucou.txt" et le split donnera :
0: "E:\VB\TEST
1: et
2: Travail\TRUC.txt"
3: "F:\Coucou.txt"
Je précise pour ceux qui n'aurait pas compris, je ne suis pas maître de mes paramètres... C'est windows qui me les fournis... Et je dois me débrouiller avec ça... D'où ma procédure du dessus...
Voici le source modifié :
If Command <> "" Then
Dim Arguments() As String
Dim NewArg() As String
Dim i As Integer
Dim NbArg As Integer
Dim TempPath As String
Dim F As Integer
Dim LigneCmd As String
LigneCmd = Replace(Command, Chr(34), "")
Arguments = Split(LigneCmd, " ")
For i = 0 To UBound(Arguments)
TempPath = TempPath & Arguments(i)
If CBool(PathFileExists(TempPath)) Then
'Argument ok
NbArg = NbArg + 1
ReDim Preserve NewArg(NbArg - 1)
NewArg(NbArg - 1) = TempPath
TempPath = ""
Else
'Argument nok
TempPath = TempPath & " "
End If
Next i
For i = 0 To UBound(NewArg)
If Mid(NewArg(i), InStrRev(NewArg(i), "") + 1) <> Cte_LogFileName Then
Call OpenFile(NewArg(i), Mid(NewArg(i), InStrRev(NewArg(i), "") + 1))
End If
Next i
End If
En fait, j'utilise un petit artifice qui consiste à recréer mon chemin d'accès jusqu'à ce qu'il soit reconnu comme valide.
Ex : Sous Win 2000/Xp, je passe "E:\VB\TEST et Travail\TRUC.txt" en paramètre (Le but étant dans mon appli d'ouvrir ce fichier si un utilisateur a effectué la commande windows "envoyer vers" mon appli - Il s'agit d'un editeur de texte...)
Je supprime les guillemets, puis ma fonction Split me donne un tableau de 3 élements :
0) E:\VB\TEST
1) et
2) Travail\TRUC.txt
Au premier passage, je teste grace à l'API PathFileExists si le chemin en 0 (E:\VB\TEST) est valide
Il ne l'ai pas, donc j'ajoute un espace, et je concatène au suivant
Au deuxième passage, je teste si le chemin 'E:\VB\TEST et' est valide
Au troisième, je teste si le chemin 'E:\VB\TEST et Travail\TRUC.txt' est valide
Il l'est, donc je peux placer ce chemin dans un nouveau tableau, qui sera exploité pour ouvrir mes fichiers...
A noter que sous win nt, le problème ne se pose pas... les chemins d'accès sont au format dos, et sans espaces...
Cette gestion en supprimant les guillemets, et en testant la validité du chemin permet de fonctionner sous 98, nt, 2000, xp...
Enfin, pour ceux qui serait arriver jusque là :-), je vais essayer de répondre à la question de SALAZAR.
Pour passer en paramètres dans la commande exécuter, tout dépend de ton système d'exploit...
Sous Win NT :
Lig_Cde.exe play passe l'argument play
Sous 2000 XP :
Lig_Cde.exe "play" passe l'argument play
J'espère avoir répondu à ta question...
Split(string, ",")
La fonction Split permet le découpage automatique d'une chaine via un caractère de séparation et place le résultat dans un tableau.
Le contexte : Il s'agit d'un editeur de texte. Lorsque je fais "envoyer vers" mon exe, windows place le chemin d'accès de mon fichier dans les arguments de lancement.
Par ex, pour editer C:\Truc.txt et C:\Truc2.txt, windows effectue l'opération suivante :
Monexe.exe C:\Truc.txt C:\Truc2.txt
La fonction command séparant les arguments par un espace, il est donc possible de faire comme ceci :
If Command <> "" Then
Dim Arguments() As String
Dim i As Integer
Arguments = Split(Command, " ")
For i = 0 To UBound(Arguments)
Call OpenFile(Arguments(i), Mid(Arguments(i), InStrRev(Arguments(i), "") + 1))
Next i
End If
Le tableau Arguments contient tous les paramètres fournis (sans limite), et donc tous les fichiers à ouvrir. Je lance donc pour chaque paramètre un "openfile".
C'est immédiat et rapide.
Toutefois, j'ai un problème. Les chemins d'accès ne sont pas toujours au format "dos", et si un chemin contient des espaces (par ex : E:\VB\TEST et Travail\TRUC.txt), la fonction plante... de même pour ta fonction.
Un "Envoyer vers" envoie les n fichiers au format dos alors qu'un "ouvrir avec" les envoient au format Windows...
Ex :
DOS : E:\VB\TESTET~1\TRUC.txt
Windows : E:\VB\TEST et Travail\TRUC.txt
Le dos m'arrange car chaque chemin d'accès est séparé par un espace, contrairement au windows...
Quelqu'un aurait-il une solution miracle ?
Merci d'avance.
Voici une autre méthode (moi je préfère les collection aux tableaux)
Private Function getParametre() As Collection
Dim str_CmdLine As String
Dim i As Integer
Dim col_Temp As Collection
Dim col_Resultat As Collection
Dim str_Temp As String
Dim bln_Guillemet As Boolean
Set col_Resultat = New Collection
bln_Guillemet = False
str_CmdLine = UCase(Command())
Set col_Temp = ChaineVersCollection(str_CmdLine, " ") 'Fonction qui prend en paramètre une chaine et le séparateur et renvoie une collection en fonction du séparateur ; copyright de la société, je ne peux pas fournir le source, désolé
For i = 1 To col_Temp.Count
If InStr(1, col_Temp.Item(i), """") Then
If bln_Guillemet Then
str_Temp = str_Temp & " " & Replace(col_Temp.Item(i), """", "")
col_Resultat.Add str_Temp
bln_Guillemet = False
Else
str_Temp = Replace(col_Temp.Item(i), """", "")
bln_Guillemet = True
End If
Else
If bln_Guillemet Then
str_Temp = str_Temp & " " & col_Temp.Item(i)
Else
col_Resultat.Add col_Temp.Item(i)
End If
End If
Next i
Set getParametre = col_Resultat
End Function
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.