English 中文(简体)
File Rename problem?
原标题:

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?

最佳回答

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
问题回答

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:	est"
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

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




相关问题
Prevent windows from queuing shellexecute requests

Win.ShellExecute 0, "open", "C:dirprogram.exe", "arguments", vbNullString, SW_SHOWNORMAL Win.ShellExecute 0, "open", "http://www.google.com", vbNullString, vbNullString, SW_SHOWNORMAL I want google....

Why is My Loop Only Deleting One File?

Using VB6 In a folder, i have n number of files, i want to delete a 0 kb files code Dim filename5 As String filename5 = Dir$(txtsourcedatabasefile & "*_*", vbDirectory) MsgBox filename5 Do ...

How to check the filesize?

Using VB6 I have the text file with different sizes, so i want to delete the file where filesize = 0 kb. How to make a vb6 code for deleting the 0 kb files. Need vb6 code Help

File Rename problem?

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 ...

Error 20728-F while in using Crystal Reports in VB6

I m using Crystal Reports in my VB6 project, but I m facing error while loading the report in crystalreport1.action=1; Please give me some solution for this problem. It is showing the error as Error ...

DllRegisterServer entry point was not found

When running my vb6 application I am getting error like, runtime error 53 : file not found: rscomclNoMsg.dll then i tried to register that dll from cmd line using regsvr32. Then I am getting ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签