views:

144

answers:

3

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?

+3  A: 

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
raven
A: 

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
Oorang
Why is this down voted?
Oorang
A: 

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
Beaner