[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 606
  • Last Modified:

cant get ftp script to work

hi i am using the following code for ftp and first of all i am geting the attached image as an error message, and in the log file i get this

lcd "c:\mp3"
open 211.116.164.175 21
user username
password
cd "/mp3"
binary
prompt n
put "20110417110010.mp3"
quit
'------------------------------------------------------------------------------
Const appName = "FTP Upload Utility"
'------------------------------------------------------------------------------

Const Hostname = "211.116.164.175"
Const Port = 21
Const Username = "username"
Const Password = "password"
Const RemoteDir = "/mp3"
Const strExt = "MP3"
Const LocalDir = "c:\mp3"
Const MaxAge = 10

SET objFSO = CreateObject("Scripting.FileSystemObject")

Set ObjFolder = objFSO.GetFolder(LocalDir)
FOR EACH objFile in ObjFolder.Files
  if RIGHT(UCASE(objFile.Path),LEN(strExt)+1) = "." & strExt then
    if datediff("s", objFile.DateLastModified, Now) > 40 Then
      Upload hostname, port, username, password, objFile.Name, localDir, remoteDir
      'objFile.Delete   
    End If
  End If
Next

'------------------------------------------------------------------------------

Sub Upload(hostname, port, username, password, localFile, localDir, remoteDir) 
  Set shell = CreateObject( "WScript.Shell" )
  Set fso = CreateObject("Scripting.FileSystemObject")

  tempDir = shell.ExpandEnvironmentStrings("C:\mp3")
  ' temporary script file supplied to Windows FTP client
  scriptFile = tempDir & "\" & fso.GetTempName
  ' temporary file to store standard output from Windows FTP client
  outputFile = tempDir & "\" & fso.GetTempName


  'input script
  script = script & "lcd " & """" & localDir & """" & vbCRLF
  script = script & "open " & hostname & " " & port & vbCRLF
  script = script & "user " & username & vbCRLF
  script = script & password & vbCRLF
  script = script & "cd " & """" & remoteDir & """" & vbCRLF
  script = script & "binary" & vbCRLF
  script = script & "prompt n" & vbCRLF
  script = script & "put " & """" & localFile & """" & vbCRLF
  script = script & "quit" & vbCRLF

  Set textFile = fso.CreateTextFile(scriptFile, True)
  textFile.WriteLine(script)
  textFile.Close
  Set textFile = Nothing

  ' bWaitOnReturn set to TRUE - indicating script should wait for the program
  ' to finish executing before continuing to the next statement
  shell.Run "%comspec% /c FTP -n -s:" & scriptFile & " > " & outputFile, 0, TRUE
  Wscript.Sleep 500
  ' open standard output temp file read only, failing if not present
  Set textFile = fso.OpenTextFile(outputFile, 1, 0, -2)
  if not textfile.atendofstream then
    results = textFile.ReadAll
  else
    results = ""
  end if
  textFile.Close
  Set textFile = Nothing
  If InStr(results, "550") > 0 And (InStr(results, "226") or Instr(results, "221")) Then
    fso.DeleteFile(scriptFile)
    fso.DeleteFile(outputFile)
    Msg ="WARNING: Could not change to destination directory on host!" & _
      vbCRLF & "File however appears to have been uploaded to default " & _
      "FTP directory associated with user on host."
    MsgBox Msg, vbExclamation, appName

  ElseIf (InStr(results, "226")  or Instr(results, "221")) > 0 Then
    MsgBox "File Uploaded Successfully.", vbInformation, appName
    fso.DeleteFile(scriptFile)
    fso.DeleteFile(outputFile)
  Else
    If InStr(results, "530") > 0 Then
      Msg ="ERROR: Invalid Username/Password"
    ElseIf InStr(results, "550") > 0 Then
      Msg ="ERROR: Could not change to destination directory on host"
    ElseIf InStr(results, "553") > 0 Then
      Msg ="ERROR: Could not create file on host"
    ElseIf InStr(results, "Unknown host") > 0 Then
      Msg ="ERROR: Unknown host"
    ElseIf InStr(results, "File not found") > 0 Then
      Msg ="ERROR: Local File Not Found"
    Else
      Msg ="An ERROR may have occurred."
    End If

    Msg = Msg & _
      vbCRLF & "Script file leveraged: " & scriptFile & _
      vbCRLF & "FTP Output file: " & outputFile & _
      vbCRLF & _
      vbCRLF & "Ensure the above files are manually deleted, as they may " & _
     "contain sensitive information!"
    MsgBox Msg, vbCritical, appName
  End If
  Set shell = Nothing
  Set fso = Nothing
End Sub

Open in new window

ftp.JPG
0
jonathanduane2010
Asked:
jonathanduane2010
3 Solutions
 
mistertiCommented:
Hi, i've corrected the errors in the script and it now works fine for me:
There are a couple of things though: The DateDiff function is checking if the file is more than 10 secs. old. Check this link for other possible values to DateDiff function http://msdn.microsoft.com/en-us/library/b5xbyt6f%28v=vs.80%29.aspx

In the short time put into this, i couldnt find the correct way to call the sleep function inside the upload Sub so i commented it out. Also, you can now uncomment the line 68 to see the actual results from the server.

'------------------------------------------------------------------------------
Const appName = "FTP Upload Utility"
'------------------------------------------------------------------------------

Const Hostname = "211.116.164.175"
Const Port = 21
Const Username = "username"
Const Password = "password"
Const RemoteDir = "/mp3"
Const strExt = "MP3"
Const LocalDir = "c:\mp3"
Const MaxAge = 10

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set ObjFolder = objFSO.GetFolder(localDir)
For Each objFile In ObjFolder.Files
  If Right(UCase(objFile.Path), Len(strExt) + 1) = "." & strExt Then
    If DateDiff("s", objFile.DateLastModified, Now) > MaxAge Then
      Upload hostname, port, username, password, objFile.Name, localDir, remoteDir
      'objFile.Delete
    End If
  End If
Next
'------------------------------------------------------------------------------

Sub Upload(hostname, port, username, password, localFile, localDir, remoteDir)
  Dim Shell
  Set Shell = CreateObject("WScript.Shell")
  Set fso = CreateObject("Scripting.FileSystemObject")

  tempDir = Shell.ExpandEnvironmentStrings(localDir)
  ' temporary script file supplied to Windows FTP client
  scriptFile = tempDir & "\" & fso.GetTempName
  ' temporary file to store standard output from Windows FTP client
  outputFile = tempDir & "\" & fso.GetTempName


  'input script
  script = script & "lcd " & """" & localDir & """" & vbCrLf
  script = script & "open " & hostname & " " & port & vbCrLf
  script = script & "user " & username & vbCrLf
  script = script & password & vbCrLf
  script = script & "cd " & """" & remoteDir & """" & vbCrLf
  script = script & "binary" & vbCrLf
  script = script & "prompt n" & vbCrLf
  script = script & "put " & """" & localFile & """" & vbCrLf
  script = script & "quit" & vbCrLf

  Set textFile = fso.CreateTextFile(scriptFile, True)
  textFile.WriteLine (script)
  textFile.Close
  Set textFile = Nothing

  ' bWaitOnReturn set to TRUE - indicating script should wait for the program
  ' to finish executing before continuing to the next statement
  Shell.Run "%comspec% /c FTP -n -s:" & scriptFile & " > " & outputFile, 0, True
  'WScript.Sleep 500
  ' open standard output temp file read only, failing if not present
  Set textFile = fso.OpenTextFile(outputFile, 1, 0, -2)
  If Not textFile.atendofstream Then
    results = textFile.ReadAll
  Else
    results = ""
  End If
  textFile.Close
  Set textFile = Nothing
  'MsgBox results
  If InStr(results, "550") > 0 And (InStr(results, "226") Or InStr(results, "221")) Then
    fso.DeleteFile (scriptFile)
    fso.DeleteFile (outputFile)
    Msg = "WARNING: Could not change to destination directory on host!" & _
      vbCrLf & "File however appears to have been uploaded to default " & _
      "FTP directory associated with user on host."
    MsgBox Msg, vbExclamation, appName

  ElseIf (InStr(results, "226") Or InStr(results, "221")) > 0 Then
    MsgBox "File Uploaded Successfully.", vbInformation, appName
    fso.DeleteFile (scriptFile)
    fso.DeleteFile (outputFile)
  Else
    If InStr(results, "530") > 0 Then
      Msg = "ERROR: Invalid Username/Password"
    ElseIf InStr(results, "550") > 0 Then
      Msg = "ERROR: Could not change to destination directory on host"
    ElseIf InStr(results, "553") > 0 Then
      Msg = "ERROR: Could not create file on host"
    ElseIf InStr(results, "Unknown host") > 0 Then
      Msg = "ERROR: Unknown host"
    ElseIf InStr(results, "File not found") > 0 Then
      Msg = "ERROR: Local File Not Found"
    Else
      Msg = "An ERROR may have occurred."
    End If

    Msg = Msg & _
      vbCrLf & "Script file leveraged: " & scriptFile & _
      vbCrLf & "FTP Output file: " & outputFile & _
      vbCrLf & _
      vbCrLf & "Ensure the above files are manually deleted, as they may " & _
     "contain sensitive information!"
    MsgBox Msg, vbCritical, appName
  End If
  Set Shell = Nothing
  Set fso = Nothing
End Sub

Open in new window

0
 
jonathanduane2010Author Commented:
i still am getting the same error messages and wont upload to my ftp server?

am basically looking for a vb script that uploads the mp3s in a certain folder to an ftp directory?
0
 
simon3270Commented:
What does c:\mp3\radF5313.tmp contain?

One thing to try is to put the password on the same line as the "user", as:
   script = script & "user " & username & " " & password & vbCRLF
0
 
mistertiCommented:
go to the mp3 folder on your disk and open the lastfew  tmp files with wordpad as the contain the log of the server-client conversation or uncomment the line 68 to see results, as i've told you before.
0
 
AlexPaceCommented:
Before you mess around with any scripting tool you need to know exactly what he FTP conversation will look like at the protocol level.  So put your FTP client into debug mode or enable the trace logging and do a full sequence with the logon, change directory, upload, and logoff.  You may find that the exact numeric values returned by the server are different than the ones in that VBScript, espeically the last digit.  The FTP standard leaves a lot of room for interpretation so there is often significant variation in the responses given by different servers, especially in the formatting of directory listings.

Anyway, if you givfe up on the VBScript here is how you could automate the upload using Robo-FTP:
WORKINGDIR "c:\mp3"  ;; Local folder
FTPLOGON "211.116.164.175" /user="username" /pw="secret"
FTPCD "/mp3"         ;; Remote folder
SENDFILE "*.mp3"
FTPLOGOFF
EXIT

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now