[UDF] oFile (File System)

Partagez des fonctions et des UDF AutoIt.
Règles du forum
.
Répondre
Avatar du membre
Numeric
Niveau 5
Niveau 5
Messages : 125
Enregistré le : mer. 23 mars 2016 08:17
Status : Hors ligne

[UDF] oFile (File System)

#1

Message par Numeric »

Salut, voici quelques fonctions relatives au traitement et à la gestion de vos fichiers; en d'autres termes...
#include-once
; #INDEX# =======================================================================================================================
; Title .........: oFile.au3
; AutoIt Version : 3.3.8.1 +++
; Description ...: Traite les fichiers en mode orienté objet
; Author(s) .....: Numeric
; Related .......: Scripting.FileSystemObject methodes
; Link ..........: MSDN
; ===============================================================================================================================
; #CONSTANTS# ===================================================================================================================
Global Const $___sFILE_STREAMclassName = "Scripting.FileSystemObject"
;================================================================================================================================

; #UDF Ressources#==============================================================================================================
;Verifie le fichier dll utilisé par l'UDF
If Not FileExists(@SystemDir & "\scrrun.dll") Then __WinAPI_ShowError("impossible d'utiliser cet UDF : scrrun.dll manque!")
;===============================================================================================================================

; #CURRENT# =====================================================================================================================
;oFS_Create
;oFS_DriveExists
;oFS_FileExists
;oFS_FolderExists
;oFS_GetAbsolutePathName
;oFS_GetBaseName
;oFS_GetDriveName
;oFS_GetExtensionName
;oFS_GetFileIcon
;oFS_GetFileName
;oFS_GetFileType
;oFS_GetParentFolderName
;oFS_MoveFile
;oFS_MoveFolder
;oFS_TempName
; ===============================================================================================================================

; #INTERNAL_USE_ONLY# ===========================================================================================================
;_IsDir
;_IsFile
;__oFS_MakeFolderValide
;__WinAPI_ShowError
; ===============================================================================================================================



; #FUNCTION# ====================================================================================================================
; Name ..........: oFS_Create
; Description ...: Cree une instance de l'objet FileSystem
; Syntax ........: oFS_Create()
; Parameters ....:
; Return values .: Succes : instance de l'objet file system
;                : Echec : @error <> 0
; Author ........: Numeric
; ===============================================================================================================================
Func oFS_Create()
   Return ObjCreate($___sFILE_STREAMclassName)
EndFunc   ;==>oFS_Create


; #FUNCTION# ====================================================================================================================
; Name ..........: oFS_DriveExists
; Description ...: Verifie si un lecteur existe....
; Syntax ........: oFS_DriveExists($oObj[, $sDrive = "C"])
; Parameters ....: $oObj                - an object.
;                  $sDrive              - [optional](Drive letter) a string value. Default is "C".
; Return values .: Succes :True si le lecteur en question existe et False sinon
; Author ........: Numeric
; ===============================================================================================================================
Func oFS_DriveExists($oObj, $sDrive = "C")
   If IsObj($oObj) Then Return $oObj.DriveExists($sDrive)
EndFunc   ;==>oFS_DriveExists


; #FUNCTION# ====================================================================================================================
; Name ..........: oFS_FileExists
; Description ...: Verifie si un fichier existe
; Syntax ........: oFS_FileExists($oObj, $sFileName)
; Parameters ....: $oObj                - an object.
;                  $sFileName  (le nom du Fichier à verifier)         - a string value.
; Return values .: True si le fichier existe et False s'il n'existe pas.
; Author ........: Numeric
; ===============================================================================================================================
Func oFS_FileExists($oObj, $sFileName)
   If IsObj($oObj) Then Return $oObj.FileExists($sFileName)
EndFunc   ;==>oFS_FileExists


