windows本身支持直接zip格式压缩文件,也支持创建zip文件,但是并没有提供命令行工具?今天需要,又不想下载第三方的zip工具,于是查询了一下,发现有个 zip.vbs可以实现这个功能,又创建了个cmd文件,可以批量压缩。 注意: 下面第一段代码为 zipit.cmd 文件,用于批量调用 zip.vbs ,而下面的 zip.vbs包含了删除代码,可以直接删除压缩后的文件。 如果不需要删除功能,请自行注释掉删除部分的代码就可以了。 网传的代码存在bug,或者可能我不会用,所以修改了一下,一般zip的时候估计也都是操作当前目录,所以采用了获取当前目录的方法替换掉了失效的代码,所以用这个代码压缩,需要和被压缩文件保持在同一个目录下。
@echo off
echo zipit.cmd
for %%f in (*.sql) do zip %%f
zip.vbs
Set objArgs = WScript.Arguments
REM 开始压缩文件,默认压缩文件名跟输入文件同名
For Each s In objArgs
Zip s
Next
' Msgbox "OK"
Sub Zip(ByVal mySourceDir)
REM 根据是文件还是目录走不同的流程
Set fso = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
' Msgbox mySourceDir & fso.FileExists(mySourceDir)
If fso.FolderExists(mySourceDir) Then
REM 压缩目录
Set objSource = objShell.NameSpace(mySourceDir)
Set objFolderItem = objSource.Items()
REM 生成要生成的ZIP文件名
ZipFile =mySourceDir&".zip"
ElseIf fso.FileExists(mySourceDir) Then
REM 压缩文件
FileName = fso.GetFileName(mySourceDir)
' FolderPath = Left(mySourceDir, Len(mySourceDir) - Len(FileName))
FolderPath = createobject("Scripting.FileSystemObject").GetFolder(".").Path & "\"
Set objSource = objShell.NameSpace(FolderPath)
Set objFolderItem = objSource.ParseName(FileName)
' Msgbox FolderPath
' if isNull(FolderPath) Then
' set FolderPath = "."
' end if
' if isNull(objFolderItem) Then
' set objFolderItem = "."
' end if
REM 获取输入的文件名后辍
arr = Split(mySourceDir,".")
FileExt = arr(Ubound(arr))
REM 生成要生成的ZIP文件名
ZipFile = mySourceDir & ".zip"
' Msgbox ZipFile
End If
' Msgbox ZipFile
Set f = fso.CreateTextFile(ZipFile, True)
f.Write "PK" & Chr(5) & Chr(6) & String(18, Chr(0))
f.Close
Set objShell = CreateObject("Shell.Application")
FileName = fso.GetFileName(ZipFile)
' FolderPath = Left(ZipFile, Len(ZipFile) - Len(FileName))
FolderPath = createobject("Scripting.FileSystemObject").GetFolder(".").Path & "\"
Set objTarget = objShell.NameSpace(FolderPath & FileName)
' Wscript.echo FolderPath
' .Msgbox objTarget
' Set objTarget = objShell.NameSpace(ZipFile)
' Msgbox objTarget
' Msgbox fso.FileExists(ZipFile)
objTarget.CopyHere objFolderItem , 256
Do
REM 必须的,如果文件比较大,这个数字还要上调
WScript.Sleep 10000
Loop Until objTarget.Items.Count > 0
REM 销毁变量
' Set oFSO=CreateObject(Scripting.FileSystemObject)
Set f1=fso.GetFile(mySourceDir)
fso.DeleteFile f1
Set objShell = Nothing
Set objFolderItem = Nothing
Set objSource = Nothing
Set fso = Nothing
End Sub
@echo off
echo zipit.cmd
for %%f in (*.sql) do zip %%f
zip.vbs
Set objArgs = WScript.Arguments
REM 开始压缩文件,默认压缩文件名跟输入文件同名
For Each s In objArgs
Zip s
Next
' Msgbox "OK"
Sub Zip(ByVal mySourceDir)
REM 根据是文件还是目录走不同的流程
Set fso = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
' Msgbox mySourceDir & fso.FileExists(mySourceDir)
If fso.FolderExists(mySourceDir) Then
REM 压缩目录
Set objSource = objShell.NameSpace(mySourceDir)
Set objFolderItem = objSource.Items()
REM 生成要生成的ZIP文件名
ZipFile =mySourceDir&".zip"
ElseIf fso.FileExists(mySourceDir) Then
REM 压缩文件
FileName = fso.GetFileName(mySourceDir)
' FolderPath = Left(mySourceDir, Len(mySourceDir) - Len(FileName))
FolderPath = createobject("Scripting.FileSystemObject").GetFolder(".").Path & "\"
Set objSource = objShell.NameSpace(FolderPath)
Set objFolderItem = objSource.ParseName(FileName)
' Msgbox FolderPath
' if isNull(FolderPath) Then
' set FolderPath = "."
' end if
' if isNull(objFolderItem) Then
' set objFolderItem = "."
' end if
REM 获取输入的文件名后辍
arr = Split(mySourceDir,".")
FileExt = arr(Ubound(arr))
REM 生成要生成的ZIP文件名
ZipFile = mySourceDir & ".zip"
' Msgbox ZipFile
End If
' Msgbox ZipFile
Set f = fso.CreateTextFile(ZipFile, True)
f.Write "PK" & Chr(5) & Chr(6) & String(18, Chr(0))
f.Close
Set objShell = CreateObject("Shell.Application")
FileName = fso.GetFileName(ZipFile)
' FolderPath = Left(ZipFile, Len(ZipFile) - Len(FileName))
FolderPath = createobject("Scripting.FileSystemObject").GetFolder(".").Path & "\"
Set objTarget = objShell.NameSpace(FolderPath & FileName)
' Wscript.echo FolderPath
' .Msgbox objTarget
' Set objTarget = objShell.NameSpace(ZipFile)
' Msgbox objTarget
' Msgbox fso.FileExists(ZipFile)
objTarget.CopyHere objFolderItem , 256
Do
REM 必须的,如果文件比较大,这个数字还要上调
WScript.Sleep 10000
Loop Until objTarget.Items.Count > 0
REM 销毁变量
' Set oFSO=CreateObject(Scripting.FileSystemObject)
Set f1=fso.GetFile(mySourceDir)
fso.DeleteFile f1
Set objShell = Nothing
Set objFolderItem = Nothing
Set objSource = Nothing
Set fso = Nothing
End Sub