HomeProductsFTPShell ClientFTPShell Script › Script 3
#*************************************************************
#
#  Name: backup.fscr
#
#  Desc: Compress files and folders, rename the compressed
#        files by attaching a unique timestamp string, and
#        upload to a remote server.
#        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:\\logs");

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 local 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(local, @my_list);

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


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

print("The local 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 compress, rename, and upload 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 the name matches the wildcard "*log*". If so,
# compress the file, attach an unique name to it using the current timestamp
# and upload it to the remote server.
# Note that a user defined string variable must start with "~" and may be used to
# manipulate name strings.
# eg. ~my_string

#generate the timestamp to attach to all compressed items
gettimestamp(~mytimestamp);

foreach $my_item in @my_list begin
  if("*log*" eq $my_item.name) begin
    ftpcompress($my_item.name, ~compressed_name);
    if(success eq ftpresult) begin
      strprint(~newname, ~mytimestamp, "_", ~compressed_name);
      ftprename(local, ~compressed_name, ~newname);
      if(success eq ftpresult) begin
        ftpupload(file, ~newname);
        if(success eq ftpresult) begin
          print(~newname, " ", "was successfully uploaded");
        end else begin
          print(~newname, " ", "could not be uploaded");
        end
      end else begin
        print(~compressed_name, " ", "could not be renamed");
      end
    end else begin
      print($my_item.name, " ", "could not be compressed");
    end
  end else begin
    print($my_item.name, " ", "does not match *log*");
  end
end


# Finally, disconnect from the remote ftp server

ftpdisconnect();


# we can now finish the script.

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