文件重命名问题?

发布于 2024-08-11 01:37:03 字数 1306 浏览 3 评论 0原文

我正在使用 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 PMSilver_421_export 等不会转换为 .txt 扩展名。例如,Clockings2mis04062009022511PM.txtSilver_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

昔梦 2024-08-18 01:37:03

如果我正确解释您的问题,您希望遍历目录中的文件并将所有扩展名为“.fin”的文件(或根本没有扩展名)更改为“.txt”。您可以添加对 Microsoft 脚本运行时的引用并使用 FileSystemObject 来相当轻松地完成此操作。在此示例中,我们假设 txtsourcedatabase 包含您要处理的目录:

Dim fso As New FileSystemObject
Dim fil As Variant

For Each fil In fso.GetFolder(txtsourcedatabase).Files
  If (LCase$(fso.GetExtensionName(fil.Name)) = "fin") Or _
      (fso.GetExtensionName(fil.Name) = vbNullString) Then
    fso.MoveFile fil.Path, fso.BuildPath(fso.GetParentFolderName(fil.Path), _
      fso.GetBaseName(fil.Path) & ".txt")
  End If
Next

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:

Dim fso As New FileSystemObject
Dim fil As Variant

For Each fil In fso.GetFolder(txtsourcedatabase).Files
  If (LCase$(fso.GetExtensionName(fil.Name)) = "fin") Or _
      (fso.GetExtensionName(fil.Name) = vbNullString) Then
    fso.MoveFile fil.Path, fso.BuildPath(fso.GetParentFolderName(fil.Path), _
      fso.GetBaseName(fil.Path) & ".txt")
  End If
Next
风吹雨成花 2024-08-18 01:37:03

好的,有几件事。这并不能真正解决您的问题,但您知道,您尝试执行的某些操作可以通过命令提示符 (ren *.fin *.txt) 完成。显然这并不能解决没有扩展名的文件的问题。
其次,您甚至不应该通过使用 *.fin 调用 Dir 命令来获取没有扩展名的文件。
第三,假设您的标准是重命名所有以 .fin 结尾或没有扩展名 .txt 的文件...这应该有效:

Private Const txtsourcedatabasefile As String = "C:\test\"
Public Sub Example()
    Dim filename1 As String
    Dim strOutput As String
    filename1 = Dir$(txtsourcedatabasefile & "*")
    Do While LenB(filename1)
        strOutput = filename1
        If InStrB(1, strOutput, ".", vbBinaryCompare) = 0& Then
            strOutput = strOutput & ".txt"
            Name txtsourcedatabasefile & filename1 As txtsourcedatabasefile & strOutput
        ElseIf LCase$(Right$(filename1, 4)) = ".fin" Then
            Mid$(strOutput, Len(filename1) - 2) = "txt"
            Name txtsourcedatabasefile & filename1 As txtsourcedatabasefile & strOutput
        Else
            Debug.Print "Skipped:", strOutput
        End If
        filename1 = Dir$
    Loop
End Sub

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:

Private Const txtsourcedatabasefile As String = "C:\test\"
Public Sub Example()
    Dim filename1 As String
    Dim strOutput As String
    filename1 = Dir$(txtsourcedatabasefile & "*")
    Do While LenB(filename1)
        strOutput = filename1
        If InStrB(1, strOutput, ".", vbBinaryCompare) = 0& Then
            strOutput = strOutput & ".txt"
            Name txtsourcedatabasefile & filename1 As txtsourcedatabasefile & strOutput
        ElseIf LCase$(Right$(filename1, 4)) = ".fin" Then
            Mid$(strOutput, Len(filename1) - 2) = "txt"
            Name txtsourcedatabasefile & filename1 As txtsourcedatabasefile & strOutput
        Else
            Debug.Print "Skipped:", strOutput
        End If
        filename1 = Dir$
    Loop
End Sub
懒的傷心 2024-08-18 01:37:03

首先,我认为您想将 Dir$ 属性从 vbDirectory 更改为 vbNormal。其次,我认为你应该简化你的重命名代码。您可以使用内置的 VB 函数替换来执行您想要的操作。

Dim filename1 As String

filename1 = Dir$(txtsourcedatabasefile & "\*.fin", vbNormal)

Do While Len(filename1) > 0
   Name txtsourcedatabasefile & "\" & filename1 As txtsourcedatabasefile & "\" & _
       Replace(filename1, ".fin", ".txt", Len(txtsourcedatabasefile & "\" & filename1) - 4, 1, vbTextCompare)
   filename1 = Dir$
Loop

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.

Dim filename1 As String

filename1 = Dir$(txtsourcedatabasefile & "\*.fin", vbNormal)

Do While Len(filename1) > 0
   Name txtsourcedatabasefile & "\" & filename1 As txtsourcedatabasefile & "\" & _
       Replace(filename1, ".fin", ".txt", Len(txtsourcedatabasefile & "\" & filename1) - 4, 1, vbTextCompare)
   filename1 = Dir$
Loop
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文