extra files

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
1,134
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
I have this code and not sure it is not extracting the files. no error giving me msgbox.
Idea is simple to extra all the files from the .gz (screen shot attached)

VBA Code:
Sub ExtractDataExportFilesWithWinRAR()
    ' Define the destination path, folder name, and extraction command
    Dim destinationPath As String
    Dim folderName As String
    Dim fileName As String
    Dim extractCommand As String

    ' Specify the destination path where you want to extract the files
    destinationPath = ThisWorkbook.Path & "\" & "NAF Extra File" ' You can change this to your desired destination path
    
    ' Specify the folder name that contains files with "DataExport" in their names
    folderName = "NAF Extra File"
    
    ' Iterate through files in the destination folder
    fileName = Dir(destinationPath & "*.gz")
    
    ' Loop through each file
    Do While fileName <> ""
        ' Check if the file name contains "DataExport"
        If InStr(1, fileName, folderName, vbTextCompare) > 0 Then
            ' Specify the command to extract the file using WinRAR
            extractCommand = "WinRAR x """ & destinationPath & fileName & """ """ & destinationPath & """"
            
            ' Print the extraction command to the Immediate Window for debugging
            Debug.Print "Extraction Command: " & extractCommand

            ' Execute the extraction command
            Call Shell(extractCommand, vbHide)
            
            ' Print a message indicating that the file is being extracted
            Debug.Print "Extracting file: " & fileName
        End If
        
        ' Get the next file in the destination folder
        fileName = Dir
    Loop

    ' Print a message indicating that the extraction process is complete
    MsgBox "Extraction completed successfully!"
End Sub

1705322108439.png
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I do not have WinRAR to test but the following should help.

You can assign the macro to a button and then when you click the button you will be asked to select the folder. After selecting the folder it should extract the contents of the TAR.GZ file to

VBA Code:
Sub ExtractTARGZcontents()
    Dim filePath As String
    Dim folderPath As String
    Dim fileName As String
    Dim destinationPath As String
    
    ' Open file picker
    With Application.FileDialog(msoFileDialogFilePicker)
        .Title = "Select TAR GZ file"
        .Filters.Clear
        .Filters.Add "TAR GZ Files", "*.tar.gz"
        
        If .Show = -1 Then ' User selected a file
            filePath = .SelectedItems(1)
            fileName = Mid$(filePath, InStrRev(filePath, "\") + 1)
            folderPath = Left$(filePath, InStrRev(filePath, "\"))
        Else
            Exit Sub ' User canceled
        End If
    End With
    
    ' Set destination path to the same directory as the workbook in a folder called "extracted"
    destinationPath = ThisWorkbook.Path & "\extracted\"
    
    ' Create folder if not exists
    If Dir(destinationPath, vbDirectory) = "" Then
        MkDir destinationPath
    End If
    
    ' Use WinRAR to extract
    Shell "WinRAR x """ & filePath & """ """ & destinationPath & """", vbHide
    
    ' Inform the user
    MsgBox "Extraction completed! Files are in: " & desti
nationPath, vbInformation
End Sub

t0ny84
 
Upvote 0
I do not have WinRAR to test but the following should help.

You can assign the macro to a button and then when you click the button you will be asked to select the folder. After selecting the folder it should extract the contents of the TAR.GZ file to

VBA Code:
Sub ExtractTARGZcontents()
    Dim filePath As String
    Dim folderPath As String
    Dim fileName As String
    Dim destinationPath As String
   
    ' Open file picker
    With Application.FileDialog(msoFileDialogFilePicker)
        .Title = "Select TAR GZ file"
        .Filters.Clear
        .Filters.Add "TAR GZ Files", "*.tar.gz"
       
        If .Show = -1 Then ' User selected a file
            filePath = .SelectedItems(1)
            fileName = Mid$(filePath, InStrRev(filePath, "\") + 1)
            folderPath = Left$(filePath, InStrRev(filePath, "\"))
        Else
            Exit Sub ' User canceled
        End If
    End With
   
    ' Set destination path to the same directory as the workbook in a folder called "extracted"
    destinationPath = ThisWorkbook.Path & "\extracted\"
   
    ' Create folder if not exists
    If Dir(destinationPath, vbDirectory) = "" Then
        MkDir destinationPath
    End If
   
    ' Use WinRAR to extract
    Shell "WinRAR x """ & filePath & """ """ & destinationPath & """", vbHide
   
    ' Inform the user
    MsgBox "Extraction completed! Files are in: " & desti
nationPath, vbInformation
End Sub

t0ny84
thank you will give it a try
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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