Sign in to follow this  
Followers 0
jben

Help with making my code a little more simple

7 posts in this topic

#1 ·  Posted (edited)

Hi everyone,

I have this code that i've put together from the forums. However I have found that its getting very large, considering all it does is extract a zip file to a temp dir and then extract another zip file that was contained within the zip file, and then copy certain contents from a folder.

My code seems rather large but I'm really not sure how I could condense it. My main worry is that I need to go through this process another 3 times to get other files from other folders(might be able to use a loop somehow?)...Sorry i've posted such a large amount of code and really hope someone can assist me in this dreadful matter. Thanks

#include-once
#include<MyExtras\_ZipPlugin.au3>;this is required for unzipping on dev PC
;unless the Au3Zip.dll is in the system folder.Just leaving this here for security.
#include <File.au3>;this is required for checking files.
#include <MyExtras\folder.au3>;this is for checking folders.
$szPluginPath = @ScriptDir &"\Au3Zip.dll" 
$hPluginDll = PluginOpen($szPluginPath)

;------------------------------------------------------------------------------------------
; INITIAL CHECKS BEFORE PROCEEDING
;------------------------------------------------------------------------------------------

If $hPluginDll = -1 Then
    MsgBox(266288,"Error:","Plugin not initialized.")
    Exit 
endif

if Not FileExists ("C:\INPUT\*.zip") Then Exit

;------------------------------------------------------------------------------------------
; RENAMES PARENT ZIP
;------------------------------------------------------------------------------------------

;THIS CODE WILL RENAME THE ZIP FOLDER IN THE INPUT FOLDER TO A NAME THAT THE CODE WILL
;RECOGNIZE. THE PURPOSE IN THIS IS TO ENSURE THAT REGARDLESS OF THE ZIP FILE NAME, THE CODE
;WILL STILL EXTRACT THE FILES.ORIGINAL ZIP NAME IS IN THE FORMAT OF "WEEKLY UPDATE (DATE).ZIP"
;(DATE) IS UNIQUE FOR EACH UP ISSUE OF THE ZIP FILE, THE CODE BELOW RESOLVES THIS ISSUE.

$targetDir = "C:\INPUT\"
$aZipList = _FileListToArray($targetDir,"*.zip",1);this returns files only as defined in the AutoIT help files.
If IsArray($aZipList) Then
    FileMove($targetDir &$aZipList[1],$targetDir &"DATA_PARENT.zip")
EndIf

;------------------------------------------------------------------------------------------
; EXTRACTS PARENT ZIP
;------------------------------------------------------------------------------------------

;THIS CODE WILL EXTRACT THE INFORMATION IN THE INPUT FOLDER, WHICH HAS BEEN RENAMED TO DATA_PARENT

$szZipFile = "C:\INPUT\DATA_PARENT.zip";this is the zip file to extract
$szDestDir = "C:\EXTRCT_TEMP";this is the destination to extract the zip files

If $hPluginDll = -1 Then
    MsgBox(266288,"Error:","Plugin not initialized.")
    Exit
EndIf

$aZipList = _ZipList2Array($szZipFile)
for $x =0 to UBound($aZipList)-1
    If StringLower(StringRight($aZipList[$x][1],4)) = ".zip" Then;this code extracts the file with the extensions specified here
       ConsoleWrite("Extracting:"&$aZipList[$x][1]);just shows Extracting in the AutoIT console at bottom for debugging purposes
        $vResult = _ZipUnZipItem($szZipFile,$aZipList[$x][1],$szDestDir)
        ConsoleWrite(" -- "&_ZipFormatMessage($ZR_RECENT)&@lf)
    EndIf   
Next


;------------------------------------------------------------------------------------------
; RENAMES CHILD ZIP
;------------------------------------------------------------------------------------------

;THIS CODE WILL SEARCH THE TEMP FOLDER FOR THE CHILD ZIP AND RENAME TO DATA_CHILD.ZIP
;THIS WILL ALSO ENSURE THAT REGARDLESS OF THE FILE (DATE) THAT THE FILE WILL STILL BE 
;RECOGNIZED AND EXTRACTED.

