Remove last 5 characters from filename

  • Thread starter Thread starter Brian Gruber
  • Start date Start date
B

Brian Gruber

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!
 
* Brian Gruber said:
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
///
 
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
 
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
 
* "Jay B. Harlow said:
Herfried,
Your:


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,
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
 
* "Jay B. Harlow said:
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,
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:
Then with these two lines:
Jay
 
Back
Top