ftp automation

ScriptFTP

The professional tool to automate FTP, SFTP, FTPS and schedule FTP batch jobs

The forum is now read only. Please, go to the the main ScriptFTP website if you need help.
Need help writing a script? Have any question about ScriptFTP?
Hi,

I need to write a script that compares the file sizes of the remote file, and the local version of the file that has just been downloaded...if the file sizes are the same, it should carry on as normal, but if the file sizes are different I want it to delete the downloaded file, and try the transfer again (happy for only one retry, but maximum of 5 attempts).

Please could somebody suggest some code that might be able to provide a solution?

I'm using the below code to download the files and then the comparison with GETFILESIZE but just struggling to piece together the whole sequence of downloading, file size comparison, deleting etc.
Code: Select allFOREACH $item IN $list $result=GETFILE($item) END FOREACH
Any help would be greatly appreciated! I can send the whole script if needed.
Hello,

Try the following script. It does not implement the five attempts requirement but it is fairly easy to add this feature with a GOTO and a variable that is used as counter.
Code: Select all# Connect to server OPENHOST("ftp.myhost.com","sapsupport","123456") # Change the current local directory. All files # will be downloaded here. LOCALCHDIR("C:\path_to\files\") # Get the remote file listing, store it in $list GETLIST($list,REMOTE_FILES) # For each file in $list... FOREACH $item IN $list # Get the file size of the local and remote file $local_file_size=GETFILESIZE(LOCAL,$item) $remote_file_size=GETFILESIZE(REMOTE,$item) # If the sizes are different download     # local file is replaced if exists IF($local_file_size!=$remote_file_size) GETFILE($item) END IF END FOREACH # Close the connection CLOSEHOST
Hi,

I am trying to do something similiar, but couldnt get it to work in the way i needed it to

I need the file size to be compared after download, and if they are the same size, then delete the remote file.
It pulls the files down ok, and echos out OK for the file size, but just doesnt delete the remote file.
Code: Select all# Connect to ftp.myhost.com as myuser $result=OPENHOST("192.168.96.14","username","password") # If OPENHOST failed stop the script IF($result!="OK") STOP END IF # Change the current local directory. The files # will be downloaded here. $result=LOCALCHDIR("C:\CDR\TEMP") # If LOCALCHDIR failed stop the script IF($result!="OK") CLOSEHOST STOP END IF $result=CHDIR("/cdr") # If CHDIR failed stop the script IF($result!="OK") CLOSEHOST STOP END IF # Get the remote file listing, store it in $list GETLIST($list,REMOTE_FILES) # For each file in $list... FOREACH $item IN $list # Download the file $result=GETFILE($item) # Get the file size of the local and remote file $local_file_size=GETFILESIZE(LOCAL,$item) # The size is PRINT($result) $remote_file_size=GETFILESIZE(REMOTE,$item) # The size is PRINT($result) # If the file has been succesfully downloaded     # and size is same delete the remote copy. If not CONTINUE. IF $local_file_size!=$remote_file_size)  $Result=DELETEFILE($item) END IF END FOREACH # Close the connection CLOSEHOST
i used to use this below to just delete the remote file after download, but i now need them compared first, but i just cannot get the 2 combined!
Code: Select all# If LOCALCHDIR failed stop the script IF($result!="OK") CLOSEHOST STOP END IF $result=CHDIR("/cdr") # If CHDIR failed stop the script IF($result!="OK") CLOSEHOST STOP END IF # Get the remote file listing, store it in $list $result=GETLIST($list,REMOTE_FILES,"*.*") # If GETLIST failed stop the script IF($result!="OK") CLOSEHOST STOP END IF # For each file in $list... $item IN $list # Download the file $result=GETFILE($item) # If the file has been succesfully downloaded     # delete the remote copy. If not CONTINUE.] IF($result=="OK") DELETEFILE($item) END IF END FOREACH # Close the connection CLOSEHOST  

any help would be great

cheers

phil
Hello Phil,

I think that the error is in your first script
Code: Select all    # If the file has been succesfully downloaded     # and size is same delete the remote copy. If not CONTINUE. IF $local_file_size!=$remote_file_size)  $Result=DELETEFILE($item) END IF
Note the line of the IF, you haven't put the opening bracket. The correct way is:
Code: Select all    # If the file has been succesfully downloaded     # and size is same delete the remote copy. If not CONTINUE. IF ($local_file_size!=$remote_file_size)  $result=DELETEFILE($item) END IF
Hi,

