![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | 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) |
| Guest | 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) |
| Guest | 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) |
| Guest | 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) |
| Guest | 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) |
| Guest | 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) |
| Guest | 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) |
| Guest | 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 | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to Randomize characters in a string | RICK | VB Script | 17 | 08-22-2008 01:46 PM |
| Replacing Multiple Characters In A String | JMinnick | PowerShell | 4 | 07-31-2008 03:13 PM |
| Capture a string of characters and use as filename. | MCSE_Sec | PowerShell | 4 | 06-06-2008 06:43 PM |
| How export-csv deals with string versus string[] | Marco Shaw | PowerShell | 2 | 07-13-2007 12:18 PM |
| removing selected file that contain special characters | Paul | PowerShell | 5 | 12-03-2006 04:59 PM |