Jump to content

unzip and renaming and then zipping


Recommended Posts

Hello,

I need help in creating a script that enables me to the following automatically.

I have a ZIp file by name abc.zip and it contains files-test.txt,dec.drl,tes.txt .What i need is that first it needs to unzip the file and then rename file names to parent zip file like abc.txt,abc.drl,abc.txt. 

and then finally it need to zip it back.

Any inputs are highly appreciated.

Thanks in advance.

Best Regards,

Sekhar 

Link to comment
Share on other sites

  • Moderators

Sekhar,

Welcome to the AutoIt forum. :)

But please pay attention to where you post - the "Examples" section where you started this thread is clearly marked: "This is NOT a general support forum!". I have moved the thread for you, but would ask you to be more careful in future. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Hi Sekhar,

Since I had something similar to what you're asking, here you go.  I have run a quick test and it seems to work okay using your example.  It may be crude but works!  B)

I don't think you can have more than one file with the same name in a zip file, so I will leave that alone.

 

abc.txt,abc.drl,abc.txt

 

You will need the _Zip.au3 UDF created by wraithdu here --> 

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

_ZipRenameToParent()

Func _ZipRenameToParent()
    Local $sPath = "C:\Temp\" ; sets up your path with trailing backslash for use later
    Local $zipfileList = _FileListToArray("C:\Temp", "abc.zip", 1) ; If more than one .zip file, change abc.zip to *.zip
    ; error checking for _FileListToArray
    If @error = 1 Then
        MsgBox(0, "", "No Folder Found.")
        Exit
    EndIf
    If @error = 4 Then
        MsgBox(0, "", "No File(s) Found.")
        Exit
    EndIf

    ; loops through files found from _FileListToArray -- one if you specify a particular filename, or multiple if you use *.zip above
    For $i = 1 to $zipfileList[0]
        $filename = StringTrimRight($zipfileList[$i], 4)
        $zipFile = _Zip_ListAll($sPath&$filename&".zip", 0)
        _Zip_Create($sPath&$filename&"_new.zip") ; create a new zip file to store renamed files into
        For $iZipfiles = 1 to UBound($zipFile) - 1
            _Zip_Unzip($sPath&$filename&".zip", $zipFile[$iZipfiles], $sPath)
            $filetorename = $zipFile[$iZipfiles]
            $fileextension = StringRight($zipFile[$iZipfiles],4)
            FileMove($sPath&$filetorename, $sPath&$filename&$fileextension)
            _Zip_AddItem($sPath&$filename&"_new.zip", $sPath&$filename&$fileextension)
            FileDelete($sPath&$filename&$fileextension)
        Next
        FileDelete($sPath&$filename&".zip") ; deletes the original zip file
        FileMove($sPath&$filename&"_new.zip", $sPath&$filename&".zip") ; renames the _new.zip file to the original filename.zip
    Next
    MsgBox(0,"Done", "Finished renaming files within zip(s)") ; added for sanity purposes :-)
EndFunc
Link to comment
Share on other sites

Hello ,

Thanks for your reply.

I tried and what is happening is that i get message that done renaming but in the  zip file no files are available.all are getting deleted.

Do you have any inputs to resolve this >

Thanks in advance.

Link to comment
Share on other sites

  • Moderators

Sekhar,

Your original requirement has a serious problem - renaming to "abc.txt, abc.drl, abc.txt" will not work as there are 2 files of the same name when renamed. ;)

If you assume that all the extensions are different (I used "test.txt, test.drl, test.au3"), then this script works perfectly for me:

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

#include "_Zip.au3"

; Declare paths
$sName = "abc"
$sOrgZip = @ScriptDir & "\" & $sName & ".zip"
$sNewZip = @ScriptDir & "\Renamed.zip"
$sUnzipped = @ScriptDir & "\Unzipped"
$sRenamed = @ScriptDir & "\Renamed"
; Create folders
DirCreate($sUnzipped)
DirCreate($sRenamed)

; Unzip current file
_Zip_UnzipAll($sOrgZip, $sUnzipped)
; Display results
$aUnzipped = _FileListToArray($sUnzipped, "*.*", 1)
_ArrayDisplay($aUnzipped, "Unzipped", Default, 8)

; Rename files and move to new folder
For $i = 1 To $aUnzipped[0]
    FileMove($sUnzipped & "\" & $aUnzipped[$i], $sRenamed & "\" & $sName & StringRegExpReplace($aUnzipped[$i], "^.*\.", ".$1"))
Next
; Display result
$aRenamed = _FileListToArray($sRenamed, "*.*", 1)
_ArrayDisplay($aRenamed, "Renamed", Default, 8)

; Create new zip file
$sNewFile = _Zip_Create($sNewZip)
; Add renamed files
For $i = 1 To $aRenamed[0]
    _Zip_AddItem($sNewFile, $sRenamed & "\" & $aRenamed[$i])
Next
I leave it to you to check the zip content, delete the original zip and the 2 folders, and finally rename the new zip. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

Sekhar,

You need to add some errorchecking code to see where the error lies. Do you see both of the ArrayDisplay dialogs showing the unzipped and renamed files when you run the code? :huh:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...