Hello EveryBody
i want to change a file extention property in visual basic 6
Example;
i have a File test.txt or test.jpg what we want ?
and we use a command botton and two textbox
Text1.text = "C:\test.txt" 'Textbox 1 is my file path
Text2.text = ".pdf" or ".psd" ' textbox 2 is my extention field beacause any time we want to change extention. psd,pdf,tmp,jpg Etc
change Textbox1 file path file extention in textbox2 Extention

When i use orignal extention then my file work.

Dani AI

Generated

Nice job, . Your approach works; here are a couple of safe-guards and a slightly simpler variant that plays nicely with inputs like ".pdf" or "pdf", handles names with multiple dots, and avoids renaming when nothing changes. Note that this only renames; it does not convert the file format.

Private Function SafeChangeExt(ByVal src As String, ByVal ext As String) As Boolean
    On Error GoTo Fail

    ' Must exist before we try to rename
    If Len(Dir$(src)) = 0 Then Exit Function

    ' Normalize the extension: allow ".pdf" or "pdf"
    ext = Trim$(ext)
    If Left$(ext, 1) = "." Then ext = Mid$(ext, 2)
    If ext = "" Then Exit Function

    ' Find the last dot after the last backslash
    Dim slash As Long, dot As Long, base As String, dst As String
    slash = InStrRev(src, "\")
    dot = InStrRev(src, ".")
    If dot > slash Then base = Left$(src, dot - 1) Else base = src

    dst = base & "." & ext
    If StrComp(src, dst, vbTextCompare) <> 0 Then Name src As dst

    SafeChangeExt = True
    Exit Function
Fail:
    ' Common failures: file in use (Error 70), or permissions
    SafeChangeExt = False
End Function

Private Sub Command1_Click()
    Call SafeChangeExt(Text1.Text, Text2.Text)
End Sub

Why these changes:

  • InStrRev finds the last dot without looping, which is ideal for names like photo.backup.2024.jpg. See InStrRev function.
  • Name only renames on the same drive. If you ever build a destination on another drive, copy then delete the original: FileCopy src, dst : Kill src. See Can’t rename with different drive (Error 74).
  • If you hit "Permission denied" (Error 70), the file is likely open or locked by another process. Close handles or retry later. See Permission denied (Error 70).

Alternative: you can build the new path with the FileSystemObject for readability using GetBaseName, GetParentFolderName, and BuildPath (GetBaseName, BuildPath).

Recommended Answers

All 2 Replies

See if this helps :

Public Function ChangeFileExt(ByVal aFilename As String, ByVal NewExt As String) As Boolean
Dim p As Long
Dim bp As Long
Dim nFileName As String

On Error Resume Next
ChangeFileExt = False
If aFilename = "" Then Exit Function
p = 0
Do
    bp = p
    p = InStr(p + 1, aFilename, ".", vbBinaryCompare)
Loop Until p = 0

If bp > 0 Then
    nFileName = Left(aFilename, bp - 1)
Else
    nFileName = aFilename
End If

nFileName = nFileName & "." & NewExt

Err.Clear

Name aFilename As nFileName

If Err.Number = 0 Then ChangeFileExt = True

End Function

' Change file type
Private Sub Command1_Click()
Call ChangeFileExt("D:\test.txt", "jx") ' from .txt become .jx
End Sub

Your case :

Text1.Text = "D:\test.txt"
Text2.Text = "jx"
Private Sub Command1_Click()
Call ChangeFileExt(Text1.Text, Text2.Text) 
End Sub

Thanks Brother its work i am very thanks full to you
you are a genius
Thanks thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.