• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA code tweaking

The code mentioned above zips files in a particular folder. How can i replace .zip with 7z

  • poll choice

    Votes: 0 0.0%
  • poll choice

    Votes: 0 0.0%
  • poll choice

    Votes: 0 0.0%

  • Total voters
    0
  • Poll closed .

lloydbangera

New Member
Code:
Option Explicit
Dim m_astrOldFilePaths() As String
Dim m_lngOldFileCount As Long

Public Sub ZipOldFiles()

  '++++++++++++++++++++++++++
  '+++ RUN THIS PROCEDURE +++
  '++++++++++++++++++++++++++
 
  Dim vntAgeInMonths
  Dim vntFolderPath
  Dim vntFilePath
  Dim lngZipCount As Long
  Dim wksResults As Worksheet
 
  On Error GoTo ErrorHandler
  Erase m_astrOldFilePaths
  m_lngOldFileCount = 0
 
  vntFolderPath = GetFolderPath()
  If IsEmpty(vntFolderPath) Then Exit Sub
 
  vntAgeInMonths = GetAgeInMonths()
  If IsEmpty(vntAgeInMonths) Then Exit Sub
 
  Call GetOldFilePaths(CStr(vntFolderPath), CInt(vntAgeInMonths), True)
  If m_lngOldFileCount > 0 Then
  On Error Resume Next
  Set wksResults = ThisWorkbook.Sheets("ZIP RESULTS")
 
  On Error GoTo ErrorHandler
  If wksResults Is Nothing Then
  Set wksResults = ThisWorkbook.Sheets.Add()
  wksResults.Name = "ZIP RESULTS"
  Else
  wksResults.Activate
  wksResults.Cells.Clear
  End If
 
  wksResults.Range("A1").Value = "The following files were zipped."
  wksResults.Range("A1").Font.Bold = True
 
  For Each vntFilePath In m_astrOldFilePaths
  If ZipFile(CStr(vntFilePath)) Then
  lngZipCount = lngZipCount + 1
  wksResults.Range("A" & lngZipCount + 2).Value = vntFilePath
  Kill vntFilePath ' <-- Only uncomment after testing!
  End If
  Next vntFilePath
 
  MsgBox "Macro run is complete!"
 
  '' MsgBox Format(lngZipCount, "#,0") _
  '' & " of " & Format(m_lngOldFileCount, "#,0") _
  '' & " old files were zipped successfully.", vbInformation
  Else
 
  MsgBox "Macro run is complete!"
 
  ''MsgBox "No files over " & vntAgeInMonths _
  ''  & " month(s) old were found.", vbInformation
  End If
  Exit Sub
 
ErrorHandler:
  MsgBox Err.Description, vbExclamation
End Sub

Private Function GetAgeInMonths()
  Dim blnValid As Boolean
  Dim strInput As String
  Dim dblInput As Double
 
  On Error GoTo ErrorHandler
  Do
  strInput = InputBox("Enter age in months:", "Zip Old Files", 3)
  If strInput <> vbNullString Then
  If IsNumeric(strInput) Then
  dblInput = Val(strInput)
  If dblInput = Int(strInput) Then
 
  Rem --------------------------------------------------------------------------------------- Condition for age
 
  If dblInput >= 0 And dblInput <= 6 Then
  blnValid = True
  End If
  End If
  End If
  If Not blnValid Then
  MsgBox "You must enter a whole number between 1 and 6.", vbExclamation
  End If
  End If
  Loop Until (strInput = vbNullString) Or blnValid
 
  If strInput = vbNullString Then
  GetAgeInMonths = Empty
  Else
  GetAgeInMonths = Int(strInput)
  End If
  Exit Function
 
ErrorHandler:
  GetAgeInMonths = Empty
End Function

Private Function GetFolderPath()
  Const msoFileDialogFolderPicker = 4
  Dim objFolderPicker As Object
 
  On Error GoTo ErrorHandler
  Set objFolderPicker = Application.FileDialog(msoFileDialogFolderPicker)
  objFolderPicker.Title = "Zip Old Files"
  objFolderPicker.ButtonName = "Select Folder"
 
  If objFolderPicker.Show() Then
  GetFolderPath = objFolderPicker.SelectedItems(1)
  End If
  Exit Function

ErrorHandler:
  GetFolderPath = Empty
End Function

Private Sub GetOldFilePaths(strFolderPath As String, _
  intAgeInMonths As Integer, _
  Optional blnRecursive As Boolean = False)
 
  Dim objFileSystem As Object
  Dim objSubfolder As Object
  Dim objFolder As Object
  Dim objFile As Object
 
  Set objFileSystem = CreateObject("Scripting.FileSystemObject")
  Set objFolder = objFileSystem.GetFolder(strFolderPath)
 
  On Error GoTo FileError
  For Each objFile In objFolder.Files
 
  Rem ----------------------------------------------------------------------  Check for age of file Date Accessed/ Date Created
 
  ' DateLastAccessed
  ' DateCreated
  ' DateLastModified
 
  If objFile.DateLastAccessed < DateAdd("m", -intAgeInMonths, Now()) Then
  m_lngOldFileCount = m_lngOldFileCount + 1
  ReDim Preserve m_astrOldFilePaths(1 To m_lngOldFileCount)
  m_astrOldFilePaths(m_lngOldFileCount) = objFile.Path
  End If
  GoTo NextFile