$F_Array = __FldrListToArray("C:\EXTRCT_TEMP\29944M_10386\", 0);the rest of this string is detected in the code below.
For $I = 1 To Ubound($F_Array) -1
$targetDir = $F_Array[$I];this code states that the targetdir is the information provided via the $F_Array
;the purpose in this is because the $F_Array obtains the folder within the 29944M_10386 folder to complete
;the string, which is then used in the code below to rename the .zip file.
$aZipList = _FileListToArray($targetDir,"*.zip",0)
    If IsArray($aZipList) Then
;MsgBox(0, "TEST PATH", $targetDir &$aZipList[1] & @CRLF & $targetDir &"DATA_CHILD.zip");JUST FOR TESTING PURPOSES
    FileMove($targetDir &$aZipList[1],$targetDir &"DATA_CHILD.zip")
    EndIf
Next


;------------------------------------------------------------------------------------------
; EXTRACTS CHILD ZIP .NCP & .LCN FILE
;------------------------------------------------------------------------------------------

;THIS CODE EXTRACTS THE .NCP and .LCN FILE(S) FROM THE SPECIFIED FOLDER

$szZipFile = $targetDir &"DATA_CHILD.zip";this is the zip file to extract
$szDestDir = "C:\OUTPUT";this is the destination to extract the zip files

$aZipList = _ZipList2Array($szZipFile)
for $x =0 to UBound($aZipList)-1
    $file_type = StringLower(StringRight($aZipList[$x][1],4))
    If $file_type = ".ncp" Or $file_type = ".lcn" Then
      ConsoleWrite("Extracting:"&$aZipList[$x][1])
         $vResult = _ZipUnZipItem($szZipFile,$aZipList[$x][1],$szDestDir)
        ConsoleWrite(" -- "&_ZipFormatMessage($ZR_RECENT)&@lf)
        
    EndIf   
Next

;------------------------------------------------------------------------------------------
; MOVES FILES TO APPROPRIATE LOCATION. INCLUDES ERROR CHECKING AND NOTIFICAITON
;------------------------------------------------------------------------------------------
#include <MyExtras\NotificationBar.au3>;this is a notification bar that is compiled during into setup once built


DirMove("C:\OUTPUT\MASTER\","C:\OUTPUT\29944M_10386",9);moves the .ncp file to the folder (creates folder if doesn't exist)
DirRemove("C:\OUTPUT\MASTER");deletes the original folder


;------------------------------------------------------------------------------------------
; CLEANUP
;------------------------------------------------------------------------------------------
;Cleanup procedure

DirRemove("C:\EXTRCT_TEMP", 1);Delete TEMP
FileDelete("C:\INPUT\*.zip");Deletes any zip files within the input folder

PluginClose($hPluginDll);closes the Au3Zip.dll plugin
Edited by jben

Share this post


Link to post
Share on other sites



you can chage this...

if FileExists ("C:\INPUT\*.zip")then
else
    exit
endifoÝ÷ ÚÚ4Ib殶­sfbæ÷BfÆTW7G2gV÷C´3¢b3#´åUBb3#²¢ç¦gV÷C²FVâW

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks..Thats a couple of lines down haha..

updated the post...Took out the silly notification aswell.

Is it possible to create a loop so I don't have to copy this code 4 times over... The code below is the bit I would hope I could loop so I dont have to copy it a couple of times..I will be checking a couple of different folders. I could quite easily copy the code below,pase it a 3 times and just change the folder name but thats going to make the situation worse :-(

;------------------------------------------------------------------------------------------
; RENAMES CHILD ZIP
;------------------------------------------------------------------------------------------

;THIS CODE WILL SEARCH THE TEMP FOLDER FOR THE CHILD ZIP AND RENAME TO DATA_CHILD.ZIP
;THIS WILL ALSO ENSURE THAT REGARDLESS OF THE FILE (DATE) THAT THE FILE WILL STILL BE 
;RECOGNIZED AND EXTRACTED.

$F_Array = __FldrListToArray("C:\EXTRCT_TEMP\29944M_10386\", 0);the rest of this string is detected in the code below.
For $I = 1 To Ubound($F_Array) -1
$targetDir = $F_Array[$I];this code states that the targetdir is the information provided via the $F_Array
;the purpose in this is because the $F_Array obtains the folder within the 29944M_10386 folder to complete
;the string, which is then used in the code below to rename the .zip file.
$aZipList = _FileListToArray($targetDir,"*.zip",0)
    If IsArray($aZipList) Then
;MsgBox(0, "TEST PATH", $targetDir &$aZipList[1] & @CRLF & $targetDir &"DATA_CHILD.zip");JUST FOR TESTING PURPOSES
    FileMove($targetDir &$aZipList[1],$targetDir &"DATA_CHILD.zip")
    EndIf
Next

;------------------------------------------------------------------------------------------
; EXTRACTS CHILD ZIP .NCP & .LCN FILE
;------------------------------------------------------------------------------------------

;THIS CODE EXTRACTS THE .NCP and .LCN FILE(S) FROM THE SPECIFIED FOLDER

$szZipFile = $targetDir &"DATA_CHILD.zip";this is the zip file to extract
$szDestDir = "C:\OUTPUT";this is the destination to extract the zip files

$aZipList = _ZipList2Array($szZipFile)
for $x =0 to UBound($aZipList)-1
    $file_type = StringLower(StringRight($aZipList[$x][1],4))
    If $file_type = ".ncp" Or $file_type = ".lcn" Then
      ConsoleWrite("Extracting:"&$aZipList[$x][1])
         $vResult = _ZipUnZipItem($szZipFile,$aZipList[$x][1],$szDestDir)
        ConsoleWrite(" -- "&_ZipFormatMessage($ZR_RECENT)&@lf)
        
    EndIf   
Next
Edited by jben

Share this post


Link to post
Share on other sites

What i'm trying to achieve is something like this

$OUTPUT = loop
a - 29944M_10386\
b - 53464M_10286\
c - 00002M_10376\

And the loop goes through and then the code below can call in the information from the loop.I just thought this might be the best way forward?

;------------------------------------------------------------------------------------------
; RENAMES CHILD ZIP
;------------------------------------------------------------------------------------------

$F_Array = __FldrListToArray("C:\EXTRCT_TEMP\ & $OUTPUT", 0);the rest of this string is detected in the code below.
For $I = 1 To Ubound($F_Array) -1
$targetDir = $F_Array[$I];this code states that the targetdir is the information provided via the $F_Array
;the purpose in this is because the $F_Array obtains the folder within the 29944M_10386 folder to complete
;the string, which is then used in the code below to rename the .zip file.
$aZipList = _FileListToArray($targetDir,"*.zip",0)
    If IsArray($aZipList) Then
;MsgBox(0, "TEST PATH", $targetDir &$aZipList[1] & @CRLF & $targetDir &"DATA_CHILD.zip");JUST FOR TESTING PURPOSES
    FileMove($targetDir &$aZipList[1],$targetDir &"DATA_CHILD.zip")
    EndIf
Next

Share this post


Link to post
Share on other sites

Always make helper Function for repetitive code like _UnzipExtFiles($szZipFile, $ext, $szDestDir) in this case:

#include-once
#include<MyExtras\_ZipPlugin.au3>;this is required for unzipping on dev PC
;unless the Au3Zip.dll is in the system folder.Just leaving this here for security.
#include <File.au3>;this is required for checking files.
#include <MyExtras\folder.au3>;this is for checking folders.
$szPluginPath = @ScriptDir & "\Au3Zip.dll"
$hPluginDll = PluginOpen($szPluginPath)

If $hPluginDll = -1 Then
    MsgBox(266288, "Error:", "Plugin not initialized.")
    Exit
EndIf

;------------------------------------------------------------------------------------------
; INITIAL CHECKS BEFORE PROCEEDING
;------------------------------------------------------------------------------------------

If Not FileExists("C:\INPUT\*.zip") Then Exit

;------------------------------------------------------------------------------------------
; RENAMES PARENT ZIP
;------------------------------------------------------------------------------------------

;THIS CODE WILL RENAME THE ZIP FOLDER IN THE INPUT FOLDER TO A NAME THAT THE CODE WILL
;RECOGNIZE. THE PURPOSE IN THIS IS TO ENSURE THAT REGARDLESS OF THE ZIP FILE NAME, THE CODE
;WILL STILL EXTRACT THE FILES.ORIGINAL ZIP NAME IS IN THE FORMAT OF "WEEKLY UPDATE (DATE).ZIP"
;(DATE) IS UNIQUE FOR EACH UP ISSUE OF THE ZIP FILE, THE CODE BELOW RESOLVES THIS ISSUE.

$targetDir = "C:\INPUT\"
$aZipList = _FileListToArray($targetDir, "*.zip", 1);this returns files only as defined in the AutoIT help files.
If IsArray($aZipList) Then
    FileMove($targetDir & $aZipList[1], $targetDir & "DATA_PARENT.zip")
EndIf

;------------------------------------------------------------------------------------------
; EXTRACTS PARENT ZIP
;------------------------------------------------------------------------------------------

;THIS CODE WILL EXTRACT THE INFORMATION IN THE INPUT FOLDER, WHICH HAS BEEN RENAMED TO DATA_PARENT

_UnzipExtFiles("C:\INPUT\DATA_PARENT.zip", ".zip", "C:\EXTRCT_TEMP")

;------------------------------------------------------------------------------------------
; RENAMES CHILD ZIP
;------------------------------------------------------------------------------------------

;THIS CODE WILL SEARCH THE TEMP FOLDER FOR THE CHILD ZIP AND RENAME TO DATA_CHILD.ZIP
;THIS WILL ALSO ENSURE THAT REGARDLESS OF THE FILE (DATE) THAT THE FILE WILL STILL BE
;RECOGNIZED AND EXTRACTED.

$F_Array = __FldrListToArray("C:\EXTRCT_TEMP\29944M_10386\", 0);the rest of this string is detected in the code below.
For $I = 1 To UBound($F_Array) - 1
    $targetDir = $F_Array[$I];this code states that the targetdir is the information provided via the $F_Array
    ;the purpose in this is because the $F_Array obtains the folder within the 29944M_10386 folder to complete
    ;the string, which is then used in the code below to rename the .zip file.
    $aZipList = _FileListToArray($targetDir, "*.zip", 0)
    If IsArray($aZipList) Then
        ;MsgBox(0, "TEST PATH", $targetDir &$aZipList[1] & @CRLF & $targetDir &"DATA_CHILD.zip");JUST FOR TESTING PURPOSES
        FileMove($targetDir & $aZipList[1], $targetDir & "DATA_CHILD.zip")
    EndIf
Next


;------------------------------------------------------------------------------------------
; EXTRACTS CHILD ZIP .NCP & .LCN FILE
;------------------------------------------------------------------------------------------

;THIS CODE EXTRACTS THE .NCP and .LCN FILE(S) FROM THE SPECIFIED FOLDER

_UnzipExtFiles($targetDir & "DATA_CHILD.zip", ".ncp|.lcn", "C:\OUTPUT")

;------------------------------------------------------------------------------------------
; MOVES FILES TO APPROPRIATE LOCATION. INCLUDES ERROR CHECKING AND NOTIFICAITON
;------------------------------------------------------------------------------------------
#include <MyExtras\NotificationBar.au3>;this is a notification bar that is compiled during into setup once built


DirMove("C:\OUTPUT\MASTER\", "C:\OUTPUT\29944M_10386", 9);moves the .ncp file to the folder (creates folder if doesn't exist)
DirRemove("C:\OUTPUT\MASTER");deletes the original folder


;------------------------------------------------------------------------------------------
; CLEANUP
;------------------------------------------------------------------------------------------
;Cleanup procedure

DirRemove("C:\EXTRCT_TEMP", 1);Delete TEMP
FileDelete("C:\INPUT\*.zip");Deletes any zip files within the input folder

PluginClose($hPluginDll);closes the Au3Zip.dll plugin

; uznip all files of defined filetype from source ZIP file to dest directory
Func _UnzipExtFiles($szZipFile, $ext, $szDestDir)
    $ext = StringSplit($ext, '|') ; allow more than one extension, | as delimiter
        $aZipList = _ZipList2Array($szZipFile)
    For $x = 0 To UBound($aZipList) - 1
        $file_type = StringLower(StringRight($aZipList[$x][1],4))
                ; test if filetype match
        $found = 0
        For $y = 1 To UBound($ext)
            If $ext[$y] = $file_type Then
                $found = 1
                ExitLoop
            EndIf
        Next
                If $found Then ;this code extracts the file with the extensions specified here
            ConsoleWrite("Extracting:" & $aZipList[$x][1]);just shows Extracting in the AutoIT console at bottom for debugging purposes
            $vResult = _ZipUnZipItem($szZipFile, $aZipList[$x][1], $szDestDir)
            ConsoleWrite(" -- " & _ZipFormatMessage($ZR_RECENT) & @LF)
        EndIf
    Next
EndFunc

Share this post


Link to post
Share on other sites

I also found this:

I have error checked the plugin twice, rather un-neccesary.. Thanks everyone

;------------------------------------------------------------------------------------------

; EXTRACTS PARENT ZIP

;------------------------------------------------------------------------------------------

;THIS CODE WILL EXTRACT THE INFORMATION IN THE INPUT FOLDER, WHICH HAS BEEN RENAMED TO DATA_PARENT

$szZipFile = "C:\INPUT\DATA_PARENT.zip";this is the zip file to extract

$szDestDir = "C:\EXTRCT_TEMP";this is the destination to extract the zip files

If $hPluginDll = -1 Then

MsgBox(266288,"Error:","Plugin not initialized.")

Exit

Share this post


Link to post
Share on other sites

Anyways. Thanks everyone for your contribution to this post. Its really helped alot.

Have a good evening. ;-)

Share this post


Link to post
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
Sign in to follow this  
Followers 0