J'ai un problème lors de la détection de fichiers. Ma fonction me détecte bien mes fichiers lorsque je lui passe le nom exact du fichier. Hors j'ai également besoin de l'utiliser dans un cas ou je ne connais qu'une partie du nom (pour des fichiers temporaires commencant par "tmp"). Mais lorsque je passe le nom du fichier "tmp*.*", il ne me trouve rien.
Voici le code :
Function FileExists(ByRef sFile As String) As Boolean
On Error GoTo ErrExp
FileExists ((GetAttr(sFile) And vbDirectory) 0)
ErrExp:
If Err.Number <> 0 Then
FileExists = False
Else
FileExists = True
End If
Kurky, tu as dit
J'ai un problème lors de la détection de fichiers. Ma fonction me détecte bien mes fichiers lorsque je lui passe le nom exact du fichier. Hors j'ai également besoin de l'utiliser dans un cas ou je ne connais qu'une partie du nom (pour des fichiers temporaires commencant par "tmp"). Mais lorsque je passe le nom du fichier "tmp*.*", il ne me trouve rien.
moi j'ai compris que tu ne connaissait pas la TERMINAISON du fichier, alors que c'est aussi une partie du nom du fichier que tu ne connais pas. Dans ce cas, tout le monde est d'accord, un simple
Private Function FileExists(PathetFichier As String) As Boolean
Dim Str As String
Str = Dir(PathetFichier)
If Str <> "" Then
Fichier = Str
FileExists = True
End If
Function FileExists(PathEtFichier As String, Fichier As String) As Boolean
FileExists = False
Dim X As String, Y As String
X = Dir(PathEtFichier & "*.*")
If X = vbNullString Then Exit FunctionIf LCase(X) LCase(Fichier) Then FileExists True: Exit Function
On Error GoTo Err:
Y = Left$(X, InStr(X, ".") - 1)If LCase(Y) LCase(Fichier) Then FileExists True: Exit Function
Err:
End Function
Exemple :
Existence=FileExists("c:\temporaire\temp","temp")
Si il existe des fichiers "temp" & n'importe quelle terminaison (ou "temp" sans terminaison), Existence=true autrement Existence=false
J'ai deja utilisé un code comme celui cie t je sais qu'il marche dans le cas qu'il me faut.
Le probleme c'est qu'il ne me trouve pas un fichier au démarrage alors qu'il est vraiment présent car le vbdirectory est a 16.
Or le fichier trouvé est un fichier avec une extension ".cop" dans ce cas.
Bon.
Message pour Gobillot : avant de tout rayer bêtement, essaye MA fonction et TA fonction. Tu verras que la mienne fonctionne, et que la tienne NE MARCHE PAS.
Un conseil, la prochaine fois réfléchit un peu avant d'écrire des anneries.
@+
Mais je me demande si le GetAttr est vraiment indispensable ...
Parce que si je le supprime en laissant la condition if fichier <> "" then alors ca marchera ...
Utilise
Function FileExists(PathEtFichier As String, Fichier As String) As Boolean
FileExists = False
Dim X As String, Y As String
X = Dir(PathEtFichier & "*.*")
If X = vbNullString Then Exit FunctionIf LCase(X) LCase(Fichier) Then FileExists True: Exit Function
On Error GoTo Err:
Y = Left$(X, InStr(X, ".") - 1)If LCase(Y) LCase(Fichier) Then FileExists True: Exit Function
Err:
End Function
Tu verras, çà marche.
La solution de virer le getattr marche nikel
Ta fonction ne peut pas marcher Daniel .
Dans ta condition tu met "and vbdirectory =0" ce qui sera toujours false. donc ton if ne sert a rien.
Violent ken le nom de ma fonction est osi fileexists mais je ne doit avoir qu'un seul argument :)
Remplace ta fonction par la mienne.
Pour PathEtFichier tu mets par exemple "c:\temporaires\temp" et pour Fichier tu mets "temp". Ma fonction te donneras True si il existe un fichier
"c:\temporaires\temp" ou
"c:\temporaires\temp.tmp" ou
"c:\temporaires\temp.doc" ou
"c:\temporaires\TEMP.TmP"....
et te revverras False s'il n'existe pas de fichier temp.