Detection de fichiers

Résolu
cs_kurky Messages postés 111 Date d'inscription vendredi 24 septembre 2004 Statut Membre Dernière intervention 27 avril 2010 - 2 sept. 2005 à 09:07
Gobillot Messages postés 3140 Date d'inscription vendredi 14 mai 2004 Statut Membre Dernière intervention 11 mars 2019 - 2 sept. 2005 à 16:01
Bonjours,

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

End Function

30 réponses

Gobillot Messages postés 3140 Date d'inscription vendredi 14 mai 2004 Statut Membre Dernière intervention 11 mars 2019 34
2 sept. 2005 à 12:31
les deux String séparées c'est presque obligatoire

sinon il faudrait décomposer la chaîne en Path et Nom et recomposer la chaîne ensuite

à moins que tu n'es pas besoin du nom du fichier

ce qui m'étonnerais

parce que si ta chaîne de départ est "C:\TEMP\temp*.doc"

tu ne pourras pas utiliser la chaîne pour ouvrir le fichier, le vrai nom du fichier étant par exemple "C:\TEMP\temp001.doc"

il est possible aussi de mettre le deuxième argument au niveau général

ça oblige qaund même a isoler le Path pour avoir le chemin+nom complet.



Option Explicit



Dim Fichier As String



Private Sub Command1_Click()

Dim Path As String



Path = "C:\TEMP\temp*.doc"



If FileExists(Path) Then

MsgBox Fichier

End If



End Sub



Private Function FileExists(PathetFichier As String) As Boolean



Dim Str As String

Str = Dir(PathetFichier)



If Str <> "" Then

Fichier = Str

FileExists = True

End If



End Function


Daniel
3
violent_ken Messages postés 1812 Date d'inscription mardi 31 mai 2005 Statut Membre Dernière intervention 26 octobre 2010 2
2 sept. 2005 à 12:41
Violent Ken

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

End Function

suffit et fonctionne très bien.
3
scortex84 Messages postés 379 Date d'inscription mardi 19 mars 2002 Statut Membre Dernière intervention 20 avril 2011 1
2 sept. 2005 à 10:35
Hello,

tu as essayé avec un "getfile" ? Perso, moi cela fonctionne (bon en VB.NET, mais ça doit aussi marcher en VB6) :

str est un tableau de strings

str = IO.Directory.GetFiles(path, "toto*")

Et str reçoit tous les fichiers correspondant à la recherche. Ensuite un str.lenght et ça roule !

Bon courage,

Seb.
0
Gobillot Messages postés 3140 Date d'inscription vendredi 14 mai 2004 Statut Membre Dernière intervention 11 mars 2019 34
2 sept. 2005 à 10:44
le problème c'est qu'il peut y avoir plusieurs noms de fichiers répondant au critère.

il faut aussi pouvoir renvoyer le nom du fichier dans la fonction.

sFile a le chemin complet

fichier n'a que le nom du fichier.



fichier = Dir(sFile)



While fichier <> ""

If GetAttr (fichier) And vbDirectory = 0 Then

FileExists = True

Exit Function

End If

fichier = Dir()

Wend


Daniel
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
violent_ken Messages postés 1812 Date d'inscription mardi 31 mai 2005 Statut Membre Dernière intervention 26 octobre 2010 2
2 sept. 2005 à 10:53
Violent Ken

Voilà une function :

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

Voilà, normalement çà répons à la question !
@+
0
Gobillot Messages postés 3140 Date d'inscription vendredi 14 mai 2004 Statut Membre Dernière intervention 11 mars 2019 34
2 sept. 2005 à 11:09
Function FileExists(Path<strike>EtFichier</strike> As String, Fichier As String) As Boolean



'toujours mettre les déclaratrions au début

Dim X As String, Y As String



<strike>FileExists = False 'inutile

</strike>



<strike>X = Dir(PathEtFichier & "*.*")

</strike>X = Dir(Path & Fichier)



'une chaîne vide n'est pas Null

If X = "" <strike>vbNullString</strike> Then Exit Function



if X <> "" Then

fichier = X

FileExist = True

End If



'tout cela est inutile

<strike>If 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:



</strike>End Function

Daniel
0
cs_kurky Messages postés 111 Date d'inscription vendredi 24 septembre 2004 Statut Membre Dernière intervention 27 avril 2010
2 sept. 2005 à 11:17
Gobillot,

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.

A chaque solution son probleme aparament
0
violent_ken Messages postés 1812 Date d'inscription mardi 31 mai 2005 Statut Membre Dernière intervention 26 octobre 2010 2
2 sept. 2005 à 11:17
Violent Ken

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.
@+
0
Gobillot Messages postés 3140 Date d'inscription vendredi 14 mai 2004 Statut Membre Dernière intervention 11 mars 2019 34
2 sept. 2005 à 11:19
s'il a l'attribut 16 ????

c'est pas un fichier, c'est un répertoire

Daniel
0
cs_kurky Messages postés 111 Date d'inscription vendredi 24 septembre 2004 Statut Membre Dernière intervention 27 avril 2010
2 sept. 2005 à 11:21
oui je sais à quoi correspond 16 mais c'est bien un fichier avec exrtension ".cop" de 10Mo
0
cs_kurky Messages postés 111 Date d'inscription vendredi 24 septembre 2004 Statut Membre Dernière intervention 27 avril 2010
2 sept. 2005 à 11:30
D'ailleurs je viens de remarquer que le vbdirectory restait constamment à 16
Ne faut-il pas le mettre en argument quelque part ?
0
Gobillot Messages postés 3140 Date d'inscription vendredi 14 mai 2004 Statut Membre Dernière intervention 11 mars 2019 34
2 sept. 2005 à 11:32
tient bizarre !

je sais qu'il peut y avoir des noms des répertoires avec extensions

j'ai essayé avec un fichier de mettre l'attribut vbDirectory, c'est impossible

Vb fait une erreur

et les logiciels que j'ai ne le permette pas non plus

je pense donc toujours que ton fichier est un répertoire

Daniel
0
cs_kurky Messages postés 111 Date d'inscription vendredi 24 septembre 2004 Statut Membre Dernière intervention 27 avril 2010
2 sept. 2005 à 11:38
Ben mon fichier .cop est une base de données access.
Je l'ai renommée en .mdb mais ca fait la meme chose.
0
Gobillot Messages postés 3140 Date d'inscription vendredi 14 mai 2004 Statut Membre Dernière intervention 11 mars 2019 34
2 sept. 2005 à 11:39
par définition les constantes ne sont pas modifiables

et un répertoire sera toujours 16







32 --> Archive

16 --> Répertoire

08 --> Label

04 --> System

02 --> Hidden

01 --> read Only



c'est comme ça depuis le début et c'est pas modifiable.

Daniel
0
cs_kurky Messages postés 111 Date d'inscription vendredi 24 septembre 2004 Statut Membre Dernière intervention 27 avril 2010
2 sept. 2005 à 11:41
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 ...
0
Gobillot Messages postés 3140 Date d'inscription vendredi 14 mai 2004 Statut Membre Dernière intervention 11 mars 2019 34
2 sept. 2005 à 11:43
et comment réagit le syteme ?

quand tu fais propriétés

il devrait faire type ==> Dossier de Fichier

et non pas fichier MDB

Daniel
0
violent_ken Messages postés 1812 Date d'inscription mardi 31 mai 2005 Statut Membre Dernière intervention 26 octobre 2010 2
2 sept. 2005 à 11:43
Violent Ken

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.
0
cs_kurky Messages postés 111 Date d'inscription vendredi 24 septembre 2004 Statut Membre Dernière intervention 27 avril 2010
2 sept. 2005 à 11:47
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 :)
0
violent_ken Messages postés 1812 Date d'inscription mardi 31 mai 2005 Statut Membre Dernière intervention 26 octobre 2010 2
2 sept. 2005 à 11:50
Violent Ken

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.

C'est bien ce que tu voulais faire, non ?
0
Gobillot Messages postés 3140 Date d'inscription vendredi 14 mai 2004 Statut Membre Dernière intervention 11 mars 2019 34
2 sept. 2005 à 11:57
ben oui c'est normal parce c'est un fichier

donc le Test ne sert à rien
ce qui simplifie grandement la fonction

je te conseille de mettre le Path et le nom du fichier séparément





Function FileExists(Path As String, Fichier As String) As Boolean



Dim Str As String

Str = Dir(Path & Fichier)



if Str <> "" Then

Fichier = Str

FileExist = True

End If



End Function



Daniel
0
Rejoignez-nous