1

I need to extricate the file extension from the file paths. For Eg: I have a file path like:

\\Test\data\data.dll

How to remove the data.dll from the file path, so that I get only

\\Test\data\

as the output.

I tried using the wild character - *. but that didn't work.

Thanks!

3 Answers 3

4

In your case there is a special cmdlet that by default removes the file or folder at the end. Try:

$path =  \\test\data\data.dll
#Get folderpath    
Split-Path $path
#Get filename only (thanks to Christian for tip)
Split-Path $path -Leaf
Sign up to request clarification or add additional context in comments.

5 Comments

Graimer: What if I want to do it vise versa.. i.e, How do I get only the data.dll and not the path before that??
Test the updated answer. If it doesn't work, tell me and I'll fix it in 30min when I get a computer :)
@ashishg For just the file name: Split-Path \\test\data\data.dll -Leaf
there, thanks for tip Christian. Cleaned up answer to include it now :)
Ya it works.. Thanks Graimer:) Graimer just refering to previous question could you tell me how wud I read the DLLs and PDBs if I had multiple sorces. For Eg: .\Projects\, .\Data\ etc
0

Here's 2 different ways of doing it. One is to use Split-Path. The other is to use -replace with regular expressions. They both work great in PowerShell V3, but Split-Path doesn't work in V2 if the path has a drive letter in it, but your system doesn't have that drive letter.

#Get Folder
Split-Path '\\test\data\data.dll'
'\\test\data\data.dll' -replace '\\[^\\]+$'

#Get File 
Split-Path '\\test\data\data.dll' -Leaf
'\\test\data\data.dll' -replace '^\\.*\\'

2 Comments

Make sure you use a drive letter you don't have on your system, ie J:. You can then do the following to see the problem. Split-Path 'J:\data\data.dll' You'll get the error message: "Cannot find drive. A drive with the name 'J' does not exist."I reported this to them a long time ago on connect.microsoft.com, but it wasn't fixed until V3. link
thanks for the info. You should add this in your answer. Not many people need to split-path for a non existing drive ;)
0

Just to add my 2 cents for a workaround for the issue told by @StephenMills in his answer:

PS C:\ps> ( [system.io.fileinfo]"J:\data\data.dll" ).FullName
J:\data\data.dll
PS C:\ps> ( [system.io.fileinfo]"J:\data\data.dll" ).BaseName
data
PS C:\ps> ( [system.io.fileinfo]"J:\data\data.dll" ).name
data.dll
PS C:\ps> ( [system.io.fileinfo]"J:\data\data.dll" ).DirectoryName
J:\data

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.