Mittwoch, 11. Juli 2012

Einfaches arbeiten mit ZIP Archiven / Working with ZIP files the easy way

Das Shell.Application Objekt kennt unter anderem eine Methode namens 'CopyHere'. Diese auf den ersten Blick recht harmlos erscheinende Methode beinhaltet jedoch auch das Verarbeiten von ZIP Archiven.

Wenn wir komplette Verzeichnisse archivieren bzw. wiederherstellen wollen, dann ist dies mit CopyHere auf relative einfache Weise möglich.

Das heutige Code Beispiel besteht aus zwei kleine Routinen zum Packen und Entpacken von Verzeichnissen unter Nutzung von CopyHere:

Among other things the Shell.Application object has a method called 'CopyHere'. At first glance, it appears to be rather harmless. However it contains complete processing of ZIP archives.

So, as long as we only compute complete directories, CopyHere offers a rather easy way of doing this.

Today's code example contains two small routines for un-/packing directories via CopyHere.

ZIP
CLEAR

lcFile    = [D:\Archive\myZipArchive.zip]
lcDir    = [D:\TEMP]
?
?Zip2Archive( lcDir , lcFile )

FUNCTION zip2Archive as Boolean
     LPARAMETERS vSourceDir as String, vZipFile as String
    DECLARE Sleep IN WIN32API INTEGER
    LOCAL    llReturn as Boolean, liOption as Integer, ;
            loShellObj as Object, loInputObj as Object, loOutputObj as Object, loFile as Object
    llReturn = .F.
    loShellObj = CreateObject( [Shell.Application] )
    IF TYPE( [loShellObj] ) = [O]
        IF NOT FILE( vZipFile )
            STRTOFILE( [] , vZipFile )
        ENDIF 
        loInputObj    = loShellObj.NameSpace( vSourceDir )
        loOutputObj    = loShellObj.NameSpace( vZipFile )
        liOption    = 4 && Do not display a progress dialog box 
        IF TYPE( [loInputObj] ) = [O] AND TYPE( [loOutputObj] ) = [O]
            TRY 
                FOR EACH loFile IN loInputobj.Items
                    loTargetObjExists = loOutputObj.ParseName( loFile.Name )
                    loSourceObjExists = loInputObj.ParseName( loFile.Name )
                    IF ISNULL( loTargetObjExists )
                        loOutputObj.CopyHere( loFile , liOption )
                        SLEEP( 200 )
                    ELSE  
                        IF loOutputObj.ParseName( loFile.Name ).ModifyDate < loFile.ModifyDate
                            ?loFile.Name + [ kann aktualisiert werden]
                        ELSE 
                            ?loFile.Name + [ ist aktuell]
                        ENDIF 
                    ENDIF 
                ENDFOR 
                llReturn = .T.
            CATCH 
                * Place messagebox here
            ENDTRY
        ENDIF 
    ENDIF
    RELEASE loInputObj, loOutputObj, loShellObj, loFile
    RETURN llReturn
ENDFUNC 
Paste your text here.

UNZIP
lcFile    = [D:\Archive\myZipFile.zip]
lcDir    = [D:\TEMP]
UnzipArchive( lcFile , lcDir )

FUNCTION UnzipArchive as Boolean
    LPARAMETERS vZipFile as String, vTargetDir as String
SET STEP ON 
    LOCAL    llReturn as Boolean, liOption as Integer, ;
            loShellObj as Object, loInputObj as Object, loOutputObj as Object
    llReturn = .F.
    
    loShellObj = CreateObject( [Shell.Application] )
    IF TYPE( [loShellObj] ) = [O]
        loOutputObj    = loShellObj.NameSpace( vTargetDir )
        loInputObj    = loShellObj.NameSpace( vZipFile )
        liOption    = 4 && Do not display a progress dialog box.
        IF TYPE( [loInputObj] ) = [O] AND TYPE( [loOutputObj] ) = [O]
            TRY 
                loOutputObj.CopyHere( loInputObj.Items , liOption )
                llReturn = .T.
            CATCH 
                * Place messagebox here
            ENDTRY
        ENDIF 
    ENDIF
    RELEASE loInputObj, loOutputObj, loShellObj 
    RETURN llReturn
