tags:

views:

316

answers:

2

Hi all,

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

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

A: 

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
%>
Amardeep
@Amardeep: Thanks for the reply. Sorry, I should have made it clear that the transfer is a remote location and the "move" I was talking about it local to the box the file resides on. I also want this automated after I perform the zip and then get sftp to transfer it from whatever location the file resides. Hope i'm making some sense.
molecule
SFTP doesn't have a concept of working directory at all - according to the standard all paths must be absolute. It's the client software, that emulates current directory concept.
Eugene Mayevski 'EldoS Corp
@Eugene, good clarification. I'll update the answer. Thanks.
Amardeep
A: 

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
Tester101