Removing File Extensions (Unsafe)

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Award-winning PowerShellPlus


Admin Guide to PowerShell Remoting


Mastering PowerShell eBook

Some users use Trim() to remove file extensions like this:

'c:\test\file.txt'.Trim('.txt')
c:\test\file

This seems to work great, but in reality, Trim() removes all the characters you submitted from the beginning and end of the text. Check this out:

'c:\test\report.txt'.Trim('.txt')
c:\test\repor

The file name is missing the last "t" because "t" is part of the characters Trim() removes.

 

Twitter This Tip! ReTweet this Tip!


Posted Jul 14 2011, 06:00 AM by ps1
Copyright 2012 PowerShell.com. All rights reserved.