【发布时间】:2022-04-02 15:03:50
【问题描述】:
这是我的第一篇文章,所以请原谅我的无知。我正在使用 vbscript 压缩特定文件夹中的所有 .csv 类型文件。经过一些谷歌搜索,我找到了一个可行的vbscript 来执行此操作,并启用了计划任务来自动执行此操作。
接下来我需要做的是通过 sftp 传输 zip 文件,然后将该 zip 文件“移动”到另一个文件夹中。我相信前者可以通过命令行使用 pscp.exe 来实现,但有人可以告诉我如何做后者吗?
基本上,压缩将每天进行两次,因此它的时间戳类似于yyyymmdd0900.zip(用于上午 9 点的时间表)和yyyymmdd1800.zip(用于下午 6 点的时间表)。传输后,我想将生成的 zip 文件移动(而不是复制)到另一个文件夹中。
任何指针将不胜感激。提前谢谢大家。
编辑:这是我根据一些 Google 搜索拼凑的一些代码。它做我想让它做的事情。请原谅“粘贴”,因为我不知道如何正确格式化它。目前,它在复制后运行 bat 文件,但我只是注意到我需要发送(使用 PuTTY Secure Copy)“最新”的 zip 文件,然后再将其移动到“完成”文件夹。有人可以告诉我怎么做吗?
压缩文件并重命名压缩文件
我的代码:
On Error Resume Next
strFilepath = "c:\files"
strDestination = "c:\files\completed\"
strExtension = "csv"
strYear = Year(Now)
strMonth = Right("0" & Month(Now), 2)
strDay = Right("0" & Day(Now), 2)
strHour = Right ("0" & Hour(Now), 2)
strMinute = Right ("0" & Minute (Now), 2)
strZip = strFilepath & "\" & strYear & strMonth & strDay & strHour & strMinute & ".zip"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFilepath)
For Each objFile in objFolder.Files
strFileExt = objFSO.GetExtensionName(objFile.Path)
If LCase(strFileExt) = LCase(strExtension) Then
ZipFile objFile.Path, strZip
End If
Next
Sub ZipFile(strFileToZip, strArchive)
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FileExists(strArchive) Then
Set objTxt = objFSO.CreateTextFile(strArchive)
objTxt.Write "PK" & Chr(5) & Chr(6) & String(18, Chr(0))
objTxt.Close
End If
Set objApp = CreateObject( "Shell.Application" )
intCount = objApp.NameSpace(strArchive).Items.Count + 1
objApp.NameSpace(strArchive).CopyHere strFileToZip
Do
WScript.Sleep 200
set objNameSpace = objApp.NameSpace(strArchive)
If Not objNameSpace is nothing Then
If objNameSpace.Items.Count = intCount Then
Exit Do
End If
End If
Loop
End Sub
>Move file to a different folder and then run a bat file to secury copy file to a FTP location
'Vars
Dim objFSO, objFileCopy, objFileDelete, dot, files, file
Dim strDestination, folder, subfolder, fileCount, strFilePath
'Strings
strDestination = "C:\Files\Completed\"
strFilePath = "C:\Files"
set objFSO = CreateObject("Scripting.fileSystemObject")
set folder = objFSO.getFolder(strFilePath)
For Each file In folder.files
Set objFileCopy = objFSO.GetFile(file)
If objFSO.GetExtensionName(file) = "zip" Then
objFSO.MoveFile objFileCopy.Path, strDestination
End If
Next
Dim shell
Set shell=createobject("wscript.shell")
Shell.run "C:\testsend.bat"
Set shell=nothing
【问题讨论】: