Solved

problem with FTP script

Posted on 2010-11-16
6
351 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

920 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

15 Experts available now in Live!

Get 1:1 Help Now