从文件名中删除最后5个字符 [英] Remove last 5 characters from filename

查看:95
本文介绍了从文件名中删除最后5个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




您好,我正在寻找一种方法来重命名整个文件目录

order。目录中的文件具有不同的长度,但是所有

以_xxxx结尾x'代表从我保存的程序中随机生成的数字

。所以我需要找到一种方法,我可以快速删除文件名

中的下划线和最后4个数字,而无需更改任何其他内容。我不能设置文件的最大长度

因为文件名的总长度不同。如果程序可以

从目录中的每个文件中删除最后5个字符,那么

会很棒!


谢谢你你的时间和考虑。

***通过Devdex发送 http:// www .devdex.com ***

不要只是参加USENET ......获得奖励!



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!

推荐答案

你看过System.IO.DirectoryInfo和System.IO.FileInfo

类了吗?

" Brian Gruber" < SI ******** @ hotmail.com>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
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...


我正在寻找一种方法来简短地重命名整个文件目录。目录中的文件有不同的长度,但是所有文件都以_xxxx结尾,x'表示从我保存的程序中随机生成的数字。所以我需要找到一种方法,我可以快速删除文件名中的下划线和最后4个数字
而无需更改其他任何内容。我不能设置文件的最大长度
因为文件名的总长度不同。如果程序可以从目录中的每个文件中删除最后5个字符,那将是非常棒的!

感谢您的时间和考虑。

***通过Devdex发送 http://www.devdex.com *** <不要只参加USENET ......获得奖励!


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:
* Brian Gruber <si********@hotmail.com> scripsit:
我正在寻找一种简单重命名整个文件目录的方法。目录中的文件有不同的长度,但是所有文件都以_xxxx结尾,x'表示从我保存的程序中随机生成的数字。所以我需要找到一种方法,我可以快速删除文件名中的下划线和最后4个数字
而无需更改其他任何内容。我不能设置文件的最大长度
因为文件名的总长度不同。如果程序可以从目录中的每个文件中删除最后5个字符,那将很棒!
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!




未经测试:


\\\

进口System.IO

..

..

..

Dim astr()As String = Directory.GetFiles(" C:\ Foo")

For each as As String in astr

Dim FileName As String = Path.GetFileNameWithoutExtension(s)

FileName = Strings.Left(FileName,FileName.Length - 5)

FileName& ; ="。 &安培; Path.GetExtension(s)

重命名(s,Path.Combine(Path.GetDirectoryName(s),FileName))

下一个s

///


-

Herfried K. Wagner [MVP]

< URL:http:// dotnet。 mvps.org/>



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,

我会使用System.IO.Path获取文件名本身(减少任何路径或

扩展名。)


然后我会使用String.SubString& String.LastIndexOf删除_xxxx。


最后我会使用System.IO.Path将文件名重新组合在一起......


类似于:


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 )

希望这有帮助

Jay


" Brian Gruber" < SI ******** @ hotmail.com>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
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...


我正在寻找一种方法来简短地重命名整个文件目录。目录中的文件有不同的长度,但是所有文件都以_xxxx结尾,x'表示从我保存的程序中随机生成的数字。所以我需要找到一种方法,我可以快速删除文件名中的下划线和最后4个数字
而无需更改其他任何内容。我不能设置文件的最大长度
因为文件名的总长度不同。如果程序可以从目录中的每个文件中删除最后5个字符,那将是非常棒的!

感谢您的时间和考虑。

***通过Devdex发送 http://www.devdex.com *** <不要只参加USENET ......获得奖励!


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!



这篇关于从文件名中删除最后5个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