Windows Vista Forums

powershell and 7zip
  1. #1


    David Sherman Guest

    powershell and 7zip

    Is there some way to use powershell and 7 zip to (1) zip up folders on
    a daily basis and use the folder name and time and date stamp in the
    name of the zio and (2) limit the number of zip files to some number?\



    thanks

    PS. I am not a programmer.

    thanks again

      My System SpecsSystem Spec

  2. #2


    Vadims Podans Guest

    Re: powershell and 7zip

    1) 7zip here is not neccessary. You can use ZipFolders as described here:
    http://blogs.inetium.com/blogs/mhodn...08/07/295.aspx

    1.5) to use the folder name and time and date stamp in the name of the zip:
    $datex = get-date -f dd.MM.yy_hh.mm.
    $Outputfilename = $_.name + $datex + '.zip'

    2)
    $numberofarchives = 10
    if ((dir c:\archives).count -gt $numberofarchives) {
    dir c:\archives | sort lastwritetime | select -first $(dir
    c:\archives).count - $numberofarchives) | del -force
    }

    --
    WBR, Vadims Podans
    PowerShell blog - www.sysadmins.lv

    "David Sherman" <dshermin@xxxxxx> rakstīja ziņojumā
    "news:ucinr4l7iju8ff8htdi8v8a40kbchj65de@xxxxxx"...

    > Is there some way to use powershell and 7 zip to (1) zip up folders on
    > a daily basis and use the folder name and time and date stamp in the
    > name of the zio and (2) limit the number of zip files to some number?\
    >
    > thanks
    >
    > PS. I am not a programmer.
    >
    > thanks again

      My System SpecsSystem Spec

  3. #3


    David Sherman Guest

    Re: powershell and 7zip

    On Sat, 14 Mar 2009 19:22:00 +0200, "Vadims Podans" <vpodans> wrote:
    Since I am not a programmer:

    how do I structure the file.

    # Part 1

    ########################################################
    #
    # out-zip.ps1
    #
    # Usage:
    #
    # To zip up some files:
    # ls c:\source\*.txt | out-zip c:\target\archive.zip $_
    #
    # To zip up a folder:
    # gi c:\source | out-zip c:\target\archive.zip $_
    ########################################################

    $path = $args[0]
    $files = $input

    if (-not $path.EndsWith('.zip')) {$path += '.zip'}

    if (-not (test-path $path)) {
    set-content $path ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
    }

    $ZipFile = (new-object -com shell.application).NameSpace($path)
    $files | foreach {$zipfile.CopyHere($_.fullname)}

    # then

    $datex = get-date -f dd.MM.yy_hh.mm.
    $Outputfilename = $_.name + $datex + '.zip'

    # and then

    $numberofarchives = 10
    if ((dir c:\archives).count -gt $numberofarchives) {
    dir c:\archives | sort lastwritetime | select -first $(dir
    c:\archives).count - $numberofarchives) | del -force
    }

    and where do I place my folders?

    thanks

      My System SpecsSystem Spec

  4. #4


    Vadims Podans Guest

    Re: powershell and 7zip

    take it:
    function Out-ZIP ([string]$archive) {
    Process {
    # get current date and time in specified format
    $datex = get-date -Format dd.MM.yy_hh.mm
    # create archive file name structure that includes
    # folder name, date/time and .zip extension
    $name = $archive + "$_.name" + $datex + '.zip'
    # if not exist, make new ZIP file
    if (-not (test-path $name)) {
    set-content $name ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
    }
    # create COM object and set to it archive name
    $ZipFile = (new-object -com shell.application).NameSpace($name)
    # receive from pipeline each folder object and compress it
    $_ | % {$zipfile.CopyHere($_.fullname)}
    }
    End {
    # set how much archives will be stored. Here is 10 days
    $date = (Get-Date).AddDays(-10)
    # check archive folder for archives that are older than
    # $date days and remove them.
    dir ($archive + "*.zip") | ?{$_.lastwritetime -le $date} | del -Force
    }
    }

    I hope this helps you!
    --
    WBR, Vadims Podans
    PowerShell blog - www.sysadmins.lv

    "David Sherman" <dshermin@xxxxxx> rakstīja ziņojumā
    "news:cernr41iila0k4t69dc6u675s04cr9qbgl@xxxxxx"...

    > On Sat, 14 Mar 2009 19:22:00 +0200, "Vadims Podans" <vpodans> wrote:
    > Since I am not a programmer:
    >
    > how do I structure the file.
    >
    > # Part 1
    >
    > ########################################################
    > #
    > # out-zip.ps1
    > #
    > # Usage:
    > #
    > # To zip up some files:
    > # ls c:\source\*.txt | out-zip c:\target\archive.zip $_
    > #
    > # To zip up a folder:
    > # gi c:\source | out-zip c:\target\archive.zip $_
    > ########################################################
    >
    > $path = $args[0]
    > $files = $input
    >
    > if (-not $path.EndsWith('.zip')) {$path += '.zip'}
    >
    > if (-not (test-path $path)) {
    > set-content $path ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
    > }
    >
    > $ZipFile = (new-object -com shell.application).NameSpace($path)
    > $files | foreach {$zipfile.CopyHere($_.fullname)}
    >
    > # then
    >
    > $datex = get-date -f dd.MM.yy_hh.mm.
    > $Outputfilename = $_.name + $datex + '.zip'
    >
    > # and then
    >
    > $numberofarchives = 10
    > if ((dir c:\archives).count -gt $numberofarchives) {
    > dir c:\archives | sort lastwritetime | select -first $(dir
    > c:\archives).count - $numberofarchives) | del -force
    > }
    >
    > and where do I place my folders?
    >
    > thanks

      My System SpecsSystem Spec

  5. #5


    Vadims Podans Guest

    Re: powershell and 7zip

    emm..would be better if archive folder will exist before running this
    script.

    --
    WBR, Vadims Podans
    PowerShell blog - www.sysadmins.lv

    "Vadims Podans" <vpodans> rakstīja ziņojumā
    "news:OVKDyVOpJHA.5832@xxxxxx"...

    > take it:
    > function Out-ZIP ([string]$archive) {
    > Process {
    > # get current date and time in specified format
    > $datex = get-date -Format dd.MM.yy_hh.mm
    > # create archive file name structure that includes
    > # folder name, date/time and .zip extension
    > $name = $archive + "$_.name" + $datex + '.zip'
    > # if not exist, make new ZIP file
    > if (-not (test-path $name)) {
    > set-content $name ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
    > }
    > # create COM object and set to it archive name
    > $ZipFile = (new-object -com shell.application).NameSpace($name)
    > # receive from pipeline each folder object and compress it
    > $_ | % {$zipfile.CopyHere($_.fullname)}
    > }
    > End {
    > # set how much archives will be stored. Here is 10 days
    > $date = (Get-Date).AddDays(-10)
    > # check archive folder for archives that are older than
    > # $date days and remove them.
    > dir ($archive + "*.zip") | ?{$_.lastwritetime -le $date} | del -Force
    > }
    > }
    >
    > I hope this helps you!
    > --
    > WBR, Vadims Podans
    > PowerShell blog - www.sysadmins.lv
    >
    > "David Sherman" <dshermin@xxxxxx> rakstīja ziņojumā
    > "news:cernr41iila0k4t69dc6u675s04cr9qbgl@xxxxxx"...

    >> On Sat, 14 Mar 2009 19:22:00 +0200, "Vadims Podans" <vpodans> wrote:
    >> Since I am not a programmer:
    >>
    >> how do I structure the file.
    >>
    >> # Part 1
    >>
    >> ########################################################
    >> #
    >> # out-zip.ps1
    >> #
    >> # Usage:
    >> #
    >> # To zip up some files:
    >> # ls c:\source\*.txt | out-zip c:\target\archive.zip $_
    >> #
    >> # To zip up a folder:
    >> # gi c:\source | out-zip c:\target\archive.zip $_
    >> ########################################################
    >>
    >> $path = $args[0]
    >> $files = $input
    >>
    >> if (-not $path.EndsWith('.zip')) {$path += '.zip'}
    >>
    >> if (-not (test-path $path)) {
    >> set-content $path ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
    >> }
    >>
    >> $ZipFile = (new-object -com shell.application).NameSpace($path)
    >> $files | foreach {$zipfile.CopyHere($_.fullname)}
    >>
    >> # then
    >>
    >> $datex = get-date -f dd.MM.yy_hh.mm.
    >> $Outputfilename = $_.name + $datex + '.zip'
    >>
    >> # and then
    >>
    >> $numberofarchives = 10
    >> if ((dir c:\archives).count -gt $numberofarchives) {
    >> dir c:\archives | sort lastwritetime | select -first $(dir
    >> c:\archives).count - $numberofarchives) | del -force
    >> }
    >>
    >> and where do I place my folders?
    >>
    >> thanks
    >

      My System SpecsSystem Spec

  6. #6


    Vadims Podans Guest

    Re: powershell and 7zip

    sorry, I forget the usage:

    dir RootFolderToArchive | ?{$_.psiscontainer} | out-zip ArchiveFolderPath

    for example:
    dir D:\users | ?{$_.psiscontainer} | out-zip E:\UsersBackup\

    --
    WBR, Vadims Podans
    PowerShell blog - www.sysadmins.lv

    "Vadims Podans" <vpodans> rakstīja ziņojumā
    "news:OVKDyVOpJHA.5832@xxxxxx"...

    > take it:
    > function Out-ZIP ([string]$archive) {
    > Process {
    > # get current date and time in specified format
    > $datex = get-date -Format dd.MM.yy_hh.mm
    > # create archive file name structure that includes
    > # folder name, date/time and .zip extension
    > $name = $archive + "$_.name" + $datex + '.zip'
    > # if not exist, make new ZIP file
    > if (-not (test-path $name)) {
    > set-content $name ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
    > }
    > # create COM object and set to it archive name
    > $ZipFile = (new-object -com shell.application).NameSpace($name)
    > # receive from pipeline each folder object and compress it
    > $_ | % {$zipfile.CopyHere($_.fullname)}
    > }
    > End {
    > # set how much archives will be stored. Here is 10 days
    > $date = (Get-Date).AddDays(-10)
    > # check archive folder for archives that are older than
    > # $date days and remove them.
    > dir ($archive + "*.zip") | ?{$_.lastwritetime -le $date} | del -Force
    > }
    > }
    >
    > I hope this helps you!
    > --
    > WBR, Vadims Podans
    > PowerShell blog - www.sysadmins.lv
    >
    > "David Sherman" <dshermin@xxxxxx> rakstīja ziņojumā
    > "news:cernr41iila0k4t69dc6u675s04cr9qbgl@xxxxxx"...

    >> On Sat, 14 Mar 2009 19:22:00 +0200, "Vadims Podans" <vpodans> wrote:
    >> Since I am not a programmer:
    >>
    >> how do I structure the file.
    >>
    >> # Part 1
    >>
    >> ########################################################
    >> #
    >> # out-zip.ps1
    >> #
    >> # Usage:
    >> #
    >> # To zip up some files:
    >> # ls c:\source\*.txt | out-zip c:\target\archive.zip $_
    >> #
    >> # To zip up a folder:
    >> # gi c:\source | out-zip c:\target\archive.zip $_
    >> ########################################################
    >>
    >> $path = $args[0]
    >> $files = $input
    >>
    >> if (-not $path.EndsWith('.zip')) {$path += '.zip'}
    >>
    >> if (-not (test-path $path)) {
    >> set-content $path ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
    >> }
    >>
    >> $ZipFile = (new-object -com shell.application).NameSpace($path)
    >> $files | foreach {$zipfile.CopyHere($_.fullname)}
    >>
    >> # then
    >>
    >> $datex = get-date -f dd.MM.yy_hh.mm.
    >> $Outputfilename = $_.name + $datex + '.zip'
    >>
    >> # and then
    >>
    >> $numberofarchives = 10
    >> if ((dir c:\archives).count -gt $numberofarchives) {
    >> dir c:\archives | sort lastwritetime | select -first $(dir
    >> c:\archives).count - $numberofarchives) | del -force
    >> }
    >>
    >> and where do I place my folders?
    >>
    >> thanks
    >

      My System SpecsSystem Spec

  7. #7


    David Sherman Guest

    Re: powershell and 7zip

    Thanks for your help.

    On Sat, 14 Mar 2009 22:59:45 +0200, "Vadims Podans" <vpodans> wrote:

    >sorry, I forget the usage:
    >
    >dir RootFolderToArchive | ?{$_.psiscontainer} | out-zip ArchiveFolderPath
    >
    >for example:
    >dir D:\users | ?{$_.psiscontainer} | out-zip E:\UsersBackup\

      My System SpecsSystem Spec

  8. #8


    Alex K. Angelopoulos Guest

    Re: powershell and 7zip

    By the way, if your real underlying problem with this was PowerShell not
    recognizing 7z.exe as a command, use a backtick (`) before the name, like
    this:

    `7z.exe

    The issue here is that 7zip uses a dodgy naming practice on Windows systems.

    "David Sherman" <dshermin@xxxxxx> wrote in message
    news:ucinr4l7iju8ff8htdi8v8a40kbchj65de@xxxxxx

    > Is there some way to use powershell and 7 zip to (1) zip up folders on
    > a daily basis and use the folder name and time and date stamp in the
    > name of the zio and (2) limit the number of zip files to some number?\
    >
    > thanks
    >
    > PS. I am not a programmer.
    >
    > thanks again

      My System SpecsSystem Spec

powershell and 7zip problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
7zip tries to install on boot, cannot find .msi file - help! :D abo999 General Discussion 3 20 Oct 2009
Solved THIS 7Zip file mgmnt WILL NOT CRASH pacinitaly General Discussion 0 07 Oct 2009
7zip & media player classic problems IvanI Software 3 15 Nov 2008
Automatic PowerShell Error Parsing in PowerShell Analyzer and PowerShellPlus Karl Prosser[MVP] PowerShell 0 14 Nov 2007
PowerShell Leaders Join Forces and offer a pre-release version of PowerShell for 50% off the retail value klumsy@xtra.co.nz PowerShell 0 19 Jun 2007