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! 10 13011
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!
* 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/>
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!
* "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/>
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/>
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!
* "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/>
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/>
* "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/>
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/> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: alcesteatxmissiondot |
last post by:
I have a table with a couple of TEXT columns. In one of them, there are
characters that aren't supported by the application and don't seem to be
able to display in the Query Analyzer either. I...
|
by: Li Pang |
last post by:
Hi,
I want to know the easiest way to remove the last line in a text file, or
how to catch the last line of a text file. Thanks in advance
|
by: Paul |
last post by:
Hi,
My VB is very rusty I'm afraid! What would be the most efficient way to
remove the following ASCII characters from a string?
à è ì ò ù À È Ì Ò Ù
á é í ó ú ý Á É Í Ó Ú Ý
â ê î ô û Â Ê Î Ô...
|
by: lekshminair |
last post by:
dears can u help me
remove last letter in a string.
eg: arises in this string remove only last 's'
ouput:arise
|
by: 5star |
last post by:
I need to remove special characters from a cell .The cell value is $1,234.50.
I want 1234.50 as result .
Pls help me.
regards,
5star
|
by: shapper |
last post by:
Hello,
I am trying to remove the last letter from a string:
names.Remove(names.Remove(names.Length - 1, 1))
I get index out of range. What am I doing wrong?
Thanks,
Miguel
|
by: SuvarnaChaudhari |
last post by:
Hi,I am new to .net.
As per ur guidance I tried myString = myString.SubString(0,myString.Length-1);
I also used str.lastindexof n str.remove,but by using this I m gettin string as ' (B193 '' H13 ''...
|
by: Brad |
last post by:
I'm writing a function to remove certain characters from strings. For
example, I often get strings with commas... they look like this:
"12,384"
I'd like to take that string, remove the comma...
|
by: Julien |
last post by:
Hi,
I can't seem to find the right regular expression to achieve what I
want. I'd like to remove all characters from a string that are not
numbers, letters or underscores.
For example:
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |