Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

problem with FTP script

Posted on 2010-11-16
6
Medium Priority
?
357 Views
Last Modified: 2012-05-10
hi,

i have been very lucky from one of the guys here (rogerard) who was very kind to help me out with a script for uploading files using FTP but i am  getting an error on line 61 and char 3 "input past end of file"

i was wondering if any body could help me out?

i have attached the code
'------------------------------------------------------------------------------
Const appName = "FTP Upload Utility"
'------------------------------------------------------------------------------

Const Hostname = "211.146.164.199"
Const Port = 21
Const Username = "temp"
Const Password = "temp"
Const RemoteDir = "/news/test"
Const strExt = "MP3"
Const LocalDir = "G:\Test\files\"
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("%TEMP%")
  ' 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)
  results = textFile.ReadAll
  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

0
Comment
Question by:jonathanduane2010
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:gbanik
ID: 34144323
At the outset... replace

  Set textFile = fso.OpenTextFile(outputFile, 1, 0, -2)
  results = textFile.ReadAll
  textFile.Close

by

 Set textFile = fso.OpenTextFile(outputFile, 1, 0, -2)
 If Not textFile.AtEndOfStream Then
    Results = textFile.ReadAll
 End If
 textFile.Close
0
 

Author Comment

by:jonathanduane2010
ID: 34144408
ok i have tried that, although i am not gettign any errors the file doesnt seem to appear in the remote end?
0
 
LVL 13

Accepted Solution

by:
gbanik earned 1000 total points
ID: 34144825
The code was just to see whether the file contains any data. That would either mean the source file does not contain any or the data fetch is not working properly.

I suggest you kill the outputFile before proceeding. The code would not create the file if the data fetch fails. You would immediately come to know the root problem.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jonathanduane2010
ID: 34145466
cool, i have a simple script here, how would i get the script to monitor every minute and upload any mp3s to ftp location?

set ftp = CreateObject("Chilkat.Ftp2")

'  Any string unlocks the component for the 1st 30-days.
success = ftp.UnlockComponent("Anything for 30-day trial")

If (success <> 1) Then
    MsgBox ftp.LastErrorText
    WScript.Quit
End If

ftp.Hostname = "213.146.164.185"
ftp.Username = "temp"
ftp.Password = "temp"

'  The default data transfer mode is "Active" as opposed to "Passive".

'  Connect and login to the FTP server.
success = ftp.Connect()
If (success <> 1) Then
    MsgBox ftp.LastErrorText
    WScript.Quit
End If

'  Change to the remote directory where the file will be uploaded.
success = ftp.ChangeRemoteDir("news/test")
If (success <> 1) Then
    MsgBox ftp.LastErrorText
    WScript.Quit
End If

'  Upload a file.
localFilename = "1.mp3"
remoteFilename = "1.mp3"

success = ftp.PutFile(localFilename,remoteFilename)
If (success <> 1) Then
    MsgBox ftp.LastErrorText
    WScript.Quit
End If

ftp.Disconnect 

MsgBox "File Uploaded!"

0
 
LVL 13

Expert Comment

by:gbanik
ID: 34145546
Hi jonathanduane2010, though I wouldnt mind answering it, but it may be better to post this as a seperate question.
0
 

Author Comment

by:jonathanduane2010
ID: 34145564
ok agreed.
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Progress
Introduction to Processes

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question