![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Removing characters from a string Hi there, I have a variable, $test, that contains the full path to a file on my machine. What I wish to accomplish is remove everything from the string except the filename minus the extension. The former is easy to accomplish with the split-path cmdlet (split-path $test -leaf). However, I can't seem to find out how to remove the file extension. The file is a PowerShell script, and so has an extension of .ps1. Thanks in advance, -SDL |
My System Specs![]() |
| | #2 (permalink) |
| | RE: Removing characters from a string Theres a couple of ways that come to mind: This one takes the output from split-path and uses the Replace function of Strings to remove the extension. It would also be possible to use the substring or remove functions if you knew the length of the name of the file PS> $test = "C:\WINDOWS\system32\windowspowershell\v1.0\eula.txt" PS> $fname = (Split-Path $test -leaf).ToString().Replace(".txt", "") PS> $fname eula PS> Alternatively a more direct way is to use the .Net Path class' GetFileNameWithoutExtension method which will return the name of the file without the extension PS> $test = "C:\WINDOWS\system32\windowspowershell\v1.0\eula.txt" PS> $fname = [System.IO.Path]::GetFileNameWithoutExtension($test) PS> $fname eula PS> -- Richard Siddaway Please note that all scripts are supplied "as is" and with no warranty "Samuel Denbigh Leslie" wrote: > Hi there, > > I have a variable, $test, that contains the full path to a file on my > machine. What I wish to accomplish is remove everything from the string > except the filename minus the extension. The former is easy to accomplish > with the split-path cmdlet (split-path $test -leaf). However, I can't seem > to find out how to remove the file extension. The file is a PowerShell > script, and so has an extension of .ps1. > > Thanks in advance, > > -SDL > > > |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Removing characters from a string I'd use .NET's io.path ... like so: PS ps:\> $fp = "c:\windows\system32\bubbles.scr" PS ps:\> [io.path]::GetFileNameWithoutExtension($fp) bubbles You can get a list of the static methods provided by io.path: PS ps:\> [io.path] | gm -static -- gaurhoth http://gaurhothw.spaces.live.com/ "Samuel Denbigh Leslie" <samuel.denbigh.leslie@gmail.com> wrote in message news:eRPsUcOLHHA.2028@TK2MSFTNGP03.phx.gbl... > Hi there, > > I have a variable, $test, that contains the full path to a file on my > machine. What I wish to accomplish is remove everything from the string > except the filename minus the extension. The former is easy to accomplish > with the split-path cmdlet (split-path $test -leaf). However, I can't seem > to find out how to remove the file extension. The file is a PowerShell > script, and so has an extension of .ps1. > > Thanks in advance, > > -SDL > > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Removing characters from a string another way : PoSH>$t = gi TabExpansion.ps1 PoSH>$t.FullName.Replace( $t.Extension,'') C:\PowerShell\PowerTab\TabExpansion PoSH>$t.Name.Replace( $t.Extension,'') TabExpansion gr /\/\o\/\/ "Gaurhoth" <gaurhoth@live.com> wrote in message news:uqM13JQLHHA.3668@TK2MSFTNGP02.phx.gbl... I'd use .NET's io.path ... like so: PS ps:\> $fp = "c:\windows\system32\bubbles.scr" PS ps:\> [io.path]::GetFileNameWithoutExtension($fp) bubbles You can get a list of the static methods provided by io.path: PS ps:\> [io.path] | gm -static -- gaurhoth http://gaurhothw.spaces.live.com/ "Samuel Denbigh Leslie" <samuel.denbigh.leslie@gmail.com> wrote in message news:eRPsUcOLHHA.2028@TK2MSFTNGP03.phx.gbl... > Hi there, > > I have a variable, $test, that contains the full path to a file on my > machine. What I wish to accomplish is remove everything from the string > except the filename minus the extension. The former is easy to accomplish > with the split-path cmdlet (split-path $test -leaf). However, I can't seem > to find out how to remove the file extension. The file is a PowerShell > script, and so has an extension of .ps1. > > Thanks in advance, > > -SDL > > |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Removing characters from a string This way may not work: 1) paths and file names can contain the same string as extension several times: MyFile.txt.txt -> MyFile (.txt replaced with '') My.docs\File.doc -> Mys\File (.doc replaced with '') 2) .Replace() is case sensitive, is not it? Paths are not. -- Thanks, Roman Kuzmin |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Removing characters from a string you can use the old way: echo $file.substring($file.lastindexofany("\")+1,($file.indexof(".")-1)-$file.lastindexofany("\")) it works on a file path with single or multiple extensions $hay http://scriptolog.blogspot.com "Samuel Denbigh Leslie" <samuel.denbigh.leslie@gmail.com> wrote in message news:eRPsUcOLHHA.2028@TK2MSFTNGP03.phx.gbl... > Hi there, > > I have a variable, $test, that contains the full path to a file on my > machine. What I wish to accomplish is remove everything from the string > except the filename minus the extension. The former is easy to accomplish > with the split-path cmdlet (split-path $test -leaf). However, I can't seem > to find out how to remove the file extension. The file is a PowerShell > script, and so has an extension of .ps1. > > Thanks in advance, > > -SDL > |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Removing characters from a string Can always use tolower().replace() which removes case sensitivity which won't matter if need to refer back to the file as Windows isn't case sensitive on file names or paths -- Richard Siddaway Please note that all scripts are supplied "as is" and with no warranty "Roman Kuzmin" wrote: > This way may not work: > > 1) paths and file names can contain the same string as extension several times: > > MyFile.txt.txt -> MyFile (.txt replaced with '') > My.docs\File.doc -> Mys\File (.doc replaced with '') > > 2) .Replace() is case sensitive, is not it? Paths are not. > > -- > Thanks, > Roman Kuzmin > |
My System Specs![]() |
| | #8 (permalink) |
| | RE: Removing characters from a string Also, there's the option of using a capturing regex. If $test is the filename.extension $test -match "(^.+)\..+" $filename = $matches[1] If $test is the full path $test -match "^.+\\(.+)\..+" $filename = $matches[1] "Samuel Denbigh Leslie" wrote: > Hi there, > > I have a variable, $test, that contains the full path to a file on my > machine. What I wish to accomplish is remove everything from the string > except the filename minus the extension. The former is easy to accomplish > with the split-path cmdlet (split-path $test -leaf). However, I can't seem > to find out how to remove the file extension. The file is a PowerShell > script, and so has an extension of .ps1. > > Thanks in advance, > > -SDL > > > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Remove characters in a string | VB Script | |||
| Select first 16 characters in a string | PowerShell | |||
| How to Randomize characters in a string | VB Script | |||
| Replacing Multiple Characters In A String | PowerShell | |||
| Capture a string of characters and use as filename. | PowerShell | |||