Advertisement
BugFix

AutoItObject - Path

Dec 7th, 2014
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 6.37 KB | None | 0 0
  1.  
  2. #cs
  3.     AutoIt-Object als Funktionsmodul
  4.  
  5.     Bsp.: Pfad-Objekt
  6.     Kann absolute, relative und UNC-Pfade verarbeiten
  7. #ce
  8.  
  9.  
  10.  
  11. #include "AutoitObject.au3"
  12. #include <WinAPIFiles.au3>
  13.  
  14. _AutoItObject_StartUp()
  15.  
  16.  
  17. ;------------------------------------- Variante allgemein
  18.  
  19. $oPath = __Class_Path()
  20. $sPath = 'C:\Bla\Blub\text.txt'
  21.  
  22. With $oPath
  23.     ConsoleWrite('Drive   ' & .Drive($sPath) & @CRLF)
  24.     ConsoleWrite('Dir     ' & .Dir($sPath) & @CRLF)
  25.     ConsoleWrite('DirName ' & .DirName($sPath) & @CRLF)
  26.     ConsoleWrite('File    ' & .File($sPath) & @CRLF)
  27.     ConsoleWrite('Base    ' & .Base($sPath) & @CRLF)
  28.     ConsoleWrite('Ext     ' & .Ext($sPath) & @CRLF)
  29.     ConsoleWrite('Exists  ' & .Exists($sPath) & @CRLF)
  30. EndWith
  31. ConsoleWrite(@CRLF)
  32.  
  33. ;-------------------------------------------------------
  34.  
  35.  
  36.  
  37. ;-------------------------------------- Variante gebunden
  38.  
  39. $sPath = 'C:\Bla\Blub\text.txt'
  40. $oPath = __Class_Path1($sPath)
  41.  
  42. _Print($oPath)
  43. ConsoleWrite(@CRLF)
  44.  
  45. ; Pfad neu setzen
  46. ;-- durch Verwendung einer Methode wird ein relativer Pfad sofort in einen absoluten Pfad gewandelt
  47. ;-- mit einer Property wäre das nicht möglich
  48. $oPath.NewPath('..\start.ini')
  49. _Print($oPath)
  50. ConsoleWrite(@CRLF)
  51.  
  52. ; auch UNC-Pfade
  53. $oPath.NewPath('\\Server\Ordner_A\Fotos_Urlaub_2014.zip')
  54. _Print($oPath)
  55. ConsoleWrite(@CRLF)
  56.  
  57. ; oder ein neues Objekt erstellen und sofort die gewünschte Methode aufrufen
  58. ConsoleWrite('File (@ScriptFullPath): ' & __Class_Path1(@ScriptFullPath).File & @CRLF)
  59.  
  60. Func _Print($o)
  61.     With $o
  62.         ConsoleWrite('Drive   ' & .Drive & @CRLF)
  63.         ConsoleWrite('Dir     ' & .Dir & @CRLF)
  64.         ConsoleWrite('DirName ' & .DirName & @CRLF)
  65.         ConsoleWrite('File    ' & .File & @CRLF)
  66.         ConsoleWrite('Base    ' & .Base & @CRLF)
  67.         ConsoleWrite('Ext     ' & .Ext & @CRLF)
  68.         ConsoleWrite('Exists  ' & .Exists & @CRLF)
  69.     EndWith
  70. EndFunc
  71.  
  72. ;-------------------------------------------------------
  73.  
  74.  
  75.  
  76.  
  77. ;------------------------------------------------------ allgemeines Klassenobjekt
  78. ;------------------------ Pfad wird immer als Parameter an die Methoden übergeben
  79. Func __Class_Path()
  80.     Local $oPath = _AutoItObject_Class()
  81.     $oPath.Create()
  82.     With $oPath
  83.         .AddMethod("Drive",   "__Path_Drive")
  84.         .AddMethod("Dir",     "__Path_Directory")
  85.         .AddMethod("DirName", "__Path_DirName")
  86.         .AddMethod("File",    "__Path_Filename")
  87.         .AddMethod("Base",    "__Path_Basename")
  88.         .AddMethod("Ext",     "__Path_Extension")
  89.         .AddMethod("Exists",  "__Path_Exists")
  90.         Return .Object
  91.     EndWith
  92. EndFunc
  93. ;--------------------------------------------------------------------------------
  94.  
  95. Func __Path_Drive($oSelf, $sPath)
  96.     $sPath = _WinAPI_GetFullPathName($sPath)
  97.     If StringLeft($sPath, 2) = '\\' Then
  98.         Return StringLeft($sPath, StringInStr($sPath, '\', 0, 3) -1)
  99.     Else
  100.         Return StringLeft($sPath, 2)
  101.     EndIf
  102. EndFunc
  103.  
  104. Func __Path_Directory($oSelf, $sPath)
  105.     $sPath = _WinAPI_GetFullPathName($sPath)
  106.     Local $sDir = StringLeft($sPath, StringInStr($sPath, '\', 0, -1)-1)
  107.     Return StringRegExpReplace($sDir, ':$', ':\\')
  108. EndFunc
  109.  
  110. Func __Path_DirName($oSelf, $sPath)
  111.     $sPath = _WinAPI_GetFullPathName($sPath)
  112.     Local $sDir = StringLeft($sPath, StringInStr($sPath, '\', 0, -1)-1)
  113.     If StringLen($sDir) < 4 Then
  114.         Return ''
  115.     Else
  116.         Return StringTrimLeft($sDir, StringInStr($sDir, '\', 0, -1))
  117.     EndIf
  118. EndFunc
  119.  
  120. Func __Path_Filename($oSelf, $sPath)
  121.     $sPath = _WinAPI_GetFullPathName($sPath)
  122.     Return StringTrimLeft($sPath, StringInStr($sPath, '\', 0, -1))
  123. EndFunc
  124.  
  125. Func __Path_Basename($oSelf, $sPath)
  126.     $sPath = _WinAPI_GetFullPathName($sPath)
  127.     Local $sFileExt = StringTrimLeft($sPath, StringInStr($sPath, '\', 0, -1))
  128.     Return StringLeft($sFileExt, StringInStr($sFileExt, '.', 0, -1)-1)
  129. EndFunc
  130.  
  131. Func __Path_Extension($oSelf, $sPath)
  132.     $sPath = _WinAPI_GetFullPathName($sPath)
  133.     Return StringTrimLeft($sPath, StringInStr($sPath, '.', 0, -1))
  134. EndFunc
  135.  
  136. Func __Path_Exists($oSelf, $sPath)
  137.     $sPath = _WinAPI_GetFullPathName($sPath)
  138.     ; "Not Not" für Umwandlung Integer-To-Bool
  139.     ; (nicht zwingend erforderlich, aber Bool'sche Rückgabe ist eindeutiger als '0/1')
  140.     Return Not Not FileExists($sPath)
  141. EndFunc
  142. ;--------------------------------------------------------------------------------
  143.  
  144.  
  145.  
  146. ;------------------------------------------------------- gebundenes Klassenobjekt
  147. ;---- Pfad wird bei der Erstellung des Klassenobjektes übergeben
  148. ;---- alle Methoden des Objektes verwenden nun diesen Pfad
  149. ;---- um einen anderen Pfad zu verwenden, muss die Methode 'NewPath()' aufgerufen werden
  150. Func __Class_Path1($sPath)
  151.     Local $oPath = _AutoItObject_Class()
  152.     $oPath.Create()
  153.     $sPath = _WinAPI_GetFullPathName($sPath)
  154.     With $oPath
  155.         .AddProperty("Path",  $ELSCOPE_PRIVATE, $sPath)
  156.         .AddMethod("NewPath", "__Path_NewPath")
  157.         .AddMethod("Drive",   "__Path_Drive1")
  158.         .AddMethod("Dir",     "__Path_Directory1")
  159.         .AddMethod("DirName", "__Path_DirName1")
  160.         .AddMethod("File",    "__Path_Filename1")
  161.         .AddMethod("Base",    "__Path_Basename1")
  162.         .AddMethod("Ext",     "__Path_Extension1")
  163.         .AddMethod("Exists",  "__Path_Exists1")
  164.         Return .Object
  165.     EndWith
  166. EndFunc
  167. ;--------------------------------------------------------------------------------
  168.  
  169. Func __Path_NewPath($oSelf, $sNew)
  170.     $oSelf.Path = _WinAPI_GetFullPathName($sNew)
  171. EndFunc
  172.  
  173. Func __Path_Drive1($oSelf)
  174.     If StringLeft($oSelf.Path, 2) = '\\' Then
  175.         Return StringLeft($oSelf.Path, StringInStr($oSelf.Path, '\', 0, 3) -1)
  176.     Else
  177.         Return StringLeft($oSelf.Path, 2)
  178.     EndIf
  179. EndFunc
  180.  
  181. Func __Path_Directory1($oSelf)
  182.     Local $sDir = StringLeft($oSelf.Path, StringInStr($oSelf.Path, '\', 0, -1)-1)
  183.     Return StringRegExpReplace($sDir, ':$', ':\\')
  184. EndFunc
  185.  
  186. Func __Path_DirName1($oSelf)
  187.     Local $sDir = StringLeft($oSelf.Path, StringInStr($oSelf.Path, '\', 0, -1)-1)
  188.     If StringLen($sDir) < 4 Then
  189.         Return ''
  190.     Else
  191.         Return StringTrimLeft($sDir, StringInStr($sDir, '\', 0, -1))
  192.     EndIf
  193. EndFunc
  194.  
  195. Func __Path_Filename1($oSelf)
  196.     Return StringTrimLeft($oSelf.Path, StringInStr($oSelf.Path, '\', 0, -1))
  197. EndFunc
  198.  
  199. Func __Path_Basename1($oSelf)
  200.     Local $sFileExt = StringTrimLeft($oSelf.Path, StringInStr($oSelf.Path, '\', 0, -1))
  201.     Return StringLeft($sFileExt, StringInStr($sFileExt, '.', 0, -1)-1)
  202. EndFunc
  203.  
  204. Func __Path_Extension1($oSelf)
  205.     Return StringTrimLeft($oSelf.Path, StringInStr($oSelf.Path, '.', 0, -1))
  206. EndFunc
  207.  
  208. Func __Path_Exists1($oSelf)
  209.     Return Not Not FileExists($oSelf.Path)
  210. EndFunc
  211. ;--------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement