任何人都可以创建一个接受两个输入的vbs代码,第一个是文件扩展名,第二个是zip文件名

时间:2016-07-25 01:11:16

标签: vbscript

任何人都可以创建一个接受两个输入的vbs代码,第一个是文件扩展名,第二个是zip文件名。程序使用给定的扩展名压缩文件,并将它们放在带有给定输入的压缩文件中。

示例输入:

输入文件类型:txt

输入文件名:压缩

vbs会将文件夹中的所有txt文件压缩为compress.zip

注意:我不是在学习vbs;我没有任何背景。我们的教授在向我们介绍vbs 5分钟后给了我们这个问题,他希望我们在三天内提交。所以请理解我的情况。

1 个答案:

答案 0 :(得分:0)

@ sync2018:请根据您的要求找到下面的工作代码,如果需要进一步修改,请对其进行微调。如果需要任何进一步的帮助,请告诉我

注意:这是一个有效的代码,可能存在不需要的变量或操作

WorkingDir = "Add the path of the directory where the files are present should be zipped" for eg.,"C:\Users\U465205\Desktop\alpa_code\"
zipfile = "Give the path where the zip file should be saved with the file name as well ends with zip extension" for eg.,"C:\Users\U465205\Desktop\alpa_code\mulla.zip"      
Extension = ".csv"
Dim fso, myFolder, fileColl, aFile, FileName, SaveName
Dim objExcel, objWorkbook
Set fso = CreateObject("Scripting.FilesystemObject")
savedir=WorkingDir&"New Folder"
If Not fso.FolderExists(savedir&"\") Then
  fso.CreateFolder savedir&"\"
End If
Set myFolder = fso.GetFolder(WorkingDir)
Set fileColl = myFolder.Files
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts = False
For Each aFile In fileColl
ext = Right(aFile.Name,Len(Extension)) 
If UCase(ext) = UCase(extension) Then
fso.CopyFile aFile , savedir &"\"
End If
Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.OpenTextFile(zipfile, 2, True).Write "PK" & Chr(5) & Chr(6) & String(18, Chr(0))
Set ShellApp = CreateObject("Shell.Application")
Set zip = ShellApp.NameSpace(zipfile)
zip.CopyHere savedir
WScript.Sleep 10000
Set objWorkbook = Nothing
Set objExcel = Nothing
Set myFolder = Nothing
Set fileColl = Nothing
Set fso = Nothing
set objFolder = nothing
set objShell = nothing
Set ts = nothing