FileError:
  Err.Clear
  Resume NextFile
NextFile:
  Next objFile
 
  If blnRecursive Then
  On Error GoTo SubfolderError
  For Each objSubfolder In objFolder.SubFolders
  Call GetOldFilePaths(objSubfolder.Path, intAgeInMonths, True)
  GoTo NextSubfolder
SubfolderError:
  Err.Clear
  Resume NextSubfolder
NextSubfolder:
  Next objSubfolder
  End If
 
  Set objFileSystem = Nothing
  Set objSubfolder = Nothing
  Set objFolder = Nothing
  Set objFile = Nothing
End Sub

Private Function ZipFile(strFilePath As String) As Boolean
  Dim strParentFolderPath As String
  Dim strBaseFileName As String
  Dim strZipFilePath As String
  Dim objFileSystem As Object
  Dim blnError As Boolean
  Dim objShell As Object
  Dim cnt As Variant
 
 
  On Error GoTo ErrorHandler
  Set objFileSystem = CreateObject("Scripting.FileSystemObject")
  strParentFolderPath = objFileSystem.GetParentFolderName(strFilePath) & "\"
  strBaseFileName = objFileSystem.GetBaseName(strFilePath)
  cnt = objFileSystem.GetFolder(strParentFolderPath).Files.Count
  strZipFilePath = strParentFolderPath & strBaseFileName & ".zip"
 
 
  Dim sFile As Variant, ExtFind As Variant
  sFile = Dir(strParentFolderPath & strBaseFileName & "*")
  ExtFind = Right$(sFile, Len(sFile) - InStrRev(sFile, "."))
  If ExtFind = "zip" Then
  blnError = False
  GoTo ExitHandler
  End If
  Debug.Print ExtFind
 
  objFileSystem.CreateTextFile(strZipFilePath, True).Close
 
  Set objShell = CreateObject("Shell.Application")
  objShell.Namespace(CVar(strZipFilePath)).CopyHere CVar(strFilePath)
 
  Rem ------------------------------------------------------------------------------  Wait time
 
  Do
  Application.Wait (Now + TimeValue("00:00:05"))
  Loop Until objShell.Namespace(CVar(strParentFolderPath)).Items.Count <> cnt

  ''Application.Wait (Now + TimeValue("00:00:20"))
 
ExitHandler:
  On Error Resume Next
  ZipFile = Not blnError
  If ExtFind = "zip" Then ZipFile = False
  If blnError Then objFileSystem.DeleteFile strZipFilePath
  Set objFileSystem = Nothing
  Set objShell = Nothing
  Exit Function
 
ErrorHandler:
  blnError = True
  Resume ExitHandler
End Function
 
Last edited by a moderator:
lloydbangera
Have You tried to use replace needed "zip"-texts with "7z"-text in that code?
Hello,
I tried doing that but it doesn't work at all. I checked a lot of sites but it doesn't help. Can you please pls help. I am just not sure what to do.

Also, the code when you run it has another problem. If there are 2 files. One zipped and the other unzipped with the same names. It just skips the unzipped file. Ideally I would like it to zip that file too and delete the unzipped version.

Any help will be appreciated. Thanks a lot in advance.
 
lloydbangera
1) As written: ... needed ...
2) If there are something challenges with names ...
= Why You won't do this the whole process MANUALLY?
3) That code has MANY features which won't work at all here!
= I cannot test that at all!
4) You should send an Excel Sample file which has needed data and code.
Then, maybe someone else would give an answer ...
 
lloydbangera
1) As written: ... needed ...
2) If there are something challenges with names ...
= Why You won't do this the whole process MANUALLY?
3) That code has MANY features which won't work at all here!
= I cannot test that at all!
4) You should send an Excel Sample file which has needed data and code.
Then, maybe someone else would give an answer ...
Hello.. Thank you for the quick revert. Unfortunately. I cannot upload any files since they are confidential. I cannot do them manually as there are more than 800,000 files. The code does zip the files correctly. I am looking to use the 7z version instead of .zip as we get more compression on the 7z version.
 
I cannot upload any files since they are confidential.

Have a read of here.
https://chandoo.org/forum/threads/posting-a-sample-workbook.451/#post-73705

But why are you using VBA for 7z compression? I'd recommend PowerShell. It's faster, and more appropriate tool for this sort of thing.
https://stackoverflow.com/questions/13180346/script-to-create-archive-using-powershell-and-7zip

If you must use VBA, then have a read of Ron's site.
https://www.rondebruin.nl/win/s7/win003.htm
 
Back
Top