SFTP 传输文件并将文件移动到文件夹

发布于 2024-09-05 02:25:26 字数 2978 浏览 8 评论 0原文

这是我的第一篇文章,所以请原谅我的无知。我正在使用 vbscript 将所有 .csv 类型文件压缩到特定文件夹中。经过一番谷歌搜索后,我找到了一个可行的vbscript执行此操作并启用计划任务来自动执行此操作。

接下来我需要做的是通过 sftp 传输 zip 文件,然后将该 zip 文件“移动”到另一个文件夹中。我相信前者可以通过命令行使用 pscp.exe 来实现,但有人可以告诉我如何做后者吗?

基本上,压缩每天会进行两次,因此它的时间戳类似于 yyyymmdd0900.zip(对于上午 9 点的计划)和 yyyymmdd1800.zip(对于下午 6 点的计划)。传输后,我想将生成的 zip 文件移动(而不是复制)到另一个文件夹中。

任何指示将不胜感激。预先感谢大家。

编辑:这是我根据一些谷歌搜索拼凑在一起的一些代码。它做我想做的事。请原谅“粘贴”,因为我不知道如何正确格式化它。目前,它在复制后运行 bat 文件,但我刚刚注意到我需要发送(使用 PuTTY Secure Copy)“最新”zip 文件,然后再将其移动到“已完成”文件夹。有人可以告诉我该怎么做吗?

压缩文件并重命名 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

This is my first post so please excuse my ignorance. I am using a vbscript to zip all .csv type files in a particular folder. After some google searches, I have found a workable vbscript to do this and have enabled a scheduled task to automate this.

What I need to do next is to transfer the zip file via sftp and then "move" that zip file into another folder. I believe the former can be achieved using pscp.exe via command line but can someone show me how to do the latter?

Basically the zipping will be done twice a day and so it will have a timestamp similar to yyyymmdd0900.zip (for 9am schedule) and yyyymmdd1800.zip (for 6pm schedule). After the transfer, I want to move (not copy) the zip file generated into another folder.

Any pointers would be greatly appreciated. Thank you all in advance.

EDIT: Here is some code I slapped together based on some Google searches. It does what I want it to do. Please excuse the "pasting" as i couldn't figure out how to format it properly. Currently, it runs the bat file after copying but I just noticed that i need to send (using PuTTY Secure Copy) the "latest" zip file before moving it to the "completed" folder. Can someone please show me how to do this?

Zipping the file and rename the zip file

My Code :

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒绊 2024-09-12 02:25:35

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

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

我发现以下示例代码在检查文件存在后移动文件。源参数允许使用通配符,但 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
%>

sftp client provides a means to change working directory on the host before performing any file transfers. It would be better to thus transfer the file directly to the target location.

NOTE: The above answer was a result of misunderstanding the question. I read it to mean the file had to be moved on the destination but the real operation was to move the file on the origin.

I found the following example code that moves a file after checking that it exists. Wildcards are allowed for the source parameter but then FileExists may not work. Requires vbscript 2.0 to work.

<%
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
%>
天气好吗我好吗 2024-09-12 02:25:33

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

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

This will move a file to the specified location.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文