Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Removing characters from a string

Reply
 
Old 12-31-2006   #1 (permalink)
Samuel Denbigh Leslie


 
 

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 SpecsSystem Spec
Old 12-31-2006   #2 (permalink)
RichS


 
 

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 SpecsSystem Spec
Old 12-31-2006   #3 (permalink)
Gaurhoth


 
 

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 SpecsSystem Spec
Old 12-31-2006   #4 (permalink)
/\\/\\o\\/\\/ [MVP]


 
 

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 SpecsSystem Spec
Old 12-31-2006   #5 (permalink)
Roman Kuzmin


 
 

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 SpecsSystem Spec
Old 12-31-2006   #6 (permalink)
$hay


 
 

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 SpecsSystem Spec
Old 12-31-2006   #7 (permalink)
RichS


 
 

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 SpecsSystem Spec
Old 01-01-2007   #8 (permalink)
Rob Campbell


 
 

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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46