Option Explicit Function findFile(FSO, Folder, Ext) 'Returns Nothing if the file with the `Ext` extension wasn't found in the `Folder` directory. 'Ext must be lower-case 'Usage: FindFile(objFSO,"C:\WINDOWS","exe") Dim FSO_Folder, FSO_File Do On Error Resume Next Set FSO_Folder = FSO.GetFolder(Folder) If Err.Number = 0 Then Exit Do On Error Goto 0 Set findFile = Nothing : Exit Function Loop While 0 On Error Goto 0 For Each FSO_File In FSO_Folder.Files MsgBox FSO_File.Path If LCase(FSO.GetExtensionName(FSO_File.Path)) = Ext Then Set FindFile = FSO_File : Exit Function End If Next Set FSO_Folder = Nothing Set findFile = Nothing End Function Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject") Dim WSH : Set WSH = CreateObject("WScript.Shell") Dim NK2 : Set NK2 = FindFile(FSO, WSH.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\Outlook", "nk2") If NK2 Is Nothing Then MsgBox "Le fichier NK2 ou répertoire contenant ce dernier est introuvable.", vbExclamation, "Erreur" Else If NK2.DateCreated < "#20/07/2010#" Then MsgBox "Le fichier """ & NK2.Path & """ va être supprimé !", vbInformation, "Suppression du NK2" NK2.Delete Else MsgBox "Rien à supprimer.", vbInformation, "Info" End If Set NK2 = Nothing End If
Option Explicit 'évite de passer une demi-heure sur une connerie de variable non déclarée. Function findFile(FSO, Folder, Ext) 'Returns Nothing if the file with the `Ext` extension wasn't found in the `Folder` directory. 'Ext must be lower-case 'Usage: FindFile(objFSO,"C:\WINDOWS","exe") Dim FSO_Folder, FSO_File Do 'pas de On Error Goto en VBS, donc obligé de tricher un peu On Error Resume Next 'passe à la ligne suivante si erreur Set FSO_Folder = FSO.GetFolder(Folder) If Err.Number = 0 Then Exit Do 'si pas d'erreur, on quitte la boucle bidon On Error Goto 0 'mode normal pour les erreurs Set findFile = Nothing : Exit Function 'vu qu'il y a eu une erreur, on retourne Nothing Loop While 0 'la boucle s'exécutera une fois vu que la condition est fausse (0<>1) On Error Goto 0 'mode normal pour les erreurs (on est sorti de la boucle) For Each FSO_File In FSO_Folder.Files 'pour chaque fichier If LCase(FSO.GetExtensionName(FSO_File.Path)) = Ext Then 'si c'est la même extension Set FindFile = FSO_File : Exit Function 'on retourne l'objet fichier End If Next Set FSO_Folder = Nothing 'on détruit l'objet fichier Set findFile = Nothing 'on retourne Nothing (rien trouvé) End Function Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject") 'on crée une instance de FSO Dim WSH : Set WSH = CreateObject("WScript.Shell") 'on crée une instance de WSH Dim NK2 : Set NK2 = FindFile(FSO, WSH.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\Outlook", "nk2") 'on récupère un objet fichier correspondant au NK2 If NK2 Is Nothing Then '//si l'objet est nul MsgBox "Le fichier NK2 ou répertoire contenant ce dernier est introuvable.", vbExclamation, "Erreur" Else '//sinon If NK2.DateCreated < "#20/07/2010#" Then '//si le fichier porte la date qu'on veut MsgBox "Le fichier """ & NK2.Path & """ va être supprimé !", vbInformation, "Suppression du NK2" NK2.Delete 'on supprime le fichier Else '//sinon MsgBox "Rien à supprimer.", vbInformation, "Info" End If '//fin si Set NK2 = Nothing 'on détruit l'objet fichier End If '//fin si
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionOn Error Goto 0 'mode normal pour les erreurs : s'il y en a une, l'interpréteur affichera un message d'erreur et quittera.
Set findFile = Nothing : Exit Function 'vu qu'il y a eu une erreur, on retourne Nothing