文件重命名问题?
我正在使用 VB6,并且有一个文件夹,其中有 n
个文件。
我想将文件扩展名更改为 .txt
。我使用下面的代码将所有 .fin
文件的扩展名更改为 .txt
。
Dim filename1 As String
filename1 = Dir$(txtsourcedatabasefile & "\*.fin", vbDirectory)
Do While filename1 <> ""
Dim strInput As String
Dim strOutput As String
Dim strChar As String
Dim intChar As Integer
Dim intLoop As Integer
strInput = filename1
strOutput = ""
For intLoop = 1 To Len(strInput)
strChar = Mid$(strInput, intLoop, 1)
intChar = Asc(strChar)
If ((intChar >= 48) And (intChar <= 57)) Or _
((intChar >= 65) And (intChar <= 90)) Or _
((intChar >= 97) And (intChar <= 122)) Or _
(intChar = 95) Then
strOutput = strOutput & strChar
End If
Next
Name txtsourcedatabasefile & "\" & filename1 As txtsourcedatabasefile & "\" & strOutput & ".txt"
filename1 = Dir$
Loop
上面的代码用于将 .fin
更改为 .txt
,但文件名不带任何扩展名,例如 Clockings2.mis04062009 022511 PM
、Silver_421_export
等不会转换为 .txt
扩展名。例如,Clockings2mis04062009022511PM.txt
、Silver_421_export.txt
。
我应该如何更改此代码?
I'm using VB6 and I have a folder where I have n
number of files.
I want to change the file extension to .txt
. I used the code below to change the extension of all .fin
files to .txt
.
Dim filename1 As String
filename1 = Dir$(txtsourcedatabasefile & "\*.fin", vbDirectory)
Do While filename1 <> ""
Dim strInput As String
Dim strOutput As String
Dim strChar As String
Dim intChar As Integer
Dim intLoop As Integer
strInput = filename1
strOutput = ""
For intLoop = 1 To Len(strInput)
strChar = Mid$(strInput, intLoop, 1)
intChar = Asc(strChar)
If ((intChar >= 48) And (intChar <= 57)) Or _
((intChar >= 65) And (intChar <= 90)) Or _
((intChar >= 97) And (intChar <= 122)) Or _
(intChar = 95) Then
strOutput = strOutput & strChar
End If
Next
Name txtsourcedatabasefile & "\" & filename1 As txtsourcedatabasefile & "\" & strOutput & ".txt"
filename1 = Dir$
Loop
The code above is working for changing .fin
to .txt
, but with filenames without any extension, like Clockings2.mis04062009 022511 PM
, Silver_421_export
, etc., aren't transformed to a .txt
extention. For example, Clockings2mis04062009022511PM.txt
, Silver_421_export.txt
.
How should I change this code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我正确解释您的问题,您希望遍历目录中的文件并将所有扩展名为“.fin”的文件(或根本没有扩展名)更改为“.txt”。您可以添加对 Microsoft 脚本运行时的引用并使用 FileSystemObject 来相当轻松地完成此操作。在此示例中,我们假设 txtsourcedatabase 包含您要处理的目录:
If I am interpreting your question correctly, you want to iterate through the files in a directory and change all files with the extension ".fin", or no extension at all, to ".txt". You can add a reference to the Microsoft Scripting Runtime and use the FileSystemObject to do that rather easily. In this example, we'll assume
txtsourcedatabase
contains the directory you wish to process:好的,有几件事。这并不能真正解决您的问题,但您知道,您尝试执行的某些操作可以通过命令提示符 (ren *.fin *.txt) 完成。显然这并不能解决没有扩展名的文件的问题。
其次,您甚至不应该通过使用 *.fin 调用 Dir 命令来获取没有扩展名的文件。
第三,假设您的标准是重命名所有以 .fin 结尾或没有扩展名 .txt 的文件...这应该有效:
Ok, a few things. This won't really fix your problem, but just so you know, some of what you are trying to do can be accomplished from the command prompt (ren *.fin *.txt). Obviously this does not address the issue of files with no extensions.
Secondly, you shouldn't even be getting files with no extensions based on invoking the Dir command with *.fin.
Thirdly, assuming your criteria is to rename all files ending in .fin or having no extension .txt... This should work:
首先,我认为您想将 Dir$ 属性从 vbDirectory 更改为 vbNormal。其次,我认为你应该简化你的重命名代码。您可以使用内置的 VB 函数替换来执行您想要的操作。
First, I think you want to change your Dir$ attribute from vbDirectory to vbNormal. Second I think you should simplify your renaming code. You can use the built-in VB function Replace to do what you want.