Jump to content


Photo

FTP_GetFile


  • Please log in to reply
12 replies to this topic

#1 DataLife

DataLife
  • Members
  • 737 posts

Posted 27 April 2012 - 10:27 PM

I hope I can explain this well enough.

I have a file on my home ftp server call FileOnFTPserver.txt

I want to download this file from another computer and read line 1. If line 1 is 1 then continue, if it is not 1 then keep downloading and reading it until line 1 does equal to 1

First I tried it with FTP Functions by Olfen & Andreone found here, http://www.autohotke...pic.php?t=10393

I am able to download it just fine, I read line 1 and it does not equal 1 (it equals 0) so I keep looping and downloading FileOnFTPserver.txt

Here is the problem.

When, I change line 1 on FileOnFTPserver.txt at my server and the loop downloads it again it never downloads the changed text file. I know it is downloading it again because while my script was looping and downloading it I deleted Status.txt and it was downloaded again. But always with 0 as the first line.
Here is the script.
#Include C:\Program Files\AutoHotkey\lib\ftp.ahk
SetWorkingDir, %A_ScriptDir%

Username = JohnDoe
Password = ahk
FTPserver = 111.111.111.111

FtpConnection := FtpOpen(FTPserver, "21", Username, Password)
If (FtpConnection != 1)
 {
  MsgBox,16,Error 1, Not able to upload File.`nMake sure you are connected to the internet and try again.
  Return
 }

RemoteFile = FileOnFTPServer.txt
NewFile = Status.txt

KeepChecking:
FtpGetFile( RemoteFile, NewFile)

FileReadLine,Status,Status.txt,1 ;I go to my FTP server and change FileOnFTPServer.txt line 1 to 1   - It never downloads the changed text file. I even deleted the Status.txt file and it downloaded the unchanged txt file.
if Status <> 1
 {
  sleep 1000
  Goto KeepChecking
 }
MsgBox finished
FileDelete,Status.txt
FtpClose()

Since I am using Autohotkey_L, I rewrote the script using shajuls modified version for ahk_L, found here http://www.autohotke...getfile#p371957
This script won't even download the file. It logs into my server but my server gives an error "Cannot open data connection"
The script times out and downloads a blank Status.txt

I think the issue is with hConnect. I do not know what that is. I looked on MSDN and it says " Handle to an FTP session " so I thought maybe it was FtpConnection.

Here is that script
#Include C:\Program Files\AutoHotkey\lib\ftp.ahk
SetWorkingDir, %A_ScriptDir%

Username = JohnDoe
Password = ahk
FTPserver = 111.111.111.111

FtpConnection := Ftp_Open(FTPserver, "21", Username, Password)
If (FtpConnection = 0)
 {
  MsgBox,16,Error, Not able to upload File.`nMake sure you are connected to the internet and try again.
  Return
 }

RemoteFile = FileOnFTPServer.txt
NewFile = Status.txt

