Jump to content

_FTP_FileRead :: Number of bytes to read?


Recommended Posts

Hi,

I was wondering how to use _FTP_FileRead, I need just one file to be read which has just one number in it on the first line. But I have no idea about what I should use for "number of bytes to read". The number in the file can vary, obviously.

Also, here's a quote from the helpfile:

Reads data from a handle opened by _FTP_FileOpen()

#Include <FTPEx.au3>
_FTP_FileRead($h_File, $dwNumberOfBytesToRead)

Parameters

$h_File Handle returned by _FTP_FileOpen() to the ftp file.

$dwNumberOfBytesToRead Number of bytes to read.

Any help would be appreciated :mellow:.

Edited by nf67
Link to comment
Share on other sites

  • Estimate the maximum number of characters you are ever going to encounter in that first line.
  • Double it.
  • Use that as the number of bytes to be read.
  • Open/read your file in binary mode, and search for the first @CR character which should be your first line end delimiter.
  • All that you have read in before this is your string you are looking for.
  • If you need to convert the string to a number, that facility is also available.
Post some more code of what you have attempted, and the contents of the first two lines of the FTP source file, and we'll offer suggestions.
Link to comment
Share on other sites

  • Open/read your file in binary mode, and search for the first @CR character which should be your first line end delimiter.
  • All that you have read in before this is your string you are looking for.
  • If you need to convert the string to a number, that facility is also available
What do you mean by binary mode? Should I specify this when opening the file? I'm currently using the default.

$FTP_TRANSFER_TYPE_BINARY - Transfers the file using FTP's Image (Type I) transfer method. The file is transferred exactly as it exists with no changes. This is the default transfer method.

$FTP_TRANSFER_TYPE_UNKNOWN - Defaults to $FTP_TRANSFER_TYPE_BINARY.

$INTERNET_FLAG_TRANSFER_ASCII - Transfers the file as ASCII.

$INTERNET_FLAG_TRANSFER_BINARY - Transfers the file as binary.

And if the number I need is the only thing in the file, why would I search for @CR? Wouldn't _FTP_FileRead just give me that number?

Variables used in that piece of my code are kind of scattered, sorry :mellow:.

Link to comment
Share on other sites

This script is supposed to upload the file of which you have the path on the clipboard when Ctrl + Y is pressed.

It takes the FTP info from FTP.conf in the scriptdir, or from FTPsecure.conf, which is basically an encrypted version of FTP.conf.

Please also see "Help" from the tray icon for a more detailed explanation.

However, when checking the maximum allowed file size to transfer, which is written down in FTuP.size in the same directory the files get uploaded to, I encounter errors:

(1) MaxSize is always zero.

$MaxSize = Number(_FTP_FileRead($FtpFileOpen, _FTP_FileGetSize($FtpConnect,$FTPDir & "FTuP.size")))

(2) Even when changing If $FileSize > $MaxSize Then to If $FileSize < $MaxSize Then

I still get the "Couldn't put the file on the server, despite a succesful connection." error :mellow:.

#include <FTPEx.au3>
#include <File.au3>
#include <Misc.au3>
#Include <String.au3>
#NoTrayIcon

If _Singleton("FTuP",1) = 0 Then
    Msgbox(0,"FTuP Error:","FTuP is already running!")
    Exit
EndIf

Opt("TrayMenuMode",1)
$HelpItem = TrayCreateItem("Help")
TrayCreateItem("")
$ExitItem = TrayCreateItem("Exit")
TraySetState()

$FileDrive = ""
$FileDir = ""
$FileName = ""
$FileExt = ""

HotKeySet("^y","Upload")

While 1
    $msg = TrayGetMsg()
    Select
    Case $msg = 0
    ContinueLoop
    Case $msg = $HelpItem
    Msgbox(0,"FTuP Help:","Press Ctrl+Y to upload the file of which the path is on the clipboard." &@CRLF& "After the beep, you can use Ctrl+V to paste the link." &@CRLF&@CRLF& _
                    "FTP details are in FTP.conf or encrypted in FTPsecure.conf, sorted by line:" &@CRLF&@CRLF& "1 = FTP Server" &@CRLF& "2 = FTP Account" &@CRLF& "3 = FTP Password" &@CRLF& _
                    "4 = Directory to put the file in, include first and last slash!" &@CRLF& "5 = URL to the folder where the file is placed, include last slash!" &@CRLF&@CRLF& _
                    "If you have both FTP.conf and FTPsecure.conf, then FTPsecure.conf will be used.")
    Case $msg = $ExitItem
    ExitLoop
    EndSelect
WEnd

Func Upload()
    $FilePath = ClipGet()
    If FileExists($FilePath) = 0 Then
        MsgBox(0,"FTuP Error:","The file you want to upload couldn't be found. No FTP connection was made.")
    Else
        If FileExists(@ScriptDir & "\FTPsecure.conf") = 0 Then
            If FileExists(@ScriptDir & "\FTP.conf") = 0 Then
                MsgBox(0,"FTuP Error:","No config file could be found.")
                $NoConfigs = 1
            Else
                $FTPSecure = 0
                $FTPServer = FileReadLine(@ScriptDir & "\FTP.conf",1)
                $FTPAccount = FileReadLine(@ScriptDir & "\FTP.conf",2)
                $FTPPassword = FileReadLine(@ScriptDir & "\FTP.conf",3)
                $FTPDir = FileReadLine(@ScriptDir & "\FTP.conf",4)
                $SiteClip = FileReadLine(@ScriptDir & "\FTP.conf",5)
                $NoConfigs = 0
            EndIf
        Else
            $FTPSecure = 1
            $FTPServer = _StringEncrypt(0,FileReadLine(@ScriptDir & "\FTPsecure.conf",1),"password")
            $FTPAccount = _StringEncrypt(0,FileReadLine(@ScriptDir & "\FTPsecure.conf",2),"password")
            $FTPPassword = _StringEncrypt(0,FileReadLine(@ScriptDir & "\FTPsecure.conf",3),"password")
            $FTPDir = _StringEncrypt(0,FileReadLine(@ScriptDir & "\FTPsecure.conf",4),"password")
            $SiteClip = _StringEncrypt(0,FileReadLine(@ScriptDir & "\FTPsecure.conf",5),"password")
            $NoConfigs = 0
        EndIf

        If $NoConfigs = 0 Then
            $FtpOpen = _FTP_Open("ImageFTP")
            $FtpConnect = _Ftp_Connect($FtpOpen,$FTPServer,$FTPAccount,$FTPPassword)
            If $FtpConnect = 0 Then
                If $FTPSecure = 0 Then
                    MsgBox(0,"FTuP Error:","Couldn't connect to the FTP server." &@CRLF& $FTPAccount & " + " & $FTPPassword & " on " & $FTPServer)
                Else
                    MsgBox(0,"FTuP Error:","Couldn't connect to the FTP server." &@CRLF& "Using encrypted FTP details.")
                EndIf
                _FTP_Close($FtpOpen)
            Else
                $FtpFileOpen = _FTP_FileOpen($FtpConnect,$FTPDir & "FTuP.size")
                If $FtpFileOpen = 0 Then
                    MsgBox(0,"FTuP Error:","FTuP.size couldn't be found, it should be on the server in the directory where the files are uploaded" & _
                    " (4th line of the configuration file). It should contain the maximum number of bytes per file to upload.")
                    _FTP_Close($FtpOpen)
                Else
                    $FileSize = FileGetSize($FilePath)
                    $MaxSize = Number(_FTP_FileRead($FtpFileOpen, _FTP_FileGetSize($FtpConnect,$FTPDir & "FTuP.size")))
                    If $FileSize > $MaxSize Then
                        MsgBox(0,"FTuP Error:","Sorry, that file exceeds the file size limit and will not be uploaded." &@CRLF& _
                                "Filesize is " & $FileSize & " bytes, " & $MaxSize & " bytes allowed.")
                        _FTP_FileClose($FtpFileOpen)
                        _FTP_Close($FtpOpen)
                    Else
                        $PathSplit= _PathSplit($FilePath, $FileDrive, $FileDir, $FileName, $FileExt)
                        $File = Random(0,99,1) & $Filename & $FileExt
                        $FilePut = _FTP_FilePut($FtpConnect, $FilePath, $FTPDir & $File)
                        If $FilePut = 0 Then
                            MsgBox(0,"FTuP Error","Couldn't put the file on the server, despite a succesful connection.")
                            _FTP_FileClose($FtpFileOpen)
                            _FTP_Close($FtpOpen)
                        Else
                            _FTP_FileClose($FtpFileOpen)
                            _FTP_Close($FtpOpen)
                            ClipPut($SiteClip & $File)
                            Beep(500,100)
                        EndIf
                    EndIf
                EndIf
            EndIf
        EndIf
    EndIf
EndFunc

Script to generate FTPsecure.conf from FTP.conf:

#include <File.au3>
#Include <String.au3>

Encrypt()

Func Encrypt()
    $FTPConf = FileOpen(@ScriptDir & "\FTP.conf",0)
    If $FTPconf = -1 Then
        MsgBox(0,"FTuP Secure:","FTP.conf could not be found and therefore not be encrypted to FTPsecure.conf.")
        Exit
    Else
        FileDelete(@ScriptDir & "\FTPsecure.conf")
        For $LineNumber = 1 to 5
            $FileWrite = FileWriteLine(@ScriptDir & "\FTPsecure.conf", _StringEncrypt(1,FileReadLine($FTPconf,$LineNumber),"password"))
            If $FileWrite = 0 Then
                MsgBox(0,"FTuP Secure:","FTPsecure.conf couldn't be generated although FTP.conf could be read.")
                FileClose($FTPConf)
                Exit
            EndIf
        Next
        MsgBox(0,"FTuP Secure:","Succes, FTPsecure.conf has been generated.")
        FileClose($FTPConf)
    EndIf
EndFunc

No idea what's going wrong.. :P

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...