Jump to content

[Solved] Zipping folders from the path given


Recommended Posts

Hi Experts,

Hope everyone are having a good day!

I have this question so far, is it possible for AutoIt language can zip a multiple folders from the path given? like the below folders.

Before:

image.png.4a9d564c9e7180f90636a00bcae5ed02.png

 

After and expected:

image.png.34c60a33ad35f9802519a08ac19ab847.png

 

I tried checking the net but only single folder zipping. Maybe someone here can point me to the right direction.

 

Thanks in advance Experts....

 

KS15

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

21 minutes ago, KickStarter15 said:

Maybe someone here can point me to the right direction.

Would the following be an acceptable solution for you?
You read the folders into an array using _FileListToArrayRec (flag = $FLTAR_FOLDERS). Then loop through the array and pack each folder with the command line variant of 7-Zip (place 7z.dll and 7za.exe i.e. in the @ScriptDir).

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

25 minutes ago, Musashi said:

(place 7z.dll and 7za.exe i.e. in the @ScriptDir).

Thanks, that can be done.

 

26 minutes ago, Musashi said:

pack each folder with the command line variant of 7-Zip

Is there a package 7-zip on this that is needed?

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

On 2/6/2020 at 9:00 AM, KickStarter15 said:

Is there a package 7-zip on this that is needed? 

You will find the components 7z.dll and 7za.exe if you install 7-Zip.

 

Edited by Musashi
Attachment removed

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Here an example script :

#include <File.au3>
#include <StringConstants.au3>
#include <Array.au3>

Global $g_sBaseDir, $g_aFolders, $g_sCommand
$g_sBaseDir = @ScriptDir

$g_aFolders = _FileListToArray($g_sBaseDir, "*", $FLTAR_FOLDERS)
If @error Then
    ConsoleWrite("! @@ Error : _FileListToArray" & @CRLF)
    Exit
EndIf
_ArrayDisplay($g_aFolders, "Folders") ; *** just for display

For $i = 1 To $g_aFolders[0]
    ; only folders with 10 digits are valid :
    If StringRegExp($g_aFolders[$i], "^\d{10}$", $STR_REGEXPMATCH) Then
        ConsoleWrite("> >>>> Foldername match = " & $g_aFolders[$i] & @CRLF)
        
        ; Variant 1 : without Progresswindow
        ;$g_sCommand = @ScriptDir & '\7za.exe a -y -tzip "' & $g_aFolders[$i] & '" "' & $g_aFolders[$i] & '"'
        ;RunWait ($g_sCommand, "", @SW_HIDE)

        ; Variant 2 : with Progresswindow
        $g_sCommand = @ScriptDir & '\7zG.exe a -y -tzip "' & $g_aFolders[$i] & '" "' & $g_aFolders[$i] & '"'
        RunWait ($g_sCommand, "", @SW_SHOW)
    EndIf
Next

 

EDIT : @KickStarter15

Besides 7za.exe, 7-Zip offers another standalone variation (7zG.exe) with a progress bar. This can be useful if the folders contain large amounts of data. I have enhanced the example above and also added the 7zG.exe as a zipfile (see attachment).

7zG.zip

Edited by Musashi
Enhancement

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

@Musashi

Thanks it is working. However, the folder is not just 10 digits or how many letters, it vary on the folder being stored on that certain path. So how can I change this peace of RegEx "^\d{10}$" here.

Some Folders has folder name of ABC1234 or ABCD11 means letters and numbers.

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

5 minutes ago, KickStarter15 said:

However, the folder is not just 10 digits

Is there any pattern by which the folders can be identified? Otherwise all folders of the base directory would be taken.
Alternatively you could create an array with the desired folders, e.g. as .csv , or in the script itself.

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

9 minutes ago, KickStarter15 said:

I think i got it, I change this RegEx "^\d{10}$" to this "^." means anything. Is this correct?

If you want to pack all folders, then you can completely skip If StringRegExp ;).

#include <File.au3>
#include <StringConstants.au3>
#include <Array.au3>

Global $g_sBaseDir, $g_aFolders, $g_sCommand
$g_sBaseDir = @ScriptDir

$g_aFolders = _FileListToArray($g_sBaseDir, "*", $FLTAR_FOLDERS)
If @error Then
    ConsoleWrite("! @@ Error : _FileListToArray" & @CRLF)
    Exit
EndIf
_ArrayDisplay($g_aFolders, "Folders") ; *** just for display

For $i = 1 To $g_aFolders[0]
    ConsoleWrite("> >>>> Foldername = " & $g_aFolders[$i] & @CRLF)

    ; Variant 1 : without Progresswindow
    ;$g_sCommand = @ScriptDir & '\7za.exe a -y -tzip "' & $g_aFolders[$i] & '" "' & $g_aFolders[$i] & '"'
    ;RunWait ($g_sCommand, "", @SW_HIDE)

    ; Variant 2 : with Progresswindow
    $g_sCommand = @ScriptDir & '\7zG.exe a -y -tzip "' & $g_aFolders[$i] & '" "' & $g_aFolders[$i] & '"'
    RunWait ($g_sCommand, "", @SW_SHOW)
Next

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

If someone doesn't want to use external program, this could be an alternative :

#include <Constants.au3>
#include <String.au3>
#include <Array.au3>
#include <File.au3>

Opt("MustDeclareVars", 1)

Const $g_sBaseDir = "C:\Apps\Temp"

Local $g_aFolders = _FileListToArray($g_sBaseDir, "*", $FLTAR_FOLDERS, True)
If @error Then Exit ConsoleWrite("! @@ Error : _FileListToArray" & @CRLF)

_ArrayDisplay($g_aFolders, "Folders") ; *** just for display

For $i = 1 To $g_aFolders[0]
  ConsoleWrite("> >>>> Foldername = " & $g_aFolders[$i] & @CRLF)
  Zip($g_aFolders[$i] & ".zip", $g_aFolders[$i])
Next

Func Zip($sZipFile, $sSourceFolder)
  If FileExists($sZipFile) Then Return SetError(1)  ; destination file already exists
  If Not FileExists($sSourceFolder) Then Return SetError(2)  ; source does not exist
  Local $hFile = FileOpen($sZipFile, $FO_CREATEPATH + $FO_OVERWRITE + $FO_BINARY)
  Local Const $sString = Chr(80) & Chr(75) & Chr(5) & Chr(6) & _StringRepeat(Chr(0), 18)
  FileWrite($hFile, $sString)
  FileClose($hFile)
  Local $oShell = ObjCreate("shell.application")
  If Not $oShell.NameSpace($sSourceFolder).items.count Then Return SetError(3)   ; folder empty
  Local $iFiles = 0
  For $oFile In $oShell.NameSpace($sSourceFolder).items
    $oShell.NameSpace($sZipFile).copyhere($oFile)
    Do
      Sleep(100)
    Until $oShell.NameSpace($sZipFile).items.count > $iFiles
    $iFiles = $oShell.NameSpace($sZipFile).items.count
  Next
EndFunc   ;==>Zip

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...