Solved

problem with FTP script

Posted on 2010-11-16
6
350 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
  • 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 250 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now