; #FUNCTION# ====================================================================================================================
; Name ..........: oFS_FolderExists
; Description ...: Verifie si un dossier existe
; Syntax ........: oFS_FolderExists($oObj, $sFolder)
; Parameters ....: $oObj                - an object.
;                  $sFolder (le nom du dossier à verifier l'existence)  - a string value.
; Return values .: True si le dossier existe et false s'il n'existe pas..
; Author ........: Numeric
; ===============================================================================================================================
Func oFS_FolderExists($oObj, $sFolder)
   If IsObj($oObj) Then Return $oObj.FolderExists($sFolder)
EndFunc   ;==>oFS_FolderExists

; #FUNCTION# ====================================================================================================================
; Name ..........: oFS_GetAbsolutePathName
; Description ...: Retourne le chemin complet, du disque jusqu'a l'extension d'un fichier .
; Syntax ........: oFS_GetAbsolutePathName($oObj, $sPath)
; Parameters ....: $oObj                - an object.
;                  $sPath (le chemin sur lequel appliqué la fonction   - a string value.
; Remarque       : La fonction ne verifie pas l'existence d'un fichier.D'ailleurs si le chemin n'existe pas, le chemin retourné par defaut
;                ; est @ScriptDir.
;                ; Aussi meme si le fichier n'existe pas,son chemin est retourné.
;                ; Si sPath est un dir valide, son chemin complet est retourné.
; Return values .: Le chemin complet du fichier ou du dossier.
; Author ........: Numeric
; ===============================================================================================================================
Func oFS_GetAbsolutePathName($oObj, $sPath)
   If IsObj($oObj) Then Return $oObj.GetAbsolutePathName($sPath)
EndFunc   ;==>oFS_GetAbsolutePathName

; #FUNCTION# ====================================================================================================================
; Name ..........: oFS_GetBaseName
; Description ...: Retourne le nom du dossier ou du fichier sans l'extension selon le fait que ($sPath) soit un dossier ou un fichier.
; Syntax ........: oFS_GetBaseName($oObj, $sPath)
; Parameters ....: $oObj                - an object.
;                  $sPath               - a string value.
; Return values .: le nom du fichier ou du dossier
; Author ........: Numeric
; ===============================================================================================================================
Func oFS_GetBaseName($oObj, $sPath)
   If IsObj($oObj) Then Return $oObj.GetBaseName($sPath)
EndFunc   ;==>oFS_GetBaseName

; #FUNCTION# ====================================================================================================================
; Name ..........: oFS_GetDriveName
; Description ...: Obtient le nom du lecteur...
; Syntax ........: oFS_GetDriveName($oObj, $sPath)
; Parameters ....: $oObj                - an object.
;                  $sPath   (Drive path)            - a string value.
; Return values .: Drive Name s'il existe ou une chaine vide sinon
; Author ........: Numeric
; ===============================================================================================================================
Func oFS_GetDriveName($oObj, $sPath)
   If IsObj($oObj) Then Return $oObj.GetDriveName($sPath)
EndFunc   ;==>oFS_GetDriveName


; #FUNCTION# ====================================================================================================================
; Name ..........: oFS_GetExtensionName
; Description ...: Retourne le nom de l'extension(sans le point)
; Syntax ........: oFS_GetExtensionName($oObj, $sPath)
; Parameters ....: $oObj                - an object.
;                  $sPath               - a string value.
; Return values .: Extension sans le "." en cas d'echec la fonction retourne une chaine vide
; Author ........: Numeric
; ===============================================================================================================================
Func oFS_GetExtensionName($oObj, $sPath)
   If IsObj($oObj) Then Return $oObj.GetExtensionName($sPath)
EndFunc   ;==>oFS_GetExtensionName


; #FUNCTION# ====================================================================================================================
; Name ..........: oFS_GetFileIcon
; Description ...: Obtient le fichier icone d'un fichier ainsi que son index au vu du system
; Syntax ........: oFS_GetFileIcon($oObj, $sFilePath)
; Parameters ....: $oObj                - an object.
;                  $sFilePath           - a string value.
; Return values .: un tableau d'index 0 comportant le fichier icon et d'index 1 comportant l'index de l'icone dans le fichier icone..
; Author ........: Numeric
; Modified ......:
; Remarks .......: certains fichiers ont d'autres emplacements registre, ceux-ci sont representés par cette fonction
;                : par l'icone 23 du fichier shell32.dll
;                : Aussi cette fonction ne gere pas les icones des executables.Cette  petite option  necessitera d'inclure
;                : D'autres Fichiers UDF pour extraire l'icone et l'inserer dans la liste d'image afin de lui attribuer un element...
; Related .......:
; Link ..........:
; Example .......: Oui...
; ===============================================================================================================================
Func oFS_GetFileIcon($oObj, $sFilePath)
   If Not oFS_FileExists($oObj, $sFilePath) Then Return SetError(1, 0, -1) ;le fichier n'existe pas...

   Local $aIconInfos[2]
   Local $sIcon, $iSplit, $iNewIndex, $sFileIcon
   $sIcon = RegRead("HKEY_CLASSES_ROOT\" & RegRead("HKEY_CLASSES_ROOT\." & _
         oFS_GetExtensionName($oObj, $sFilePath), "") & "\DefaultIcon", "")

   If $sIcon Then
      $sIcon = StringReplace($sIcon, '"', "")
      $iSplit = StringSplit($sIcon, ",", 2)
      $iNewIndex = StringReplace($sIcon, $iSplit[$iSplit[0]] & ",", "")
      $sFileIcon = $iSplit[$iSplit[0]]
   Else
      $sFileIcon = @SystemDir & "\shell32.dll"
      $iNewIndex = 23
   EndIf

   $aIconInfos[0] = $sFileIcon
   $aIconInfos[1] = $iNewIndex
   Return $aIconInfos
EndFunc   ;==>oFS_GetFileIcon


; #FUNCTION# ====================================================================================================================
; Name ..........: oFS_GetFileName
; Description ...: Retourne le nom du fichier(avec extension) dans un repertoire
; Syntax ........: oFS_GetFileName($oObj, $sPath)
; Parameters ....: $oObj                - an object.
;                  $sPath               - a string value.
; Return values .: Le nom du fichier
; Author ........:Numeric
; ===============================================================================================================================
Func oFS_GetFileName($oObj, $sPath)
   If IsObj($oObj) Then Return $oObj.GetFileName($sPath)
EndFunc   ;==>oFS_GetFileName


; #FUNCTION# ====================================================================================================================
; Name ..........: oFS_GetFileType
; Description ...: Retourne le type d'un fichier...
; Syntax ........: oFS_GetFileType($oObj, $sFilePath)
; Parameters ....: $oObj                - an object.
;                  $sFilePath           - a string value.
; Return values .: Type du fichier.
; Author ........: Numeric....
; ===============================================================================================================================
Func oFS_GetFileType($oObj, $sFilePath)
   If oFS_FileExists($oObj, $sFilePath) Then Return RegRead("HKEY_CLASSES_ROOT\" & "." & _
         oFS_GetExtensionName($oObj, $sFilePath), "PerceivedType")
EndFunc   ;==>oFS_GetFileType


; #FUNCTION# ====================================================================================================================
; Name ..........: oFS_GetParentFolderName
; Description ...: Retourne le nom complet du Dossier dans lequel se trouve un fichier
; Syntax ........: oFS_GetParentFolderName($oObj, $sPath)
; Parameters ....: $oObj                - an object.
;                  $sPath               - a string value.
; Return values .: Le chemin du dossier sans \ avant le nom du fichier
; Author ........: Numeric
; Modified ......:
; Remarks .......:
; ===============================================================================================================================
Func oFS_GetParentFolderName($oObj, $sPath)
   If IsObj($oObj) Then Return $oObj.GetParentFolderName($sPath)
EndFunc   ;==>oFS_GetParentFolderName


; #FUNCTION# ====================================================================================================================
; Name ..........: oFS_MoveFile
; Description ...: Deplace un fichier
; Syntax ........: oFS_MoveFile($oObj, $sSource, $sDestFolder)
; Parameters ....: $oObj                - an object.
;                  $sSource             - a string value.
;                  $sDestFolder         - a string value.
; Return values .: None
; Author ........: Numeric
; ===============================================================================================================================
Func oFS_MoveFile($oObj, $sSource, $sDestFolder)
   $sDestFolder = __oFS_MakeFolderValide($sDestFolder)
   If oFS_FolderExists($oObj, $sDestFolder) And oFS_FileExists($oObj, $sSource) Then _
         $oObj.MoveFile($sSource, $sDestFolder)
EndFunc   ;==>oFS_MoveFile


; #FUNCTION# ====================================================================================================================
; Name ..........: oFS_MoveFolder
; Description ...: Deplace un dossier vers un autre
; Syntax ........: oFS_MoveFolder($oObj, $sSrceFolder, $sDestFolder)
; Parameters ....: $oObj                - an object.
;                  $sSrceFolder (dossier à deplacer)        - a string value.
;                  $sDestFolder  (dossier de destination)       - a string value.
; Return values .: None
; Author ........: Numeric
; ===============================================================================================================================
Func oFS_MoveFolder($oObj, $sSrceFolder, $sDestFolder)
   $sSrceFolder = StringRight($sSrceFolder, 1) = "\" ? StringTrimRight($sSrceFolder, 1) : $sSrceFolder
   $sDestFolder = __oFS_MakeFolderValide($sDestFolder)
   If oFS_FolderExists($oObj, $sSrceFolder) And oFS_FolderExists($oObj, $sDestFolder) Then
      $oObj.MoveFolder($sSrceFolder, $sDestFolder)
   EndIf
EndFunc   ;==>oFS_MoveFolder

; #FUNCTION# ====================================================================================================================
; Name ..........: oFS_TempName
; Description ...: Genere un nom qui peut etre utilisé comme un fichier temporaire..
; Syntax ........: oFS_TempName($oObj)
;================================================================================================================================
Func oFS_TempName($oObj)
   If IsObj($oObj) Then Return $oObj.GetTempName()
EndFunc   ;==>oFS_TempName



#Region Fonctions internes
;#INTERNAL_USE_ONLY# ====================================================================================================================
; Name ..........: __oFS_MakeFolderValide
; Description ...: Ajuste les chemins des dossiers pour certaines fonctions de transfert
; Syntax ........: oFS_MakeFolderValide($sPath)
; Parameters ....: $sPath               - a string value.
; Return values .:
; Author ........: Numeric
; ===============================================================================================================================
Func __oFS_MakeFolderValide($sPath)
   Return $sPath = (StringRight($sPath, 1) <> "\") ? ($sPath & "\") : ($sPath)
EndFunc   ;==>__oFS_MakeFolderValide

; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __WinAPI_ShowError
; Descriction ...: pour eviter d'inclure le fichier WinAPI.au3
; ===============================================================================================================================
Func __WinAPI_ShowError($sText, $bExit = True)
   MsgBox(0, "Error", $sText)
   If $bExit Then Exit
EndFunc   ;==>__WinAPI_ShowError

; #INTERNAL_USE_ONLY# ====================================================================================================================
; Author ........:Numeric
; Modified.......:
;Description.....: Verifie si un Chemin est un fichier...
; ===============================================================================================================================
Func _IsFile($sFilePath)
   Return StringInStr(FileGetAttrib($sFilePath), "D") = 0
EndFunc   ;==>_IsFile
#EndRegion Fonctions internes
Fichiers joints
oFile.au3
(16.97 Kio) Téléchargé 649 fois
De 0 et 1 vers les étoiles , tout part du Binaire, Numeric
Répondre