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

Extraire seulement le nom + l'extension dun répertoire

7 réponses
Avatar
fetnat
Bonjour,

La macro suivante permet d'obtenir le chemin et le nom de fichier en
précisant le type d'extension.

1 - Comment faire pour obtenir seulement le nom des fichiers + l'extension ?

2 - Et si c'est possible, comment faire pour obtenir seulement le nom ?

Ce deuxième point est moins crucial car j'ai une boucle pour supprimer
l'extension. Ce n'est pas élégant mais ça fonctionne.

Merci pour votre aide

Cordialement

Fetnat


Sub AllFilesByType()
' list all files in specified dir (fullpath with extension)
Dim fs, i As Integer, x As Integer
Set fs = Application.FileSearch
With Sheets("Contents")
.Cells.Clear
End With
With fs
.LookIn = "C:\Windows"
.Filename = "*.txt"
.Execute
For i = 1 To .FoundFiles.Count
x = x + 1
Range("A" & x) = .FoundFiles(i)
Next i
If .FoundFiles.Count = 0 Then
MsgBox "No files founded."
End If
End With
End Sub

7 réponses

Avatar
JPMonnier
Bonjour,
Sub Fichiers()
Set fs = Application.FileSearch
With fs
.LookIn = "d:photos" 'Répertoire de ton choix
.Filename = "*.jpg" 'extension de ton choix
If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) > 0 Then
MsgBox .FoundFiles.Count & _
" fichier(s) trouvé(s)."
For i = 1 To .FoundFiles.Count
'MsgBox .FoundFiles(i)

Cells(1 + x, 10) = .FoundFiles(i)
x = x + 1
Next i
Else
MsgBox "Aucun fichier n'a été trouvé."
End If
End With
End Sub
--
Cordialement


"fetnat" a écrit dans le message de
news:
Bonjour,

La macro suivante permet d'obtenir le chemin et le nom de fichier en
précisant le type d'extension.

1 - Comment faire pour obtenir seulement le nom des fichiers + l'extension
?

2 - Et si c'est possible, comment faire pour obtenir seulement le nom ?

Ce deuxième point est moins crucial car j'ai une boucle pour supprimer
l'extension. Ce n'est pas élégant mais ça fonctionne.

Merci pour votre aide

Cordialement

Fetnat


Sub AllFilesByType()
' list all files in specified dir (fullpath with extension)
Dim fs, i As Integer, x As Integer
Set fs = Application.FileSearch
With Sheets("Contents")
.Cells.Clear
End With
With fs
.LookIn = "C:Windows"
.Filename = "*.txt"
.Execute
For i = 1 To .FoundFiles.Count
x = x + 1
Range("A" & x) = .FoundFiles(i)
Next i
If .FoundFiles.Count = 0 Then
MsgBox "No files founded."
End If
End With
End Sub


Avatar
fetnat
Bonjour,

Quelle rapidité !

En ajoutant : Range("A" & x) = .FoundFiles(i), j'obtiens le chemin +
fichier + extension.

Je n'arrive pas à manipuler fs.foundfiles.name ou quel chose...

Comment obtenir seulement fichier + extension ? Voire seulement fichier ?

Merci pour votre aide

Fetnat


JPMonnier a écrit :
Bonjour,
Sub Fichiers()
Set fs = Application.FileSearch
With fs
.LookIn = "d:photos" 'Répertoire de ton choix
.Filename = "*.jpg" 'extension de ton choix
If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) > 0 Then
MsgBox .FoundFiles.Count & _
" fichier(s) trouvé(s)."
For i = 1 To .FoundFiles.Count
'MsgBox .FoundFiles(i)

Cells(1 + x, 10) = .FoundFiles(i)
x = x + 1
Next i
Else
MsgBox "Aucun fichier n'a été trouvé."
End If
End With
End Sub


Avatar
JPMonnier
Re,
Sub Fichiers()
Repertoire = "d:images" 'a adapter
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDossier = objFSO.GetFolder(Repertoire)
If (objDossier.Files.Count > 0) Then
For Each objFichier In objDossier.Files
If (InStr(1, objFichier.Name, ".jpg", 1) > 0) Then '".jpg" à adapter
Cells(1 + x, 10) = objFichier.Name
x = x + 1
End If
Next
End If
End Sub

--
Cordialement

"fetnat" a écrit dans le message de
news:OG%
Bonjour,

Quelle rapidité !

En ajoutant : Range("A" & x) = .FoundFiles(i), j'obtiens le chemin +
fichier + extension.

Je n'arrive pas à manipuler fs.foundfiles.name ou quel chose...

Comment obtenir seulement fichier + extension ? Voire seulement fichier ?

Merci pour votre aide

Fetnat


JPMonnier a écrit :
Bonjour,
Sub Fichiers()
Set fs = Application.FileSearch
With fs
.LookIn = "d:photos" 'Répertoire de ton choix
.Filename = "*.jpg" 'extension de ton choix
If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) > 0 Then
MsgBox .FoundFiles.Count & _
" fichier(s) trouvé(s)."
For i = 1 To .FoundFiles.Count
'MsgBox .FoundFiles(i)

Cells(1 + x, 10) = .FoundFiles(i)
x = x + 1
Next i
Else
MsgBox "Aucun fichier n'a été trouvé."
End If
End With
End Sub




Avatar
fetnat
Merci JP, c'est une merveille

Cordialement

Fetnat

JPMonnier a écrit :
Re,
Sub Fichiers()
Repertoire = "d:images" 'a adapter
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDossier = objFSO.GetFolder(Repertoire)
If (objDossier.Files.Count > 0) Then
For Each objFichier In objDossier.Files
If (InStr(1, objFichier.Name, ".jpg", 1) > 0) Then '".jpg" à adapter
Cells(1 + x, 10) = objFichier.Name
x = x + 1
End If
Next
End If
End Sub



Avatar
fetnat
Merci Corto

C'est aussi une merveille pour une perfection rêvée avec la petite
variante qui supprime l'extension...

Cordialement

Fetnat



Corto a écrit :
Bonjour fetnat,

Sub Fichiers()
Set fs = Application.FileSearch
With fs
.LookIn = "C:Windows"
.Filename = "*.jpg" 'extension de ton choix
If .Execute(SortBy:=msoSortByFileName,
SortOrder:=msoSortOrderAscending) > 0 Then
MsgBox .FoundFiles.Count & _
" fichier(s) trouvé(s)."
For I = 1 To .FoundFiles.Count
'MsgBox .FoundFiles(i)
* XYZ = Split(.FoundFiles(I), Application.PathSeparator)
Cells(1 + x, 10) = XYZ(UBound(XYZ))
* x = x + 1
Next I
Else
MsgBox "Aucun fichier n'a été trouvé."
End If
End With
End Sub
Tu pourrais aussi séparer le nom de l'extension par
XYZ = Split(.FoundFiles(I), Application.PathSeparator)
*WXY = Split(XYZ(UBound(XYZ)), ".")*
* Cells(1 + x, 10) = WXY(0)
*à condition de n'avoir qu'un point dans le nom de fichier.

Corto

fetnat a écrit :
Bonjour,

La macro suivante permet d'obtenir le chemin et le nom de fichier en
précisant le type d'extension.

1 - Comment faire pour obtenir seulement le nom des fichiers +
l'extension ?

2 - Et si c'est possible, comment faire pour obtenir seulement le nom ?

Ce deuxième point est moins crucial car j'ai une boucle pour supprimer
l'extension. Ce n'est pas élégant mais ça fonctionne.

Merci pour votre aide

Cordialement

Fetnat


Sub AllFilesByType()
' list all files in specified dir (fullpath with extension)
Dim fs, i As Integer, x As Integer
Set fs = Application.FileSearch
With Sheets("Contents")
.Cells.Clear
End With
With fs
.LookIn = "C:Windows"
.Filename = "*.txt"
.Execute
For i = 1 To .FoundFiles.Count
x = x + 1
Range("A" & x) = .FoundFiles(i)
Next i
If .FoundFiles.Count = 0 Then
MsgBox "No files founded."
End If
End With
End Sub




Avatar
Daniel.C
Après mûre réflexion, fetnat a écrit :
Bonjour,

La macro suivante permet d'obtenir le chemin et le nom de fichier en
précisant le type d'extension.

1 - Comment faire pour obtenir seulement le nom des fichiers + l'extension ?

2 - Et si c'est possible, comment faire pour obtenir seulement le nom ?

Ce deuxième point est moins crucial car j'ai une boucle pour supprimer
l'extension. Ce n'est pas élégant mais ça fonctionne.

Merci pour votre aide

Cordialement

Fetnat


Sub AllFilesByType()
' list all files in specified dir (fullpath with extension)
Dim fs, i As Integer, x As Integer
Set fs = Application.FileSearch
With Sheets("Contents")
.Cells.Clear
End With
With fs
.LookIn = "C:Windows"
.Filename = "*.txt"
.Execute
For i = 1 To .FoundFiles.Count
x = x + 1
Range("A" & x) = .FoundFiles(i)
Next i
If .FoundFiles.Count = 0 Then
MsgBox "No files founded."
End If
End With
End Sub



Bonsoir.

Remplace :
Range("A" & x) = .FoundFiles(i)
par
Range("A" & x) = Dir(.FoundFiles(i))
ou
Range("A" & x) = Left(Dir(.FoundFiles(i)), Len(Dir(.FoundFiles(i))) -
4)
Cordialement.
Daniel
Avatar
fetnat
Merci Daniel .C

C'est la version finale la plus complète qui me convient parfaitement.

Bonne soirée

Fetnat


Bonsoir.

Remplace :
Range("A" & x) = .FoundFiles(i)
par
Range("A" & x) = Dir(.FoundFiles(i))
ou
Range("A" & x) = Left(Dir(.FoundFiles(i)), Len(Dir(.FoundFiles(i))) - 4)
Cordialement.
Daniel