DevAdmin Blog

Blog di Ermanno Goletto (Microsoft MVP Directory Services - MCITP - MCTS - MCSA - MCP)
posts - 887, comments - 447, trackbacks - 13

My Links

News

Avatar

Curriculum Vitae

Visualizza il profilo di Ermanno Goletto su LinkedIn


Il contenuto di questo blog e di ciascun post viene fornito “così come é”, senza garanzie, e non conferisce alcun diritto. Questo blog riporta il mio personale pensiero che non riflette necessariamente il pensiero del mio datore di lavoro.

Logo Creative Commons Deed


Logo SysAdmin.it SysAdmin.it Staff


Logo TechNet Forum TechNet Italia @ForumTechNetIt Follow TechNet Forum on Twitter


Logo MVP


Ermanno Goletto Follow ermannog on Twitter

Article Categories

Archives

Post Categories

Blogs

Friends

Knowledge Base

MVP Sites

Resources

Script per comprimere un file

A volte in uno script amministrativo può essere necessario dover comprimere un file e in tal caso una possibilità è quella di usare le cartelle comprese presenti a partire da XP.

[Update] Si tenga conto che Le cartelle compresse di XP hanno il limite di 4 GB

Option Explicit

Dim strScriptFullName, strCurrentPath, strZipFilePath, strAddFilePath

strScriptFullName = wscript.scriptfullname
strCurrentPath = Left(strscriptfullname, InStrRev(strScriptFullName, "\"))
strZipFilePath = strCurrentPath & "test.zip"
strAddFilePath = strCurrentPath & "test.txt"

'Crea un file zip vuoto
If CreateEmptyZip(strZipFilePath) Then

  'Aggiunge un file all'archivio zip
  Call AddFile2Zip(strZipFilePath, strAddFilePath)

End If

Function CreateEmptyZip(strZipFilePath)
  On Error Resume Next

  'Apertura file in scrittura
  Dim objFso, objFile
  Const ForWriting = 2
  Set objFso = CreateObject("Scripting.FileSystemObject")
  Set objFile = objFso.OpenTextFile(strZipFilePath, ForWriting, True)

  If Err = 0 Then
    objFile.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
  End If

  If Err = 0 Then
    objFile.Close
  End If

  Set objFso = Nothing
  Set objFile = Nothing 

  If Err = 0 Then
    CreateEmptyZip=True
  Else
    Err.Clear
    CreateEmptyZip=False
  End If
End Function

Function AddFile2Zip(strZipFilePath, strAddFilePath)
  'On Error Resume Next

  Dim objApp, objFolder
  Set objApp = createobject("Shell.Application")
  Set objFolder = objApp.NameSpace(strZipFilePath)

  If Err = 0 Then
    Call objFolder.CopyHere(strAddFilePath)

    'Le opzioni di CopyHere sembrano non avere effetto
    'per operazioni su cartelle compresse

    'Pausa per consentire l'avvio del processo di compressione
    'in quanto il processo verrà avviato quando quando il
    'processo dello script verrà messo in idle se il
    'processo dello script termina prima il processo
    'di compressione non viene avviato.
    'Ciò accade perchè CopyHere è asincrona
     wscript.Sleep 500
  End If

  Set objFolder = Nothing
  Set objApp = Nothing

  If Err = 0 Then
    AddFile2Zip = True
  Else
    Err.Clear
    AddFile2Zip = False
  End If
End Function

Print | posted on Thursday, January 31, 2008 2:11 PM | Filed Under [ Code, Snippets & Scripts IT ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 2 and 4 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET