Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Ouvrir un fichier avec son chemin DOS

2 réponses
Avatar
Marc
Bonjour,

Je cherche a ouvrir un fichier sous VB en lui indiquant le chemin absolu.
Cependant sur certains fichiers l'operation se passe mal et je suppose
fortement que cela est du a la presence d'espace dans le chemin (path) que
je lui indique.

Aussi, je recherche un moyen de transformer un chemin au format WinNT en
chemin DOS (i.e. avec les ~ et les numeros) : existe-t-il une fonctions API
de win ? Ou peut-etre connaissez-vous une autre solution ?

D'avance merci.

Marc

2 réponses

Avatar
Pierre Alexis
Salut Marc,

Tu as écrit :

Aussi, je recherche un moyen de transformer un chemin au format WinNT en
chemin DOS (i.e. avec les ~ et les numeros) : existe-t-il une fonctions API
de win ? Ou peut-etre connaissez-vous une autre solution ?



Réponse sur la FAQ :-) :

Question 30 - Comment trouver le nom court d'un fichier en fonction de son nom
long ?
http://faq.vb.free.fr/index.php?question0

--
Pierre ALEXIS - MVP Visual Basic
Courriel & messenger :
FAQ Visual Basic : http://faq.vb.free.fr/
Avatar
François Picalausa
Bonjour/soir,

faire gaffe quand même que le code n'est pas très optimisé...

Voici une version un peu plus mieux (ça va te faire un "à faire" en plus
pour la faq, Pierre ;-) )

'Une feuille, form1
'contenant 2 commandbuttons, command1 et command2
'et 2 textbox, text1 et text2
Option Explicit

Private Declare Function GetShortPathName _
Lib "kernel32" _
Alias "GetShortPathNameA" _
( _
ByVal lpszLongPath As String, _
ByVal lpszShortPath As String, _
ByVal cchBuffer As Long _
) _
As Long
Private Declare Function GetShortPathNameUnicode _
Lib "kernel32" _
Alias "GetShortPathNameW" _
( _
ByVal lpszLongPath As String, _
ByVal lpszShortPath As String, _
ByVal cchBuffer As Long _
) _
As Long

Private Const MAX_PATH = 260

Private Sub Command1_Click()
Text2.Text = ShortPath(Text1.Text)
End Sub

Private Sub Command2_Click()
'nécessite la version Unicode de l'API
'Win NT, 2k, xp
'ou Windows Me/98/95: GetShortPathNameW supporté par le Microsoft Layer
for Unicode

Text2.Text = ShortPathUnicode(Text1.Text)
End Sub

'La longueur de path doit être < à MAX_PATH
Function ShortPath(Path As String) As String
Dim lgnAPIResult As Long, PathLenght As Long

PathLenght = MAX_PATH

Do
ShortPath = String$(PathLenght, vbNullChar)
lgnAPIResult = GetShortPathName(Path, ShortPath, PathLenght)

'Si le résultat renvoyé est suppérieur à la taille du chemin,
'il faut agrandir le buffer
If lgnAPIResult > PathLenght Then
PathLenght = lgnAPIResult
ShortPath = String$(PathLenght, vbNullChar)
Else
ShortPath = Left(ShortPath, lgnAPIResult)
End If
Loop While lgnAPIResult > PathLenght And lgnAPIResult <> 0
End Function

'Longueur de path < à 32767 caractères
Function ShortPathUnicode(Path As String, Optional UNCPrepend As String ".") As String
Dim lgnAPIResult As Long, PathLenght As Long

PathLenght = MAX_PATH

Do
ShortPathUnicode = String$(PathLenght, vbNullChar)
lgnAPIResult = GetShortPathNameUnicode(StrConv(UNCPrepend & Path,
vbUnicode), ShortPathUnicode, PathLenght)

If lgnAPIResult > PathLenght Then
PathLenght = lgnAPIResult
ShortPathUnicode = String$(PathLenght, vbNullChar)
Else
ShortPathUnicode = Left(StrConv(ShortPathUnicode,
vbFromUnicode), lgnAPIResult)
End If
Loop While lgnAPIResult > PathLenght And lgnAPIResult <> 0
End Function


--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


"Pierre Alexis" a écrit dans le message de
news:
Salut Marc,

Tu as écrit :

Aussi, je recherche un moyen de transformer un chemin au format
WinNT en chemin DOS (i.e. avec les ~ et les numeros) : existe-t-il
une fonctions API de win ? Ou peut-etre connaissez-vous une autre
solution ?



Réponse sur la FAQ :-) :

Question 30 - Comment trouver le nom court d'un fichier en fonction
de son nom long ?
http://faq.vb.free.fr/index.php?question0