Script

FTP

The professional tool to automate FTP, SFTP, FTPS and Batch FTP

Syntax:

GETFILESIZE(location,file)

  • location: Set it to one of the following values.
    LOCAL The file in the second parameter is a local file.
    REMOTE The file in the second parameter is a file located in the FTP server.
  • file: file name with or without path.

Return Value:

GETFILESIZE returns the size of the file in bytes if the operation was successful. On error, it returns a negative value of the corresponding error code.

Command compatibility:

This command was added in ScriptFTP 3.2 build Jan 11th 2009.

See also:

GETFILETIME

Examples:

# Get the file size (in bytes) of the remote file
# test1.txt located in the current remote directory
$size=GETFILESIZE(REMOTE,"test1.txt")
 
# The size is
PRINT($size)
 
# Get the file size (in bytes) of the remote file
# test2.txt located in myremotedir/myremotesubdir/
$size=GETFILESIZE(REMOTE,"myremotedir/myremotesubdir/test2.txt")
 
# The size is
PRINT($size)
 
# Get the file size (in bytes) of the remote file
# test3.txt located in the remote root directory
$size=GETFILESIZE(REMOTE,"/test3.txt")
 
# Get the file size (in bytes) of the local file
# test4.txt located in the current local directory
$size=GETFILESIZE(LOCAL,"test4.txt")
 
# The size is
PRINT($size)
 
# Get the file size (in bytes) of the local file
# test5.txt located in D:\example
$size=GETFILESIZE(LOCAL,"D:\example\test5.txt")
 
# The size is
PRINT($size)
 
# This script checks every 10 seconds if a
# remote file has changed its size. If the
# size changes it downloads the file.
 
:start
 
$result=OPENHOST("127.0.0.1","carl","123456")
 
# Check if the connection failed
IF($result!="OK")
    # wait 5 seconds and try again
    SLEEP(5)
    GOTO :start
END IF
 
:check_file_size
 
$file_size=GETFILESIZE(REMOTE,"data.log")
 
# Check if GETFILESIZE failed
IF($file_size<0)
    # wait 5 seconds and try again
    SLEEP(5)
    # Connection may be still opened
    CLOSEHOST
    # Jump to the beginning of the script
    GOTO :start
END IF
 
PRINT("Last size was ".$last_file_size)
PRINT("Read size is ".$file_size)
 
# Check if the file size has changed
IF($last_file_size!=$file_size)
    # the size has changed. download the file
    PRINT("File size change detected")
    $result=GETFILE("data.log")
    # If the download failed jump to start
    IF($result!="OK")
        # wait 5 seconds and try again
        SLEEP(5)
        # Connection may be still opened
        CLOSEHOST
        # Jump to the beginning of the script
        GOTO :start
    ELSE
        PRINT("File successfully downloaded")
        $last_file_size=$file_size
        # Wait 10 seconds and check if
        # the size has changed again
        SLEEP(10)
        GOTO :check_file_size
    END IF
ELSE
    # The size has not changed. 
    # wait 10 seconds and check again
    SLEEP(10)
    GOTO :check_file_size
END IF