Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #cs
- AutoIt-Object als Funktionsmodul
- Bsp.: Pfad-Objekt
- Kann absolute, relative und UNC-Pfade verarbeiten
- #ce
- #include "AutoitObject.au3"
- #include <WinAPIFiles.au3>
- _AutoItObject_StartUp()
- ;------------------------------------- Variante allgemein
- $oPath = __Class_Path()
- $sPath = 'C:\Bla\Blub\text.txt'
- With $oPath
- ConsoleWrite('Drive ' & .Drive($sPath) & @CRLF)
- ConsoleWrite('Dir ' & .Dir($sPath) & @CRLF)
- ConsoleWrite('DirName ' & .DirName($sPath) & @CRLF)
- ConsoleWrite('File ' & .File($sPath) & @CRLF)
- ConsoleWrite('Base ' & .Base($sPath) & @CRLF)
- ConsoleWrite('Ext ' & .Ext($sPath) & @CRLF)
- ConsoleWrite('Exists ' & .Exists($sPath) & @CRLF)
- EndWith
- ConsoleWrite(@CRLF)
- ;-------------------------------------------------------
- ;-------------------------------------- Variante gebunden
- $sPath = 'C:\Bla\Blub\text.txt'
- $oPath = __Class_Path1($sPath)
- _Print($oPath)
- ConsoleWrite(@CRLF)
- ; Pfad neu setzen
- ;-- durch Verwendung einer Methode wird ein relativer Pfad sofort in einen absoluten Pfad gewandelt
- ;-- mit einer Property wäre das nicht möglich
- $oPath.NewPath('..\start.ini')
- _Print($oPath)
- ConsoleWrite(@CRLF)
- ; auch UNC-Pfade
- $oPath.NewPath('\\Server\Ordner_A\Fotos_Urlaub_2014.zip')
- _Print($oPath)
- ConsoleWrite(@CRLF)
- ; oder ein neues Objekt erstellen und sofort die gewünschte Methode aufrufen
- ConsoleWrite('File (@ScriptFullPath): ' & __Class_Path1(@ScriptFullPath).File & @CRLF)
- Func _Print($o)
- With $o
- ConsoleWrite('Drive ' & .Drive & @CRLF)
- ConsoleWrite('Dir ' & .Dir & @CRLF)
- ConsoleWrite('DirName ' & .DirName & @CRLF)
- ConsoleWrite('File ' & .File & @CRLF)
- ConsoleWrite('Base ' & .Base & @CRLF)
- ConsoleWrite('Ext ' & .Ext & @CRLF)
- ConsoleWrite('Exists ' & .Exists & @CRLF)
- EndWith
- EndFunc
- ;-------------------------------------------------------
- ;------------------------------------------------------ allgemeines Klassenobjekt
- ;------------------------ Pfad wird immer als Parameter an die Methoden übergeben
- Func __Class_Path()
- Local $oPath = _AutoItObject_Class()
- $oPath.Create()
- With $oPath
- .AddMethod("Drive", "__Path_Drive")
- .AddMethod("Dir", "__Path_Directory")
- .AddMethod("DirName", "__Path_DirName")
- .AddMethod("File", "__Path_Filename")
- .AddMethod("Base", "__Path_Basename")
- .AddMethod("Ext", "__Path_Extension")
- .AddMethod("Exists", "__Path_Exists")
- Return .Object
- EndWith
- EndFunc
- ;--------------------------------------------------------------------------------
- Func __Path_Drive($oSelf, $sPath)
- $sPath = _WinAPI_GetFullPathName($sPath)
- If StringLeft($sPath, 2) = '\\' Then
- Return StringLeft($sPath, StringInStr($sPath, '\', 0, 3) -1)
- Else
- Return StringLeft($sPath, 2)
- EndIf
- EndFunc
- Func __Path_Directory($oSelf, $sPath)
- $sPath = _WinAPI_GetFullPathName($sPath)
- Local $sDir = StringLeft($sPath, StringInStr($sPath, '\', 0, -1)-1)
- Return StringRegExpReplace($sDir, ':$', ':\\')
- EndFunc
- Func __Path_DirName($oSelf, $sPath)
- $sPath = _WinAPI_GetFullPathName($sPath)
- Local $sDir = StringLeft($sPath, StringInStr($sPath, '\', 0, -1)-1)
- If StringLen($sDir) < 4 Then
- Return ''
- Else
- Return StringTrimLeft($sDir, StringInStr($sDir, '\', 0, -1))
- EndIf
- EndFunc
- Func __Path_Filename($oSelf, $sPath)
- $sPath = _WinAPI_GetFullPathName($sPath)
- Return StringTrimLeft($sPath, StringInStr($sPath, '\', 0, -1))
- EndFunc
- Func __Path_Basename($oSelf, $sPath)
- $sPath = _WinAPI_GetFullPathName($sPath)
- Local $sFileExt = StringTrimLeft($sPath, StringInStr($sPath, '\', 0, -1))
- Return StringLeft($sFileExt, StringInStr($sFileExt, '.', 0, -1)-1)
- EndFunc
- Func __Path_Extension($oSelf, $sPath)
- $sPath = _WinAPI_GetFullPathName($sPath)
- Return StringTrimLeft($sPath, StringInStr($sPath, '.', 0, -1))
- EndFunc
- Func __Path_Exists($oSelf, $sPath)
- $sPath = _WinAPI_GetFullPathName($sPath)
- ; "Not Not" für Umwandlung Integer-To-Bool
- ; (nicht zwingend erforderlich, aber Bool'sche Rückgabe ist eindeutiger als '0/1')
- Return Not Not FileExists($sPath)
- EndFunc
- ;--------------------------------------------------------------------------------
- ;------------------------------------------------------- gebundenes Klassenobjekt
- ;---- Pfad wird bei der Erstellung des Klassenobjektes übergeben
- ;---- alle Methoden des Objektes verwenden nun diesen Pfad
- ;---- um einen anderen Pfad zu verwenden, muss die Methode 'NewPath()' aufgerufen werden
- Func __Class_Path1($sPath)
- Local $oPath = _AutoItObject_Class()
- $oPath.Create()
- $sPath = _WinAPI_GetFullPathName($sPath)
- With $oPath
- .AddProperty("Path", $ELSCOPE_PRIVATE, $sPath)
- .AddMethod("NewPath", "__Path_NewPath")
- .AddMethod("Drive", "__Path_Drive1")
- .AddMethod("Dir", "__Path_Directory1")
- .AddMethod("DirName", "__Path_DirName1")
- .AddMethod("File", "__Path_Filename1")
- .AddMethod("Base", "__Path_Basename1")
- .AddMethod("Ext", "__Path_Extension1")
- .AddMethod("Exists", "__Path_Exists1")
- Return .Object
- EndWith
- EndFunc
- ;--------------------------------------------------------------------------------
- Func __Path_NewPath($oSelf, $sNew)
- $oSelf.Path = _WinAPI_GetFullPathName($sNew)
- EndFunc
- Func __Path_Drive1($oSelf)
- If StringLeft($oSelf.Path, 2) = '\\' Then
- Return StringLeft($oSelf.Path, StringInStr($oSelf.Path, '\', 0, 3) -1)
- Else
- Return StringLeft($oSelf.Path, 2)
- EndIf
- EndFunc
- Func __Path_Directory1($oSelf)
- Local $sDir = StringLeft($oSelf.Path, StringInStr($oSelf.Path, '\', 0, -1)-1)
- Return StringRegExpReplace($sDir, ':$', ':\\')
- EndFunc
- Func __Path_DirName1($oSelf)
- Local $sDir = StringLeft($oSelf.Path, StringInStr($oSelf.Path, '\', 0, -1)-1)
- If StringLen($sDir) < 4 Then
- Return ''
- Else
- Return StringTrimLeft($sDir, StringInStr($sDir, '\', 0, -1))
- EndIf
- EndFunc
- Func __Path_Filename1($oSelf)
- Return StringTrimLeft($oSelf.Path, StringInStr($oSelf.Path, '\', 0, -1))
- EndFunc
- Func __Path_Basename1($oSelf)
- Local $sFileExt = StringTrimLeft($oSelf.Path, StringInStr($oSelf.Path, '\', 0, -1))
- Return StringLeft($sFileExt, StringInStr($sFileExt, '.', 0, -1)-1)
- EndFunc
- Func __Path_Extension1($oSelf)
- Return StringTrimLeft($oSelf.Path, StringInStr($oSelf.Path, '.', 0, -1))
- EndFunc
- Func __Path_Exists1($oSelf)
- Return Not Not FileExists($oSelf.Path)
- EndFunc
- ;--------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement