By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,909 Members | 2,008 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,909 IT Pros & Developers. It's quick & easy.

Remove last 5 characters from filename

P: n/a


Hi, I'm looking for a way to rename a whole directory of files in short
order. The files in the directory have different lengths, however all
of them end with _xxxx the x's represent a randomly generated number
from a program that I saved from. So I need to find a way that I can
quickly remove the underscore and the last 4 numbers from the filename
without changing anything else. I can't set a max length to the file
becuase the filename's overall length varies. If the program could
remove these last 5 characters from every file in the directory that
would be awesome!

Thanks for your time and consideration.
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Have you looked at the System.IO.DirectoryInfo and System.IO.FileInfo
classes?
"Brian Gruber" <si********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...


Hi, I'm looking for a way to rename a whole directory of files in short
order. The files in the directory have different lengths, however all
of them end with _xxxx the x's represent a randomly generated number
from a program that I saved from. So I need to find a way that I can
quickly remove the underscore and the last 4 numbers from the filename
without changing anything else. I can't set a max length to the file
becuase the filename's overall length varies. If the program could
remove these last 5 characters from every file in the directory that
would be awesome!

Thanks for your time and consideration.
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #2

P: n/a
* Brian Gruber <si********@hotmail.com> scripsit:
Hi, I'm looking for a way to rename a whole directory of files in short
order. The files in the directory have different lengths, however all
of them end with _xxxx the x's represent a randomly generated number
from a program that I saved from. So I need to find a way that I can
quickly remove the underscore and the last 4 numbers from the filename
without changing anything else. I can't set a max length to the file
becuase the filename's overall length varies. If the program could
remove these last 5 characters from every file in the directory that
would be awesome!


Untested:

\\\
Imports System.IO
..
..
..
Dim astr() As String = Directory.GetFiles("C:\Foo")
For Each s As String In astr
Dim FileName As String = Path.GetFileNameWithoutExtension(s)
FileName = Strings.Left(FileName, FileName.Length - 5)
FileName &= "." & Path.GetExtension(s)
Rename(s, Path.Combine(Path.GetDirectoryName(s), FileName))
Next s
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3

P: n/a
Brian,
I would use System.IO.Path to get at the filename itself (less any path or
extension).

Then I would use String.SubString & String.LastIndexOf to remove the _xxxx.

Finally I would use System.IO.Path to put the filename back together...

Something like:

Dim fullPath As String = "C:\My Documents\MyFile_1234.text"
Dim fileName As String = Path.GetFileNameWithoutExtension(fullPath)
Dim extension As String = Path.GetExtension(fullPath)
fileName = fileName.Substring(0, fileName.LastIndexOf("_"c))
fileName = Path.ChangeExtension(fileName, extension)
fullPath = Path.GetDirectoryName(fullPath)
fullPath = Path.Combine(fullPath, fileName)
Hope this helps
Jay

"Brian Gruber" <si********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...


Hi, I'm looking for a way to rename a whole directory of files in short
order. The files in the directory have different lengths, however all
of them end with _xxxx the x's represent a randomly generated number
from a program that I saved from. So I need to find a way that I can
quickly remove the underscore and the last 4 numbers from the filename
without changing anything else. I can't set a max length to the file
becuase the filename's overall length varies. If the program could
remove these last 5 characters from every file in the directory that
would be awesome!

Thanks for your time and consideration.
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #4

P: n/a
* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
fileName = Path.ChangeExtension(fileName, extension)


I knew that I missed something... :-).

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5

P: n/a
Herfried,
Your:
FileName &= "." & Path.GetExtension(s)
Would have the same effect, I started out with the above, however I didn't
want to bother checking if GetExtension removed the leading "." or if
FileName had a trailing "."...

Which raises an interesting question: What does either of our code do with
names like:

MyFile._1234.Text

Which is a valid name, however MyFile..Text I don't think is a valid file
name...

Just a thought
Jay

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2k*************@uni-berlin.de... * "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
fileName = Path.ChangeExtension(fileName, extension)


I knew that I missed something... :-).

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #6

P: n/a


Yes, I got it to work great! Thanks for the help.

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #7

P: n/a
* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
Herfried,
Your:
FileName &= "." & Path.GetExtension(s)
Would have the same effect, I started out with the above, however I didn't
want to bother checking if GetExtension removed the leading "." or if
FileName had a trailing "."...


Yep. That's a "bug" in my version, because 'GetExtension' will return
the extension with the leading ".". Filenames like "A.B.C" will return
the extension ".C" and the name without extension "A.B". Filenames like
".A" will return ".A" as extension and "" as filename.
Which raises an interesting question: What does either of our code do with
names like:

MyFile._1234.Text
..NET would return ".Text" as extension and "MyFile._1234" as filename
without extension.
Which is a valid name, however MyFile..Text I don't think is a valid file
name...


It's a valid filename. The filename without extension is "MyFile.", the
extension is ".Text". Explorer treats files like "A.B.C", "A..C" and
".C" as files of type ".C".

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #8

P: n/a
Herfried,
Which is a valid name, however MyFile..Text I don't think is a valid file name...
It's a valid filename. The filename without extension is "MyFile.", the
extension is ".Text". Explorer treats files like "A.B.C", "A..C" and
".C" as files of type ".C".


Doing a quick Test, ChangeExtension will remove the trailing "." from
"MyFile." before adding the new extension.

Which raises an interesting problem, my routine fails with "C:\My
Documents\MyFile.xyz_1234.text", it will return "C:\My
Documents\MyFile.text"

I'd like to revise my answer to:

Dim fullPath As String = "C:\My Documents\MyFile.xyz_1234.text"
Dim fileName As String = Path.GetFileNameWithoutExtension(fullPath)
Dim extension As String = Path.GetExtension(fullPath)
fileName = fileName.Substring(0, fileName.LastIndexOf("_"c))
' fileName = Path.ChangeExtension(fileName, extension)
fileName = fileName.TrimEnd("."c)
fileName &= extension
fullPath = Path.GetDirectoryName(fullPath)
fullPath = Path.Combine(fullPath, fileName)

I do like Path.ChangeExtension, for example when I am reading xml &
converting to html, its just not the best fit for the above.

Dim input As String = "myfile.xml"
Dim output As String = Path.ChangeExtension(input, ".html")

Dim xslt As XslTransform
xslt.Transform(input, output, Nothing)

Hope this helps
Jay

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2k*************@uni-berlin.de... * "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
Herfried,
Your:
FileName &= "." & Path.GetExtension(s)


Would have the same effect, I started out with the above, however I didn't want to bother checking if GetExtension removed the leading "." or if
FileName had a trailing "."...


Yep. That's a "bug" in my version, because 'GetExtension' will return
the extension with the leading ".". Filenames like "A.B.C" will return
the extension ".C" and the name without extension "A.B". Filenames like
".A" will return ".A" as extension and "" as filename.
Which raises an interesting question: What does either of our code do with names like:

MyFile._1234.Text


.NET would return ".Text" as extension and "MyFile._1234" as filename
without extension.
Which is a valid name, however MyFile..Text I don't think is a valid file name...


It's a valid filename. The filename without extension is "MyFile.", the
extension is ".Text". Explorer treats files like "A.B.C", "A..C" and
".C" as files of type ".C".

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #9

P: n/a
* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
It's a valid filename. The filename without extension is "MyFile.", the
extension is ".Text". Explorer treats files like "A.B.C", "A..C" and
".C" as files of type ".C".


Doing a quick Test, ChangeExtension will remove the trailing "." from
"MyFile." before adding the new extension.

Which raises an interesting problem, my routine fails with "C:\My
Documents\MyFile.xyz_1234.text", it will return "C:\My
Documents\MyFile.text"

I'd like to revise my answer to:

Dim fullPath As String = "C:\My Documents\MyFile.xyz_1234.text"
Dim fileName As String = Path.GetFileNameWithoutExtension(fullPath)
Dim extension As String = Path.GetExtension(fullPath)
fileName = fileName.Substring(0, fileName.LastIndexOf("_"c))
' fileName = Path.ChangeExtension(fileName, extension)
fileName = fileName.TrimEnd("."c)
fileName &= extension
fullPath = Path.GetDirectoryName(fullPath)
fullPath = Path.Combine(fullPath, fileName)

I do like Path.ChangeExtension, for example when I am reading xml &
converting to html, its just not the best fit for the above.


Are you sure? On my system, 'ChangeExtension("A.B.C", ".D")' returns
"A.B.D".

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #10

P: n/a
Herfried,
Are you sure? On my system, 'ChangeExtension("A.B.C", ".D")' returns
"A.B.D". Yes! You need to use my code specifically. My use of ChangeExtension has a
problem in the context of my code!

Try it to see what I mean, single step my code:

First with this line:
fileName = Path.ChangeExtension(fileName, extension)
Then with these two lines:
fileName = fileName.TrimEnd("."c)
fileName &= extension
Jay
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2k*************@uni-berlin.de... * "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit: It's a valid filename. The filename without extension is "MyFile.", the extension is ".Text". Explorer treats files like "A.B.C", "A..C" and
".C" as files of type ".C".


Doing a quick Test, ChangeExtension will remove the trailing "." from
"MyFile." before adding the new extension.

Which raises an interesting problem, my routine fails with "C:\My
Documents\MyFile.xyz_1234.text", it will return "C:\My
Documents\MyFile.text"

I'd like to revise my answer to:

Dim fullPath As String = "C:\My Documents\MyFile.xyz_1234.text"
Dim fileName As String =

Path.GetFileNameWithoutExtension(fullPath) Dim extension As String = Path.GetExtension(fullPath)
fileName = fileName.Substring(0, fileName.LastIndexOf("_"c))
' fileName = Path.ChangeExtension(fileName, extension)
fileName = fileName.TrimEnd("."c)
fileName &= extension
fullPath = Path.GetDirectoryName(fullPath)
fullPath = Path.Combine(fullPath, fileName)

I do like Path.ChangeExtension, for example when I am reading xml &
converting to html, its just not the best fit for the above.


Are you sure? On my system, 'ChangeExtension("A.B.C", ".D")' returns
"A.B.D".

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.