Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB SCript to automate FTP

Status
Not open for further replies.

mydisney

Programmer
May 7, 2007
55
US
I am having problems getting my VB script FTP program to work. When I try to run it using the dos command (ftp -s:xxxx) or using a shell script nothing happens (in dos I get error = invalid command).

Here is my code:

'VBScript FTP Upload to Client
Option Explicit
Dim objFSO, objMyFile, objShell, strFTPScriptFileName, strFilePut
Dim strLocalFolderName, strFTPServerName, strLoginID
Dim strPassword, strFTPServerFolder

'Set FTP options
strLocalFolderName = "c:\myftpfolder/myfiles"
strFTPServerName = "00.00.0.0"
strLoginID = "myuserid"
strPassword = "mypw"
strFTPServerFolder = "remotefolder"

'Set File name
strFilePut = "test.txt"

'Generate FTP command
strFTPScriptFileName = strLocalFolderName & "\FTPScript.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")

'If (objFSO.FileExists(strFTPScriptFileName)) Then
'objFSO.DeleteFile (strFTPScriptFileName)
'End If

Set objMyFile = objFSO.CreateTextFile(strFTPScriptFileName, True)
'objMyFile.WriteLine ("ftp -s open " & strFTPServerName)
objMyFile.WriteLine ("open " & strFTPServerName)
objMyFile.WriteLine (strLoginID)
objMyFile.WriteLine (strPassword)
objMyFile.WriteLine ("cd " & strFTPServerFolder)
'objMyFile.WriteLine ("bin")
objMyFile.WriteLine ("lcd " & strLocalFolderName)
objMyFile.WriteLine ("put " & strFilePut)
objMyFile.WriteLine ("disconnect")
objMyFile.WriteLine ("bye")
objMyFile.Close

Set objFSO = Nothing
Set objMyFile = Nothing
 
I'd replace this:
strLocalFolderName = "c:\myftpfolder/myfiles"
with this:
strLocalFolderName = "c:\myftpfolder[!]\[/!]myfiles"

furthemore, where in your code is ftp launched ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I thought if I ran the code using the "ftp -s:xxxx.txt" from a dos command screen it would work? You might be right, I'm not launching the ftp, what do I need to do? Thank you.
 
Here is the shelln object I have already created.

Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run ("ftp -s:" & chr(34) & AS400.vbs & chr(34))
Set objShell = Nothing
 
I finally got the VB script to work, somewhat. It will ftp a single file and rename the file to a .bak extension.

However, I need to be able to select all files with a ".pgp" extension and then do a "mput". When I do this the script hits the "bye" line before it has executed the ftp. What do I need to do prevent this?



'VBScript FTP Upload to Client
Option Explicit
Dim objFSO, objMyFile, objShell, strFTPScriptFileName, strFilePut
Dim strLocalFolderName, strFTPServerName, strLoginID
Dim strPassword, strFTPServerFolder

'Set FTP options
strLocalFolderName = "c:\myftpfolder\myftpfiles"
strFTPServerName = "00.00.0.0"
strLoginID = "userid"
strPassword = "pw"
strFTPServerFolder = "ftpfolder"

'Set File name
strFilePut = "*.pgp"

'Generate FTP command
strFTPScriptFileName = strLocalFolderName & "\FTPScript.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objMyFile = objFSO.CreateTextFile(strFTPScriptFileName, True)
objMyFile.WriteLine ("open " & strFTPServerName)
objMyFile.WriteLine (strLoginID)
objMyFile.WriteLine (strPassword)
objMyFile.WriteLine ("cd " & strFTPServerFolder)
objMyFile.WriteLine ("bin")
objMyFile.WriteLine ("lcd " & strLocalFolderName)
objMyFile.WriteLine ("mput " & strFilePut)
objMyFile.WriteLine ("disconnect")
objMyFile.WriteLine ("bye")
objMyFile.Close

Set objFSO = Nothing
Set objMyFile = Nothing

'Run ftp script
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run ("ftp -s:" & chr(34) & strFTPScriptFileName & chr(34))
Set objShell = Nothing

'Rename .pgp files after ftp upload
Dim filesys, file, folderName, objfile, folderObj, fileColl, objRegExp, newFile
Set filesys = CreateObject("Scripting.FileSystemObject")

folderName = "c:\travelers\totravelers"
Set folderObj = filesys.GetFolder(folderName)
Set fileColl = folderObj.Files

Set objRegExp = New RegExp
objRegExp.Pattern = ".pgp" 'looking for .pgp (extension) match
objRegExp.IgnoreCase = True

For Each objFile In fileColl
newFile = objRegExp.Replace(objFile.Name, ".bak") 'replacing with .bak extension
filesys.MoveFile objFile, folderName & "\" & newFile
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top