title: Compress files in folder to zip file by using PS
body: <p>I have the following scripts to compress a folder (all files in the folder) to a zip file:</p>
  
  <pre><code> set-content $zipFileName ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 
   $ZipFile = (new-object -com shell.application).NameSpace($zipFileName) 
   Get-ChildItem $folder | foreach {$zipFile.CopyHere($_.fullname)} 
  </code></pre>
  
  <p>where $folder = "C:\Test", and $zipFileName = "C:\data\test.zip" as example</p>
  
  <p>It works fine if "C:\Test" contains no empty sub-folders, and it seems works recursively to compress all files within sub-folders. I really like above simple line script. For example:</p>
  
  <pre><code>C:\Test
     file1.dat
     file2.dat
     Test-Sub
        File21.bat
        ....
  </code></pre>
  
  <p>However, I got error in one case. I find that if there is any empty folder such as "C:\Test\EmptySub", </p>
  
  <pre><code>C:\Test
     file1.dat
     file2.dat
     Test-Sub
        File21.bat
        ....
     EmptySub
     AnotherSub
        file31.sp1
        ...
  </code></pre>
  
  <p>the script will generate an error.  I tried the following scripts:</p>
  
  <pre><code>Get-ChildItem $files -Recurse | foreach { if (!$_.PSIsContainer) 
      {$zipFile.CopyHere($_.fullname)}} 
  </code></pre>
  
  <p>This does not work as expected. It just skips all the sub-folders. Not sure if there are  filter or clause available to skip all the empty sub-folders?</p>
  
  <p><strong>Updated</strong>: Based on suggests, I gave it a try. My problem has not be resolved. Here is the update of my question.  First, I updated the scripts above to show how $zipFile object is created. Secondly I have the suggested codes:</p>
  
  <pre><code>Get-ChildItem $files | ? {-not ($_.PSIsContainer -eq $True -and 
    $_.GetFiles().Count -eq 0) } | % {$zipfile.CopyHere($_.fullname)}
  </code></pre>
  
  <p>I tried above updates on my WindowsXP, it works fine with empty sub-folders. However, the same codes do not workin in Windows 2003 Server. The following is the error message:</p>
  
  <p>[Window Title]
  Compressed (zipped) Folders Error</p>
  
  <p>[Content]
  File not found or no read permission.</p>
  
  <p>[OK] </p>
  
  <p>Not sure if this type PK object works in Windows 2003 server, or if there is other settings for the object.</p>