;FTP_GetFile(hConnect,RemoteFile, NewFile="", Flags=0
KeepChecking:  ;What is hConnect - I looked on MSDN and it says " Handle to an FTP session " so I thought maybe it was FtpConnection
Ftp_GetFile( FtpConnection,RemoteFile, NewFile)

FileReadLine,Status,Status.txt,1

if Status <> 1
 {
  sleep 1000
  Goto KeepChecking
 }
MsgBox done
FileDelete,Done.txt
Ftp_Close()

Also, this taken from ftp.ahk by shajul.
FTP_GetFile(hConnect,RemoteFile, NewFile="", Flags=0) {
   ;Flags:
   ;FTP_TRANSFER_TYPE_UNKNOWN = 0 (Defaults to FTP_TRANSFER_TYPE_BINARY)
   ;FTP_TRANSFER_TYPE_ASCII = 1
   ;FTP_TRANSFER_TYPE_BINARY = 2
   If NewFile=
      NewFile := RemoteFile
   ;global ic_hInternet
   r := DllCall("wininet\FtpGetFile" , "uint", hConnect , "str", RemoteFile , "str", NewFile
   , "int", 1 ;do not overwrite existing files
   , "uint", 0 ;dwFlagsAndAttributes
   , "uint", Flags
   , "uint", 0) ;dwContext
   Return (ErrorLevel or !r) ? 0 : 1
   }
Does 1 mean "do not overwrite existing files"
I changed it to 0 and still have the same problem.

I suppose I need to get the script working using the ftp functions by shajul and then see if I have that same issue with it not downloading the changed FileOnFTPServer.txt

#2 dylan904

dylan904
  • Members
  • 706 posts

Posted 30 April 2012 - 12:18 AM

Hey there!
I've had my issues with FTP you can believe that.
But for this, IMHO, an FTP lib is excessive.
Take a look...
SetWorkingDir, %A_ScriptDir%

UserName = JohnDoe
Password = ahk
FTPserver = 111.111.111.111
RemoteFile = FileOnFTPServer.txt
NewFile = Status.txt

KeepChecking:
UrlDownloadToFile, ftp://%UserName:%Password%@%111.111.111.111%/%RemoteFile%, %NewFile%

FileReadLine,Status,Status.txt,1 ;I go to my FTP server and change FileOnFTPServer.txt line 1 to 1   - It never downloads the changed text file. I even deleted the Status.txt file and it downloaded the unchanged txt file.
if (Status != 1)
{
  sleep 1000
  Goto KeepChecking
}
MsgBox finished
FileDelete,Status.txt
It simplifies it if it doesn't do anything else, but try it out and let me know if it worked for you or not.

#3 DataLife

DataLife
  • Members
  • 737 posts

Posted 30 April 2012 - 12:27 AM

dylan904,

I should have done my research on UrlDownloadtofile. I use UrlDownloadToFile all the time and did not know I could use it for FTP.

I now see the example in the documentation.

I will convert my code.

thanks very much.

#4 dylan904

dylan904
  • Members
  • 706 posts

Posted 30 April 2012 - 12:39 AM

Well that's no problem DataLife.

The question is, does it work as you expect?

If not, I'll be glad to help.

#5 DataLife

DataLife
  • Members
  • 737 posts

Posted 30 April 2012 - 03:40 AM

Well that's no problem DataLife.

The question is, does it work as you expect?

If not, I'll be glad to help.


I tried
UrlDownloadToFile, ftp://Username:Password@XXXXXX.myftp.org:21/FileOnFTPServer.txt, %a_scriptdir%\new.txt

My server replied "Can't open data connection"

So I went into FileZilla passive mode settings and selected use custom port range. I tried the default 1-65535 then I tried 1024-65535 and neither worked.

Any ideas?

#6 dylan904

dylan904
  • Members
  • 706 posts

Posted 30 April 2012 - 06:41 AM

Sure, to start, I would recommend trying AHK.net's FTP service.

http://www.autohotkey.net/xfm/login/

I'm on Windows 7 and use the FTP URLDownloadToFile all the time with no problem. you might want to try to omit the port, as I personally haven't had the need to either. Once you've made the switch, get back with me and we'll take it from there.

#7 DataLife

DataLife
  • Members
  • 737 posts

Posted 30 April 2012 - 12:54 PM

Sure, to start, I would recommend trying AHK.net's FTP service.

http://www.autohotkey.net/xfm/login/

I'm on Windows 7 and use the FTP URLDownloadToFile all the time with no problem. you might want to try to omit the port, as I personally haven't had the need to either. Once you've made the switch, get back with me and we'll take it from there.



I have several scripts that use UrlDownloadToFile from my Autohotkey.net site like this.
URLDownloadToFile,http://www.autohotkey.net/~DataLife/SpaTrayIcon.ico,spatrayicon.ico

But I have never used UrlDownloadToFile with FTP to download from the Autohotkey.net site.
So I tried this..
UrlDownloadToFile, ftp://DataLife:password@autohotkey.net:21/noimage.jpg, new.jpg

then I removed the :21

Still, nothing. The file I want to repeatedly download is on my FTP server.

Basically, this file is created after a certain process has ran on my FTP server. So I keep looking for the file to exist by attempting to download it.

I could use FtpPutFile to upload this file to Autohotkey.net then have my script use UrlDownloadToFile to repeatedly attempt to download it. But that would require repeated requests to Autohotkey.net in a short amount of time. I would rather all those repeated requests be going to my FTP server.

#8 dylan904

dylan904
  • Members
  • 706 posts

Posted 30 April 2012 - 06:50 PM

If both your remote and your local filepaths are correct and in place, i recommend you go to you addressbar, and type in your FTP address as it is typed withing the code "ftp://DataLife:password@autohotkey.net" and if it logs you in, there shouldn't be a problem then, if it does something like ask you for your code again, then try to just do a http URLDownloadToFile on said file, and of coarse it should work then. This IS on AHK.net, yes?
If you have further questions, you may continue to question on here, or feel free to join the IRC channel #ahk if you like.

#9 DataLife

DataLife
  • Members
  • 737 posts

Posted 30 April 2012 - 07:22 PM

I put this in IEs address bar with my correct password

<!-- m -->ftp://DataLife:password@autohotkey.net<!-- m -->

and it prompts me for my username and password.

I can log into <!-- m -->ftp://autohotkey.net<!-- m -->

It waits appx 10 seconds then prompts me again.

#10 dylan904

dylan904
  • Members
  • 706 posts

Posted 30 April 2012 - 09:20 PM

OK, in that case, you will need a lib.

Here is a link to my personal ftp lib (FTPv2.ahk) with a few minor modifications to it. I found out that some ISP's such as Verizon block the use of FTP so they may charge for it. So you much change a few flags is all. Using http://www.autohotke...an904/FTPv2.ahk, you would do something like this...
#SingleInstance Force
#Include FTPv2.ahk

Server := "ftp.autohotkey.net", UserName := "DataLife", Password := "pass"

ftp1 := new FTP()
ftp1 ? : Quit("Could not load module/InternetOpen")
ftp1.Open(Server, UserName, Password) ? : Quit(ftp1.LastError)
ftp1.SetCurrentDirectory("\WhereIsIt") ? : Msg(ftp1.LastError) ; If it is in root directory, omit.
ftp1.GetFile("noimage.jpg", A_ScriptDir "\New.jpg") ? :
ftp1 := ""    ;__Delete called

Msgbox, 262208, Complete!, Let's hope this did the trick
Return

Quit(Message="") {
   if Message
      MsgBox, 16, Error!, %Message%, 5
   ExitApp
}

Msg(Message="") {
   MsgBox, 64, , %Message%, 5
}
I hope this works for you!

#11 DataLife

DataLife
  • Members
  • 737 posts

Posted 30 April 2012 - 09:47 PM

I get the message Error:12014 - The password was not allowed

I am able to log into <!-- m -->http://autohotkey.net<!-- m --> with my username and password.

I am not able to log into <!-- m -->ftp://autohotkey.net<!-- m --> with the same username and password.

If I put <!-- m -->ftp://autohotkey.net<!-- m --> in the IE address bar and wait for the login screen then enter my username and password it won't accept it.

Should I be able to log in that way?

thanks
DataLife

#12 dylan904

dylan904
  • Members
  • 706 posts

Posted 30 April 2012 - 10:24 PM

Really, like I said i was having the same issue while connected to Verizon, but this solved it, particularly converting the libs.
If it is a similar case, you wouldn't be able to login over FTP, AHK.net hosts the FTP over http, so naturally you would be able to access it that way. If you did in deed switch the libs, I'm not 100%. But i know it is directly related to either a firewall, or an ISP's restrictions. If you come to the IRC, i may be able to provide additional help.

#13 DataLife

DataLife
  • Members
  • 737 posts

Posted 30 April 2012 - 10:34 PM

I was using Verizon with all my previous tests.

I just connected to my home Windstream account and get the same error as above.

I am going to be busy for about 2 hours. Then I will look at it again.

thanks
Robert