ViSiToR

Silver Member | Редактировать | Профиль | Сообщение | ICQ | Цитировать | Сообщить модератору euheny Цитата: как определяем имя файла и его расширение ? | Когда я не умел юзать RegExp, делал так: Код: $sPath = "C:\Path\Test.zip" ;Только путь (без имени файла и расширения) $sPathOnly = StringLeft($sPath, StringInStr($sPath, "\", 0, -1)-1) MsgBox(0, "", $sPathOnly) ;Только Имя файла $sFile = StringTrimLeft($sPath, StringInStr($sPath, "\", 0, -1)) $sFile = StringLeft($sFile, StringInStr($sFile, ".", 0, -1)-1) MsgBox(0, "", $sFile) ;Только расширение $sExt = StringTrimLeft($sPath, StringInStr($sPath, ".", 0, -1)) MsgBox(0, "", $sExt) ;Имя файла + расширение $sFileAndExt = StringTrimLeft($sPath, StringInStr($sPath, "\", 0, -1)) MsgBox(0, "", $sFileAndExt) | А когда научился немного RegExp, написал вот такую “бесмертную” функцию (amel27 мне в этом немного помог ), в протест местной _PathSplit (мне не нравилось в ней условие передачи строки ByRef )... Код: #include <Array.au3> ;Only for example $Path = "c:\test\my folder\file.zip" $PathArr = _PathSplitByRegExp($Path) If IsArray($PathArr) Then _ArrayDisplay($PathArr, "Demo of _PathSplitRegExp()") ElseIf $PathArr = $Path Then MsgBox(64, "Demo of _PathSplitRegExp()", $PathArr) Else MsgBox(48, "Error", "The path is not correct") EndIf ;=============================================================================== ; Function Name: _PathSplitByRegExp() ; Description: Split the path to 8 elements. ; Parameter(s): $sPath - Path to split. ; Requirement(s): AutoIt 3.2.2.0. ; Return Value(s): On seccess - Array $RetArray that contain 8 elements: ; $RetArray[0] = Full path ($sPath) ; $RetArray[1] = Drive letter ; $RetArray[2] = Path without FileName and extension ; $RetArray[3] = Full path without File Extension ; $RetArray[4] = Full path without drive letter ; $RetArray[5] = FileName and extension ; $RetArray[6] = Just Filename ; $RetArray[7] = Just Extension of a file ; ; On failure - If $sPath not include correct path (the path is not splitable), ; then $sPath returned. ; If $sPath not include needed delimiters, or it's emty, ; then @error set to 1, and returned -1. ; ; Note(s): The path can include backslash as well (exmp: C:/test/test.zip). ; ; Author(s): G.Sandler a.k.a CreatoR (MsCreatoR) - Thanks to amel27 for help with RegExp ;=============================================================================== Func _PathSplitByRegExp($sPath) If $sPath = "" Or (StringInStr($sPath, "\") And StringInStr($sPath, "/")) Then Return SetError(1, 0, -1) Local $RetArray[8], $pDelim = "" If StringRegExp($sPath, '^(?i)([A-Z]:|\\)(\\[^\\]+)+$') Then $pDelim = "\" If StringRegExp($sPath, '(?i)(^.*:/)(/[^/]+)+$') Then $pDelim = "//" If $pDelim = "" Then $pDelim = "/" If Not StringInStr($sPath, $pDelim) Then Return $sPath If $pDelim = "\" Then $pDelim &= "\" $RetArray[0] = $sPath $RetArray[1] = StringRegExpReplace($sPath, $pDelim & '.*', $pDelim) $RetArray[2] = StringRegExpReplace($sPath, $pDelim & '[^' & $pDelim & ']*$', '') $RetArray[3] = StringRegExpReplace($sPath, '\.[^.]*$', '') $RetArray[4] = StringRegExpReplace($sPath, '(?i)([A-Z]:' & $pDelim & ')', '') $RetArray[5] = StringRegExpReplace($sPath, '^.*' & $pDelim, '') $RetArray[6] = StringRegExpReplace($sPath, '.*' & $pDelim & '|\.[^.]*$', '') $RetArray[7] = StringRegExpReplace($sPath, '^.*\.', '') Return $RetArray EndFunc | Тут есть модификация (последний пост), чтобы работало и с UNC-путями. Цитата: можно ли добавить меру в массив | В смысле? в нулевой элемент добавить размерность массива?
---------- ViSiToR a.k.a CreatoR CreatoR это не ник, CreatoR это стиль жизни! |
| Всего записей: 3251 | Зарегистр. 01-04-2006 | Отправлено: 09:27 22-11-2007 | Исправлено: ViSiToR, 09:29 22-11-2007 |
|