2017-11-29 1 views
0

저는 powershell을 배우는 데있어서 새로운입니다. 내 Powershell 스크립트에 Azure 데이터 호수 저장소 경로 \data\sb\published\Juned\Testfile.txt에서 "Testfile.txt"를 추출하려고합니다. ("\data\sb\published\Juned\Testfile.txt" -split '\')[0]을 사용하고 있습니다. Powershell - 하위 문자열을 추출하는 방법

당신은 확장 파일과 파일 이름을 얻기 위해 -leaf 매개 변수와 함께 split-path cmdlet를 사용할 수있는 오류

parsing "\" - Illegal \ at end of pattern. 
At line:1 char:1 
+ ("\data\sb\published\Juned\Testfile.txt" -split '\')[0] 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : OperationStopped: (:) [], ArgumentException 
    + FullyQualifiedErrorId : System.ArgumentException 
+0

에게 : 오류의 정확한 원인은 -split이 정규 표현식을 사용하고 \가 정규 표현식 문자이기 때문입니다. 아래 답변은 정규식을 사용하지 않는 .split 메서드를 사용합니다. –

답변

0

을 보여주고있다 : 당신은 절대적으로 당신의 분할 방식을 고수 할 경우 split-path "\data\sb\published\Juned\Testfile.txt" -leaf

당신 마지막 요소 ([0] 대신에 [-1])를 선택해야합니다. "\data\sb\published\Juned\Testfile.txt".split('\')[-1]

+0

빠른 도움에 감사드립니다. 분할 경로 "\ data \ sb \ published \ Juned \ Testfile.txt"-leaf가 작동합니다. – juned

0

이 두 방법은 나를 위해 일한 :

split-path "H:\Documents\devops\tp-mkt-SPD-38.4.10.msi" -leaf 

TP-MKT-SPD-38.4.10.msi

참고
[System.IO.Path]::GetFileName("C:\abc\xyz\test.txt") 

있는 test.txt

[System.IO.Path]::GetFileNameWithoutExtension("C:\abc\xyz\test.txt") 

테스트

+0

빠른 도움에 감사드립니다. 분할 경로 "\ data \ sb \ published \ Juned \ Testfile.txt"-leaf가 작동합니다. – juned

관련 문제