将文件重命名为上/下的问题 [英] Problems Renaming File To Upper / Lower

查看:106
本文介绍了将文件重命名为上/下的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将文件重命名为全大写/小写时遇到问题。我需要做的就是更改文件名的大小写,而不是文件名本身,但我不断得到文件已存在的异常。这就是我到目前为止所尝试的:



#1

Dim OldName,NewName As String

OldName =C:\ Users \ ME\MyFile.txt

NewName =C:\ Users \ ME\MYFILE.txt

重命名(OldName,NewName)



#2

Dim fi作为新FileInfo(C:\ Users\ME \ MyFile.txt)

My.Computer.FileSystem.RenameFile(C:\ Users \ ME\MyFile.txt,C:\ Users \ ME\MYFILE。 txt)

fi.MoveTo(FILENEW)



这些都没有用,也没有找到我发现的文章关于重命名文件讨论这个问题。

解决方案

这是因为不幸的是,Windows存在以下不一致:文件区分大小写的名称被保留,但是...仅在案例中有所不同的文件的两个名称被视为指向同一文件。我不知道是谁发明了这个跛脚(我明白这是来自可怕的MS-DOS遗产,但对我来说,它看起来不是一个充分的借口),但这是一个真正的跛脚,一种病态。



现在,算法可能是这样的:创建一些目前不存在的中间文件名,并重命名两次:首先是中间文件名,然后是你想要的名称。它会工作的。你看到了这个主意吗?实际上,它将像MyFile.txt - > MYFILE-2.txt - > MYFILE.TXT。或者,甚至更好,忽略问题,不要重命名。 Lame很蹩脚,不是你的错,也不是我的错。



祝你好运,

-SA

I am having problems renaming a file to all upper case / lower case. All I need to do is change the case of the filename, not the filename itself, but I keep getting an exception that the file already exists. This is what I''ve tried so far:

#1
Dim OldName, NewName As String
OldName = "C:\Users\ME\MyFile.txt"
NewName = "C:\Users\ME\MYFILE.txt"
Rename(OldName, NewName)

#2
Dim fi As New FileInfo("C:\Users\ME\MyFile.txt")
My.Computer.FileSystem.RenameFile("C:\Users\ME\MyFile.txt", "C:\Users\ME\MYFILE.txt")
fi.MoveTo(FILENEW)

Neither of these have worked, and none of the articles that I''ve found about renaming files discusses this.

解决方案

This is because this is, unfortunately, Windows, with the following inconsistency: the case-sensitive name of the file is preserved, but… two names of the file which differ only in the case are considered as pointing to the same file. I have no idea who invented this lame (I understand that came from dreaded legacy of MS-DOS, but to me, it does no look like a sufficient excuse), but this is a real lame, a pathology.

Now, the algorithm could be like this: create some intermediate file name which does not presently exist, and rename twice: first to the intermediate file name, then to the name you want. It will work then. Do you see the idea? Effectively, it will be like MyFile.txt -> MYFILE-2.txt -> MYFILE.txt. Or, maybe even better, ignore the problem and don''t rename. Lame is lame, not your fault or my fault.

Good luck,
—SA


这篇关于将文件重命名为上/下的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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