none
how to send multiple csv file to winzip as attachement RRS feed

  • Question

  • Hi

    I have multiple csv file and I want to send all of them as attachment in zip format.

    How can include all csv file in zip format.



    Monday, June 17, 2013 5:07 AM

Answers

  • Hi Raj,

    Below are the two zip functions which i use in my script to zip.

    1st function is to create a zip file and the second to add files to that zip folder.

    function New-Zip
    {
     param(
     [Parameter(Mandatory=$true,
      Position=0,
      ValueFromPipeline=$true)]
     [String]
     $Path,
     
     [Switch]
     $PassThru,
     
     [Switch]
     $Force
     )
     process {
       if (Test-Path $path)
       {
        if (-not $Force) { 
         return
        }
       }
       Set-Content $path ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
       $item = Get-Item $path
       $item.IsReadOnly = $false 
       if ($passThru) { $item } 
      
     }
    }
    Function Copy-ToZip
    {
      param(
      [Parameter(Mandatory=$true,
        Position=0,
        ValueFromPipelineByPropertyName=$true)]
      [Alias('FullName')]
      [String]$File,
      [Parameter(Mandatory=$true,Position=1)]
      [String]$ZipFile,
      
      [Switch]$HideProgress,
      [Switch]$Force
      )
      begin {
          $ShellApplication = New-Object -ComObject Shell.Application
          if (-not (Test-Path $ZipFile)) {
            New-Zip $ZipFile
          }
          $Path = Resolve-Path $ZipFile
          $ZipPackage =$ShellApplication.Namespace("$Path")
        
      }
      process {
          $RealFile = Get-Item $File
          if (-not $RealFile) { return }    
          if (-not $hideProgress) {
            $perc +=5 
            if ($perc -gt 100) { $perc = 0 } 
            Write-Progress "Copying to $ZipFile" $RealFile.FullName -PercentComplete $perc
          }
          $Flags = 0
          if ($force) {
            $flags = 16 -bor 1024 -bor 64 -bor 512
          } 
          Write-Verbose $realFile.Fullname
          $ZipPackage.CopyHere($realFile.Fullname, $flags)
          Start-Sleep -Milliseconds 500
        
      }
    }

    New-Zip  "filelocation and name"
    copy-tozip -file "filetoadd" -zipfile "above zip file name and location"


    Not sure about the source of this code. Obviously the credit goes to the one who created it.


    Thanks Azam When you see answers please Mark as Answer if Helpful..vote as helpful.

    Monday, June 17, 2013 8:49 AM