ENDFUNC 

Der oben stehende Code kann natürlich auf die Verarbeitung bestimmter Dateien/Dateitypen umgestellt bzw. erweitert werden.
Above code can easily be modified to work with specific files/filetypes.

Weiterführende Infos zur Arbeit mit Verzeichnissen und Archiven gibt es hier:
Additional Infos about working with directories and archives can be found here:

http://technet.microsoft.com/en-us/library/ee176625

Montag, 7. Mai 2012

Datumsspielereien (Teil 9) / Date gadgets (Part 9)

Irgendwie sind Datumberechnungen ein Faß ohne Boden. Nach Teil 4 der Datumsspielereien war ich bereits der Meinung, nichts neues mehr zu diesem Thema zu finden. Aber irgendwie poppt in unregelmäßigen Abständen immer wieder eine neue Frage dazu auf.

Aktuell dreht es sich darum herauszufinden, wieviele Jahre, Monate und Tage zwischen zwei Datumswerten liegen.

Im folgenden Mustercode erledigt diese Aufgabe eine kleine Schleife die zunächst die Jahre, danach die Monate und letztendlich die Tage aufaddiert und uns so den genauen Zeitraum übermittelt.

Somehow, date calculations are a bottomless pit. With Part 4 of my date gadgets I was sure to find nothing new on this subject. But somehow every now and then, a new question pops up.

This post is about finding out the period between two dates.

The following sample code does this by using a loop that first adds years, then months and finally the days to give us the exact time between the given date values.


LOCAL ldDate1 AS Date, ldDate2 AS Date, liYear AS Integer, liMonth AS Integer, liDay AS Integer, llStatus as Boolean
ldDate1 = {28.8.1961}
ldDate2 = DATE()
STORE 0 TO liYear, liMonth, liDay

llStatus = GetPeriodBetweenDates( ldDate1 , ldDate2 , @liYear , @liMonth , @liDay )

CLEAR 
IF llStatus = .T.
    ? [Zeitraum zwischen ] + DTOC( ldDate1 ) + [ und ] + DTOC( ldDate2 )
    ?  TRANSFORM( liYear )  + [ Jahr]  + IIF( liYear  <> 1 , [e ] , [ ] )
    ?? TRANSFORM( liMonth ) + [ Monat] + IIF( liMonth <> 1 , [e ] , [ ] )
    ?? TRANSFORM( liDay )   + [ Tag]   + IIF( liDay   <> 1 , [e ] , [ ] )
ENDIF 

FUNCTION GetPeriodBetweenDates as Boolean

    * // Parameter 3-5 als Referenz übergeben!
    * // Parameters 3-5 by reference
    LPARAMETERS vDate1 as Date, vDate2 as Date, vYear as Integer, vMonth as Integer, vDay as Integer

    LOCAL ldDate1 as Date, ldDate2 as Date
    ldDate1 = IIF( vDate1 > vDate2 , vDate2 , vDate1 )
    ldDate2 = IIF( vDate1 > vDate2 , vDate1 , vDate2 )

    STORE 0 TO vYear , vMonth , vDay

    DO WHILE ldDate1 < ldDate2
        DO CASE 
        CASE GOMONTH( ldDate1 , 12 ) < ldDate2
            vYear = vYear + 1 
            ldDate1 = GOMONTH( ldDate1 , 12 )
        CASE GOMONTH( ldDate1 , 1 ) < ldDate2
            vMonth = vMonth + 1 
            ldDate1 = GOMONTH( ldDate1 , 1 )
        OTHERWISE 
            vDay = vDay + 1 
            ldDate1 = ldDate1 + 1 
        ENDCASE 
    ENDDO 

    RETURN .T.

ENDFUNC