Created
October 5, 2018 22:15
-
-
Save Mandorlo/f5ddbf8eeaee17e0ccb3305272c8a8e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' auto-porteur :) | |
Function fileExists(ByVal fichier As String) As Boolean | |
fileExists = (Dir$(fichier, vbNormal) <> "") And (fichier <> "") | |
End Function | |
' affiche la boîte de dialogue pour choisir un dossier | |
Function GetFolder(Optional strpath As String = "") As String | |
Dim fldr As FileDialog | |
Dim sItem As String | |
Set fldr = Application.FileDialog(msoFileDialogFolderPicker) | |
With fldr | |
.Title = "Select a Folder" | |
.AllowMultiSelect = False | |
If strpath <> "" Then .InitialFileName = strpath | |
If .Show <> -1 Then GoTo NextCode | |
sItem = .SelectedItems(1) | |
End With | |
NextCode: | |
GetFolder = sItem | |
Set fldr = Nothing | |
End Function | |
' affiche la boîte de dialogue pour choisir un fichier | |
Function GetFile(Optional strpath As String = "") As String | |
Dim fldr As FileDialog | |
Dim sItem As String | |
Set fldr = Application.FileDialog(msoFileDialogFilePicker) | |
With fldr | |
.Title = "Select a File" | |
.AllowMultiSelect = False | |
If strpath <> "" Then .InitialFileName = strpath | |
If .Show <> -1 Then GoTo NextCode | |
sItem = .SelectedItems(1) | |
End With | |
NextCode: | |
GetFile = sItem | |
Set fldr = Nothing | |
End Function | |
' renvoie le nom du fichier à partir de son chemin, en gardant l'extension | |
Function basename(ByVal inVal As String, Optional directorySeparater As String = "\") As String | |
Dim Index As Integer | |
Index = InStrRev(inVal, directorySeparater) | |
If Index > 0 Then | |
basename = Mid(inVal, Index + 1) | |
Else | |
basename = Mid(inVal, Index + 1) | |
End If | |
End Function | |
' renvoie le dossier parent | |
Function parentFolder(ByVal p As String) As String | |
If Len(p) > 1 Then | |
If Right(p, 1) = "\" Then | |
p = Left(p, Len(p) - 1) | |
End If | |
parentFolder = getLastSlash(p, "") | |
End If | |
End Function | |
' fonction auxiliaire pour parentFolder qui renvoie le dossier parent d'un chemin | |
' TODO : trop nul car on peut utiliser intrrev !!! | |
Function getLastSlash(ByVal restant As String, ByVal res As String) | |
n = InStr(restant, "\") | |
If n > 0 Then | |
getLastSlash = getLastSlash(Right(restant, Len(restant) - n), res & Left(restant, n)) | |
Else | |
getLastSlash = res | |
End If | |
End Function | |
'You can use this to delete all the files in the folder mondossier | |
Sub cleanFolder(ByVal mondossier As String) | |
On Error Resume Next | |
Kill mondossier & "\*.*" | |
On Error GoTo 0 | |
End Sub | |
' renvoie l'url du fichier trouvé dans le dossier avec les motcles (non recursif) | |
' opt = first|last|mostrecent | |
Function chercheFichier(ByVal dossier As String, ByVal motscle As Variant, Optional ByVal opt As String = "first") As String | |
Set fso = CreateObject("Scripting.FileSystemObject") | |
If Dir(dossier, vbDirectory) = "" Then Exit Function ' on quitte si le dossier n'existe pas | |
Set objFiles = fso.GetFolder(dossier).Files | |
Set res = New Collection | |
Dim maxdate, ladate As Date | |
For Each f In objFiles | |
' si ça existe on crée le lien | |
If InStr(f.Name, "~") = 0 Then | |
ok = True | |
For j = 0 To UBound(motscle) | |
'If InStr(LCase(f.Name), LCase(motscle(j))) = 0 Then ok = False | |
tmpre = RegexMatch(LCase(motscle(j)), LCase(f.Name)) | |
If tmpre = "" Then ok = False | |
Next j | |
If ok Then | |
chercheFichier = f.Path | |
If opt = "first" Then | |
Exit Function | |
ElseIf opt = "mostrecent" Then | |
res.Add f.Path | |
End If | |
End If | |
End If | |
Next f | |
If opt = "mostrecent" Then | |
If res.Count > 0 Then | |
chercheFichier = res(1) | |
maxdate = getLastModifDate(res(1)) | |
For i = 2 To res.Count | |
ladate = getLastModifDate(res(i)) | |
If maxdate < ladate Then | |
chercheFichier = res(i) | |
maxdate = ladate | |
End If | |
Next i | |
End If | |
End If | |
End Function | |
' renvoie la date de dernière modification d'un fichier | |
Function getLastModifDate(ByVal f As String) As Date | |
'Instanciation du FSO | |
Set oFSO = CreateObject("Scripting.FileSystemObject") | |
'Instanciation de l'objet File | |
If oFSO.fileExists(f) Then | |
Set oFl = oFSO.GetFile(f) | |
getLastModifDate = oFl.DateLastModified | |
End If | |
End Function | |
' dit si le dossier existe ou non | |
Function isDir(ByVal chemin As String) As Boolean | |
isDir = chemin <> "" And (Dir(chemin, vbDirectory) <> "") | |
End Function | |
' renvoie tous les fichiers récursivement situés dans chemin | |
' en option on peut spécifier une regex pour filtrer uniquement certains fichiers | |
Function getFiles(ByVal chemin As String, Optional ByVal re As String = "") As Collection | |
Dim FileSystem As Object | |
Dim c As Collection | |
Set c = New Collection | |
Set FileSystem = CreateObject("Scripting.FileSystemObject") | |
DoFolder FileSystem.GetFolder(chemin), re, c | |
Set getFiles = c | |
End Function | |
' fonction auxiliaire de getFiles | |
Sub DoFolder(Folder, ByVal re As String, ByRef res As Collection) | |
Dim SubFolder | |
For Each SubFolder In Folder.SubFolders | |
DoFolder SubFolder, re, res | |
Next | |
Dim File | |
For Each File In Folder.Files | |
If re = "" Or RegexMatch(re, File.Name) <> "" Then | |
res.Add File.Path | |
End If | |
Next | |
End Sub | |
' écrit s dans filePath et renvoie True si tout s'est bien passé | |
Function str2file(ByVal s As String, ByVal filePath As String) As Boolean | |
Open filePath For Output As #1 | |
Print #1, s | |
Close #1 | |
str2file = fileExists(filePath) | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment