' MakeZIP.VBS V1.03 (C) yoshioka.teruo@nifty.com 2007-04-08
' MakeZIP.VBS makes ZIP file in command line.

Option Explicit
Const Usage="Usage: CScript.exe MakeZIP.VBS ZIPfile files..."
Dim fso
Dim ZIPfile
Dim Shell
Dim zFolder
Dim k
Dim Path
Dim FileName
Dim sFolderItem
Dim zFolderItem
Dim Count
Dim Ans

If WScript.Arguments.Count()<2 Then
  WScript.Echo Usage
  WScript.Quit
End If
ZIPfile=WScript.Arguments.Item(0)
Set fso=CreateObject("Scripting.FileSystemObject")
If UCase(fso.GetExtensionName(ZIPfile))<>"ZIP" Then
  WScript.Echo "Invalid Extension Name -",ZIPfile
  WScript.Quit
End If
If Not fso.FileExists(ZIPfile) Then
  fso.CreateTextFile(ZIPfile,False).Write "PK" & Chr(5) & Chr(6) & String(18,0)
End If
Set Shell=CreateObject("Shell.Application")
Set zFolder=Shell.NameSpace(fso.GetAbsolutePathName(ZIPfile))
For k=1 To WScript.Arguments.Count()-1
  Path=WScript.Arguments.Item(k)
  FileName=fso.GetFileName(Path)
  ' ----- Windows7/WindowsServer2008対応
	' Set sFolderItem=Shell.NameSpace(fso.GetAbsolutePathName(Path)&"\..\").ParseName(FileName)
	Set sFolderItem=Shell.NameSpace(fso.GetAbsolutePathName(fso.GetParentFolderName(Path))).ParseName(FileName)
	' -----

  If sFolderItem Is Nothing Then
    WScript.Echo Path,"- Not Found."
    Exit For
  End If
  Do
    Set zFolderItem=zFolder.ParseName(FileName)
    If zFolderItem Is Nothing Then
      Count=zFolder.Items().Count
      zFolder.CopyHere sFolderItem
      Do While zFolder.Items().Count=Count
        WScript.Sleep 1500
      Loop
      Exit Do
    Else
      'Ans=MsgBox("このフォルダには既に次のファイルが存在します:"&vbLf&vbLf&_
      '           """"&FileName&""""&vbLf&vbLf&"既存のファイルと置き換えますか?",_
      '           vbYesNoCancel+vbQuestion,"ファイル置換の確認")
      'Select Case Ans
      'Case vbYes
      '  zFolderItem.InvokeVerb("delete")
      'Case vbNo
        Exit Do
      'Case vbCancel
      '  Exit For
      'End Select
    End If
  Loop
Next
WScript.Quit