Thanks for the response!

Just to confirm, will simply re-downloading the file if the file sizes are not equal replace the already downloaded file? I just want to ensure that multiple copies of the same file (with different sizes) aren't downloaded and then picked up later on in the chain.

Please could you also provide a bit more advice on the counter? I understand how to use the GOTO command but not quite sure how I'd prevent this from repeating indefinitely if the script was unable to carry out the process.

Many thanks.
Just to confirm, will simply re-downloading the file if the file sizes are not equal replace the already downloaded file?
Yes, ScriptFTP will always replace the files.
Please could you also provide a bit more advice on the counter? I understand how to use the GOTO command but not quite sure how I'd prevent this from repeating indefinitely if the script was unable to carry out the process.
You can make your script detect the errors retrieveing the value that the commands return. Take a look at http://www.scriptftp.com/reference.php?go=topic310 there you can find many informations and examples.

Regarding to the counters you can make GOTO work only if the counter is less than a certain value. This way you prevent infinite loops:
Code: Select all:start $counter=$counter+1 # # more commands here # IF($counter<5)  GOTO :start END IF  
Hi Ya,

I think the missing ( was a typo on my part, sorry.

i double checked the script to make sure the bracket was in and re-ran the script.
it sill doesnt delete the remote files though.
I can still get it to delete using my old script, but i cant get them to work as a single script.

this is the output from the script.
---------------------------------------------------------------------------
OPENHOST("192.168.96.14","Username",******)
Connecting to 192.168.96.14
Connected.

LOCALCHDIR("C:\CDR\TEMP")
Changing current local directory to C:\CDR\TEMP

CHDIR("/cdr")
Changing current remote directory to /cdr

GETLIST(REMOTE_FILES)
Getting file listing of current remote directory
Found 40 files.

GETFILE("UKCRE201_Creative C0 Designs_873402.csv")
Downloading................. UKCRE201_Creative Co Designs_873402.csv

GETFILESIZE(LOCAL,"UKCRE201_Creative C0 Designs_873402.csv")
OK

GETFILESIZE(REMOTE,"UKCRE201_Creative C0 Designs_873402.csv")
OK

CLOSEHOST
Disconnected.

-------------------------------------------------------------------------

Phil
Hello Phil,

I think that the error is in your first script
Code: Select all    # If the file has been succesfully downloaded     # and size is same delete the remote copy. If not CONTINUE. IF $local_file_size!=$remote_file_size)  $Result=DELETEFILE($item) END IF
Note the line of the IF, you haven't put the opening bracket. The correct way is:
Code: Select all    # If the file has been succesfully downloaded     # and size is same delete the remote copy. If not CONTINUE. IF ($local_file_size!=$remote_file_size)  $result=DELETEFILE($item) END IF
Hi Phil,

Try the following and post the output, please:
Code: Select allPRINT($local_file_size) PRINT($remote_file_size) IF ($local_file_size!=$remote_file_size)  $result=DELETEFILE($item) END IF
FYI we achieved the desired effect thus;

Code: Select all######################################################### # Set some variables $myftpserver="****" $myuser="****" $mypassword="****" ######################################################### # Connect to ftp.myhost.com as myuser $result=OPENHOST($myftpserver,$myuser,$mypassword) # If OPENHOST failed stop the script IF($result!="OK") STOP END IF ######################################################### # Change the current local directory. The files # will be downloaded here. $result=LOCALCHDIR("C:\...") # If LOCALCHDIR failed stop the script IF($result!="OK") CLOSEHOST STOP END IF $result=CHDIR("/cdr") # If CHDIR failed stop the script IF($result!="OK") CLOSEHOST STOP END IF ######################################################### # Get the remote file listing, store it in $list1 GETLIST($list1,REMOTE_FILES) # For each file in $list... FOREACH $item IN $list1 # Get the file size of the remote file $RFS= GETFILESIZE(REMOTE,$item) # Download the file $result= GETFILE($item) END FOREACH ######################################################### # Get the remote file listing, store it in $list2 GETLIST($list2,LOCAL_FILES) FOREACH $item IN $list2 # For each file in $list... # Get the file size of the local file $LFS= GETFILESIZE(LOCAL,$item) PRINT($RFS) PRINT($LFS) IF ($RFS!= $LFS) DELETEFILE($item) END IF END FOREACH ######################################################### # Close the connection CLOSEHOST  

I hope this helps anyone else trying to do the same thing.

Cheers, Stephen