【问题标题】:SFTP transfer file and move file to folderSFTP传输文件并将文件移动到文件夹
【发布时间】: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

【问题讨论】:

    标签: vbscript sftp


    【解决方案1】:

    这会将文件移动到指定位置。

    Sub Move_File(Source_File, Destination_Folder)
        Dim fso
        Set fso = CreateObject("Scripting.FileSystemObject")
        fso.MoveFile Source_File, Destination_Folder
        Set fso = Nothing
    End Sub
    

    【讨论】:

    • 我们可以使用上述逻辑将文件传输到Unix系统中的远程目录吗?...如果它需要用户名和密码才能登录到Unix系统,我也可以传递它们吗在这个?...谢谢。
    【解决方案2】:

    sftp 客户端提供了一种在执行任何文件传输之前更改主机上的工作目录的方法。因此,最好将文件直接传输到目标位置。

    注意:以上答案是对问题的误解。我读它的意思是必须将文件移动到目的地,但真正的操作是将文件移动到原点。

    我发现以下示例代码在检查文件是否存在后移动文件。 source 参数允许使用通配符,但 FileExists 可能不起作用。需要 vbscript 2.0 才能工作。

    <%
    dim filesys
    set filesys=CreateObject("Scripting.FileSystemObject")
    If filesys.FileExists("c:\sourcefolder\anyfile.html") Then
       filesys.MoveFile "c:\sourcefolder\anyfile.html", "c:\destfolder\"
    End If
    %>
    

    【讨论】:

    • 感谢您的回复。抱歉,我应该明确表示传输是一个远程位置,而我所说的“移动”是文件所在的盒子本地的。我还希望在执行 zip 之后自动执行此操作,然后让 sftp 从文件所在的任何位置传输它。希望我说得有道理。
    • SFTP 根本没有工作目录的概念——根据标准,所有路径都必须是绝对路径。它是模拟当前目录概念的客户端软件。
    • @Eugene,很好的说明。我会更新答案。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 2016-04-11
    • 2015-03-25
    • 1970-01-01
    • 2013-12-26
    • 2016-10-11
    相关资源
    最近更新 更多