HomeProductsFTPShell ClientFTPShell Script › Script 1
#*************************************************************
#
#  Script name: checkftp.fscr
#
#  Description: Poll a remote FTP server every 5 minutes,
#               checking for new files that match a specific
#               wildcard name. If a match is found, the file
#               is downloaded to the local computer and deleted
#               from the remote FTP server.
#               NOTE: The script must be scheduled as a
#                     recurring task so that it can be
#                     executed every 5 minutes. Also, for
#                     debugging, be sure to enable the log
#                     file for the session used to schedule
#                     this script.
#               NOTE: Always verify that a script is doing
#                     exactly what you wanted it to do,
#                     before scheduling it as an automated
#                     task to avoid unpleasant surprises!
#*************************************************************


# Try to connect to the FTP server - 3 attempts max.
# If the connection was successful, exit out of the loop early.
# Otherwise, wait for 10 seconds and try again until the loop
# is exceeded (3 in our case)
# NOTE: Whenever a macro that starts with "ftp" is executed,
#       eg. ftpconnect, ftpsetpath, etc., the result of
#       the last executed macro is stored in the keyword
#       "ftpresult"

loop 3 begin
  ftpconnect("127.0.0.1", 229, "jj", "jj");
  if(success eq ftpresult) begin
    exitloop;
  end else begin
    waitsecs(10);
  end
end


# Check if the ftpconnect command was successful.
# If the connect was not successful, print out an error
# message and end the script.
# NOTE: loop, if, and foreach statements must contain
#       enclosing "begin" and "end" blocks.

if(success ne ftpresult) begin
  print("ERROR: could not connect to server after 3 attempts");
  endscript;
end


# Set the desired local and remote paths
# NOTE: "local" and "remote" are keywords used to denote the local
#       and remote computers respectively.
# NOTE: Since "\" is also used as an escape character to specify
#       printable quotes, local paths may be specified using a
#       double backslash like "c:\\myfolder"

ftpsetpath(local, "c:\\temp");

if(success ne ftpresult) begin
  print("ERROR: could not set local path");
  ftpdisconnect();
  endscript;
end

ftpsetpath(remote, "/");

if(success ne ftpresult) begin
  print("ERROR: could not set remote path");
  ftpdisconnect();
  endscript;
end


# If passive mode transfers are required, enable passive mode

enablepasv();


# List the contents of the remote folder. In order
# to be able to manipulate this list in the future,
# we store it in a variable.
# Any name can be chosen for this variable, but the first
# character must be "@". eg. @my_list

ftpgetlist(remote, @my_list);

if(success ne ftpresult) begin
  print("ERROR: could not list the contents of the current path");
  ftpdisconnect();
  endscript;
end


#print out the number of items that are in the list

print("The remote folder contains ", @my_list.count, " items");


# Set the transfer type to auto. Valid keywords are "ascii",
# "binary" and "auto".
# Also, set the rules to use if a duplicate file already exists
# in the destination path. The files may be compared by size or
# by date. This is indicated by the keywords "bysize" or "bydate".
# The following rule keywords may be specified: "resume", "rename"
# "overwrite", and "skip". Refer the help manual for the syntax of
# the "setduperules" macro.

settransfertype(auto);
setduperules(bysize, overwrite, skip, skip);

# We can now pick each item in the list and decide whether we
# need to download it. This is accomplished by the "foreach"
# statement. Any name can be chosen for the variable that denotes
# each individual item, but the first character must be "$".
# eg. $my_item
# For each item, check if it is a file. If so, check if the name
# matches the wildcard "*.txt". If so, download the file to the
# local folder, and delete it from the remote server.

foreach $my_item in @my_list begin
  if(false eq $my_item.isfolder) begin
    if("*.txt" eq $my_item.name) begin
      ftpdownload(file, $my_item.name);
      if(success eq ftpresult) begin
        ftpdelete(remote, file, $my_item.name);
      end else begin
        print($my_item.name, " ", "could not be downloaded");
      end
    end else begin
      print($my_item.name, " ", "does not match *.txt");
    end
  end else begin
    print($my_item.name, " is a folder");
  end
end


# Finally, disconnect from the remote ftp server

ftpdisconnect();


# we can now finish the script. Since this script has been
# scheduled as a recurring task, it will be run again
# after 5 minutes.

endscript;

Download Free 30 Day Trial

Download FTPShell Server and FTPShell Client software and try it out free for 30 days.

Go to the Download Page



Buy License

Purchase a license for FTPShell Server or FTPShell Client.

Find out more about Licensing Options



FTPShell Server

File Transfer Server Software that supports FTP and secure SFTP and FTPS protocols.

Find out more about FTPShell Server



FTPShell Client

File Transfer Client Software that connects to Servers supporting FTP, SFTP, and FTPS.

Find out more about FTPShell Client



Operating System Requirements

FTPShell Server and FTPShell Client work with all Microsoft Windows Desktop and Windows Server Operating Systems including Windows XP/7/8/10/11 and Windows Server 2003/2008/2012/2016/2019/2022