change specific file extension

Dossfm0q

Banned User
Joined
Mar 9, 2009
Messages
570
Office Version
  1. 2019
Platform
  1. Windows
How change specific file extension ?

Code:
Sub changeExt()


    strDir = ThisWorkbook.Path & "\"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFolder(strDir)
        With CreateObject("wscript.shell")
          For Each file In f.Files
          
            If InStr(file.Name, "root.zip") > 0 Then
            .currentdirectory = strDir
           .Run "%comspec% /c ren *.zip *.docx", 0, True
            End If
          Next
        End With


End Sub


Sub changeExt1()


    strDir = ThisWorkbook.Path & "\"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFolder(strDir)
        With CreateObject("wscript.shell")
          For Each file In f.Files
          
            If InStr(file.Name, "root.docx") > 0 Then
            .currentdirectory = strDir
           .Run "%comspec% /c ren *.docx *.zip", 0, True
            End If
          Next
        End With


End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
.
I'm not certain it is possible to change a file's extension to / from ZIP and expect it to function correctly.

If you want a file to have the extension ZIP, I believe you will need to actually ZIP that file. There are macros on the NET that can do that for you.
Likewise with going FROM zip to another extension. You will need to UNZIP it. There are macros on the NET that can do that for you.
 
Upvote 0
Thanks For respond

I found below macro and it work well need to go to VBAproject Tools >> reference and Check "Microsoft Scripting Runtime"

Code:
Public Sub CnvrtExtn()

StrPath = ThisWorkbook.Path '<<<<<< to be change as file path is
Call ConvertToTXT(StrPath)
End Sub
Private Sub ConvertExtension(filePath As String)

    With New Scripting.FileSystemObject
        Dim directory As Folder
        Set directory = .GetFolder(filePath)
        Dim target As File
        For Each target In directory.Files
            If InStr(target.Name, "MyFile.zip") > 0 Then ' or ".zip"
                Dim newName As String
                newName = .BuildPath(filePath, .GetBaseName(MyFile.Name)) & "MyFile.docx" ' or ".docx"
                .MoveFile target.Path, newName
            End If
        Next
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,705
Messages
6,173,996
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top