Что нового

[Данные, строки] Как получить аналоги CMD переменных (%~d1, %~dp1, %~dpn1 т.д.)

gora

Знающий
Сообщения
315
Репутация
19
Есть переменная, например, $var1 в которой находится полный путь\имя файла с расширением. Как из неё получить аналоги CMD переменных %~d1, %~dp1, %~dpn1, %~n1, %~nx1, %~x1 ?
Ограничения:
- путь\имя файла могут содержать пробелы и любые допустимые символы
- имя файла может не иметь расширения
- число символов в расширении неизвестно (произвольное)
- имя файла может содержать точки, например: setup.exe.7z
- путь может содержать точки

Спасибо.
 

CreatoR

Must AutoIt!
Команда форума
Администратор
Сообщения
8,671
Репутация
2,481
Код:
_PathSplit
 

CreatoR

Must AutoIt!
Команда форума
Администратор
Сообщения
8,671
Репутация
2,481
Хотя я лично использую её аналог (моя версия):

Код:
;Деление пути на части:
;имя диска, путь без имени и расширения файла, путь бз расширения, путь без имени диска,
;имя файла и расширение, имя файла, только расширение файла

#include <Array.au3> ;Только для примера

$sPath = @ScriptFullPath
$aPathArr = _PathSplitByRegExp($sPath)

If IsArray($aPathArr) Then
	_ArrayDisplay($aPathArr, "Demo of _PathSplitRegExp()")
ElseIf $aPathArr = $sPath Then
	MsgBox(64, "Demo of _PathSplitRegExp()", $aPathArr)
Else
	MsgBox(48, "Error", "The path is not correct")
EndIf

;===============================================================================
; Function Name:    _PathSplitByRegExp()
; Description:      Split the path to 9 elements.
; Parameter(s):     $sPath - Path to split.
; Requirement(s):   
; Return Value(s):  On seccess - Array $aRetArray that contain 9 elements:
;					$aRetArray[0] = Full path ($sPath)
;					$aRetArray[1] = Drive letter
;					$aRetArray[2] = Path without FileName and extension
;					$aRetArray[3] = Full path without File Extension
;					$aRetArray[4] = Full path without drive letter
;					$aRetArray[5] = Full path without drive letter and without FileName and extension
;					$aRetArray[6] = FileName and extension
;					$aRetArray[7] = Just Filename
;					$aRetArray[8] = Just Extension of a file
;
;					On failure - If $sPath is not a valid path (the path is not splitable), then original $sPath is returned.
;					If $sPath does not include supported delimiters or it's emty, @error set to 1 and -1 is returned.
;
; Note(s):			The path can include backslash as well (exmp: C:/test/test.zip).
;
; Author(s):        G.Sandler a.k.a CreatoR (MrCreatoR) - Thanks to amel27 for help with RegExp
;===============================================================================
Func _PathSplitByRegExp($sPath)
	If $sPath = "" Or (StringInStr($sPath, "\") And StringInStr($sPath, "/")) Then
		Return SetError(1, 0, -1)
    EndIf
	
	Local $aRetArray[9], $pDelim = ""
	
	If StringRegExp($sPath, '^(?i)([A-Z]:|\\)(\\[^\\]+)+$') Then
		$pDelim = "\"
	EndIf
	
	If StringRegExp($sPath, '(?i)(^.*:/)(/[^/]+)+$') Then
		$pDelim = "//"
	EndIf
	
	If $pDelim = "" Then
		$pDelim = "/"
	EndIf
	
	If Not StringInStr($sPath, $pDelim) Then
		Return $sPath
	EndIf
	
	If $pDelim = "\" Then
		$pDelim &= "\"
	EndIf
	
	$aRetArray[0] = $sPath ;Full path
	$aRetArray[1] = StringRegExpReplace($sPath,  $pDelim & '.*', $pDelim) ;Drive letter
	$aRetArray[2] = StringRegExpReplace($sPath, $pDelim & '[^' & $pDelim & ']*$', '') ;Path without FileName and extension
	$aRetArray[3] = StringRegExpReplace($sPath, '\.[^.]*$', '') ;Full path without File Extension
	$aRetArray[4] = StringRegExpReplace($sPath, '(?i)([A-Z]:' & $pDelim & ')', '') ;Full path without drive letter
	$aRetArray[5] = StringRegExpReplace($aRetArray[4], $pDelim & '[^' & $pDelim & ']*$', '') ;Full path without drive letter and without FileName and extension
	$aRetArray[6] = StringRegExpReplace($sPath, '^.*' & $pDelim, '') ;FileName and extension
	$aRetArray[7] = StringRegExpReplace($sPath, '.*' & $pDelim & '|\.[^.]*$', '') ;Just Filename
	$aRetArray[8] = StringRegExpReplace($aRetArray[6], '^.*\.|^.*$', '') ;Just Extension of a file
	
	Return $aRetArray
EndFunc
 
Верх