Creating a .tar.gz or .tgz double zipped file

Introduction

When we create a new database in TM1 V12 (IBM Planning Analytics Engine or PAE) we will typically upload a database with the files. That database could be a manual backup or a download of an automatic backup. It could be the same model or a completely different model. At any case, PAE expects us to deliver the files and folders in a .tar.gz or .tgz zip file format. This means that you need to zip twice:

  • First, zip your files and folders to a .tar zipped file format
  • Second, zip that .tar file to a .gzip zipped file format

You could use 7zip to do the work but it means 2 zip actions. Not the end of the world but still useful to automate. The AutoHotKey below will investigate all open File Explorer windows and list the paths that are visible. Then the user can select a path and the code will turn the files and folders in a double zipped tar gz file. That new file is located 1 folder up relative to the zipped folder. Change the shortcut key if wanted.

Pictures:

Automation with AutoHotKey

^q:: ; Press Ctrl+Q to run the script
{
    cPath_7zip := A_ProgramFiles "\7-Zip\7z.exe"

    ; Gather all open File Explorer windows and store unique paths
    uniquePaths := {}
    explorerPaths := []
    for window in ComObjCreate("Shell.Application").Windows
    {
        if (window.Name == "File Explorer" || window.Name == "Windows Explorer")
        {
            folderPath := window.document.folder.Self.Path
            if (!uniquePaths.HasKey(folderPath))
            {
                uniquePaths[folderPath] := true
                explorerPaths.Push(folderPath)
            }
        }
    }

    ; If no File Explorer windows are found, show a message and exit
    if (explorerPaths.MaxIndex() = 0)
    {
        MsgBox, 48, No Explorer Windows, No open File Explorer windows were found.
        return
    }

    ; Create a GUI to display and select from the list of paths
    Gui, New, +AlwaysOnTop +LabelMyGui
    Gui, Add, Text,, Select the File Explorer window:
    Gui, Add, ListView, vSelectedExplorerPath w400 h200 gOnListViewDoubleClick_DoubleZip, Path

    ; Add paths to the ListView with conditional coloring
    for index, path in explorerPaths
    {
        Row := LV_Add("", path)
        
        ; Set color for rows that meet specific conditions
        if (FileExist(path "\users") || FileExist(path "\.tm1") || FileExist(path "\.files"))
			LV_Modify(Row, "+Select")
    }

    LV_ModifyCol(1, "AutoHdr")

    Gui, Add, Button, gSelectPath, Double zip
    Gui, Show,, Select File Explorer Window

    return
}

SelectPath:
    ; Retrieve the selected path from the ListView
    LV_GetText(folderPath, LV_GetNext())
    if (!folderPath)
    {
        MsgBox, 48, No Selection, Please select a path before clicking OK.
        return
    }
    Gui, Destroy

	; Prompt the user for the file format choice
	InputBox, userChoice, Archive Format, Choose the archive format:`n1. .tar.gz (default)`n2. .tgz, , , , , , , ,1
	if ErrorLevel
		return

	; Set the file extension based on user input
	if (userChoice = "2")
		extension := ".tgz"
	else
		extension := ".tar.gz"

    ; Define the output archive name (using the folder name with .tar.gz extension)
    outputArchive := folderPath "\..\" SubStr(folderPath, InStr(folderPath, "\", 0, 0) + 1) extension

	; Delete the target double zipped file it already exists
	if FileExist(outputArchive)
		FileDelete, %outputArchive%

    ; First, create a .tar archive
    tarPath := folderPath "\" SubStr(folderPath, InStr(folderPath, "\", 0, 0) + 1) ".tar"
    RunWait, %ComSpec% /c ""%cPath_7zip%" a -ttar "%tarPath%" "%folderPath%\*"", , Hide

    ; Now, compress the .tar file into .gz format
    RunWait, %ComSpec% /c ""%cPath_7zip%" a -tgzip "%outputArchive%" "%tarPath%"", , Hide

    ; Clean up the temporary .tar file
    FileDelete, %tarPath%

    MsgBox, 64, Success, %extension% archive created successfully at:`n%outputArchive%

return

OnListViewDoubleClick_DoubleZip:
    ; Handle double-clicking a list item to select it
    GoSub, SelectPath

MyGuiClose_DoubleZip:
Gui, Destroy
return

Paths in File Explorer windows that contain either the "users" folder, or the .tm1 folder or the .files folder, will be selected. Folders that are visible in more than 1 File Explorer window will be shown only once. Both code additions will surely speed up the choice of path in the ListView.

The user will also be asked which extension he wants: .tar.gz is option 1, the default. Or .tgz is option 2.




Homepage

Section contents

About Wim

Wim Gielis is a Business Intelligence consultant and Excel expert

Other links