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?
Hello *,

we use ScriptFTP with the "new" *.ok-File-Feature. Maybe you remember the problem.
Now we have the problem that we must write an ok-File if the original-File is copied correctly.

E.G:
I put a file to a remote FTP-Server:
hello.txt
After hello.txt is completely moved to FTP-Server I must write a file "hello.txt.ok", that signals that the file-transfer is completed. The remote ftp-server can now work ahead with the hello.txt-File

This problem is occured in an environment where the remote ftp-server will work ahead with the transfered file.
ScriptFTP call the name during the transfering hello.part or hello.txt.part, but the part-file is not locked from ScriptFTP. So the remote ftp-server try to rename the file to check if the transfer is completed. Normaly the files are locked from the ftp-client and cannot be renamed (file ist not transfered completely). If a rename is possible the ftp-server can work ahead with the hello.txt-File.

local Drive ----> ScripFTP (put file to) ---> remote FTP
remote FTP ----> try to rename the file or expects a *.ok-file ----> move forward in a different system

So in this case It was nice to solve the problem with locking the file during the transfer or in smarter way write an *.ok after completion the transfer. In the actual behaviour of ScriptFTP we have the problem that the remote-FTP cannot occur if the file is transfered and move the file to the Working-System twice or thrice.

Is that in any possible.

Many thanks for answering
Marc
Hi Marc,
So in this case It was nice to solve the problem with locking the file during the transfer or in smarter way write an *.ok after completion the transfer.
The FTP protocol does not provide a way to lock files, there is no FTP protocol command for that. But, as far as I know, the FTP servers must lock the files that are being uploaded to prevent other processes accessing corrupt or incomplete data (your case). Some FTP servers, instead of locking the files, receive them in a temporary directory and once the upload is completed they copy the full transferred file from the temporary directory to it's final location. Depending on the FTP server software used (ProFTPd, filezila server, Microsoft IIS...) the bahavior can be the first one or the other, rarely the FTP server does nothing. Try checking, if possible, the FTP server configuration, there should be a switch, check box or something else for configuring this behavior.

I think that the other possibility (upload a *.ok file after upload) could be done. The way to do so is using the FOREACH loop and, after a file is uploaded, create a *.ok file locally and then upload it. It's only a sketch, let me know your thoughts:
FTP Script
  1. # Change current local directory
  2. LOCALCHDIR("C:\files_to_be_uploaded")
  3.  
  4. # Retrieve the file listing, only txt files
  5. GETLIST($mylist,LOCAL_FILES,"*.txt")
  6.  
  7. FOREACH $txtfile IN $mylist
  8.     # Upload the file
  9.    $result=PUTFILE($txtfile)
  10.  
  11.     # Check if the upload were successful
  12.     IF($result=="OK")
  13.        # Create the ok file
  14.        $okfilename=$txtfile.".ok"
  15.  
  16.        # Download the touch command for windows from
  17.        # http://www.codeproject.com/KB/applications/touch_win.aspx
  18.        EXEC("touch ".$okfilename)
  19.  
  20.        # Upload it
  21.        $result=PUTFILE($okfilename)
  22.  
  23.        # If the ok file was succesully uploaded, delete the local copy
  24.        IF($result=="OK")
  25.             EXEC("del /F /Q ".$okfilename)
  26.        END IF
  27.     END IF
  28.  
  29.  
  30.  
is it possible to name the file:
<filename>.ok instead of what the script currently does <filename.txt>.ok?

Our system looks for a filename.ok file but not to a filename.txt.ok

thanks
Hi Marty,

Yes, it is possible, you have to use TEXTCUT and TEXTLENGTH to remove the last three characters of the file name and then append "ok":
FTP Script
  1. $filename="test.txt"
  2. $length=TEXTLENGTH($filename)
  3. $filename_without_extension=TEXTCUT($filename,1,$length-3)
  4. $filename_ending_in_ok=$filename_without_extension."ok"
  5.  
  6. # Should show test.ok
  7. PRINT($filename_ending_in_ok)
Note that this code works only if the file has 3 characters after the dot (most common).

The complete script would be:
FTP Script
  1. # Change current local directory
  2. LOCALCHDIR("C:\files_to_be_uploaded")
  3.  
  4. # Retrieve the file listing, only txt files
  5. GETLIST($mylist,LOCAL_FILES,"*.txt")
  6.  
  7. FOREACH $txtfile IN $mylist
  8.     # Upload the file
  9.    $result=PUTFILE($txtfile)
  10.  
  11.     # Check if the upload were successful
  12.     IF($result=="OK")
  13.        # Create the ok file
  14.  
  15.        # ---------- Added
  16.        $length=TEXTLENGTH($txtfile)
  17.        $filename_without_extension=TEXTCUT($txtfile,1,$length-3)
  18.        $okfilename=$filename_without_extension."ok"
  19.        # -----------------------
  20.  
  21.  
  22.  
  23.        # Download the touch command for windows from
  24.        # http://www.codeproject.com/KB/applicati ... h_win.aspx
  25.        EXEC("touch ".$okfilename)
  26.  
  27.        # Upload it
  28.        $result=PUTFILE($okfilename)
  29.  
  30.        # If the ok file was succesully uploaded, delete the local copy
  31.        IF($result=="OK")
  32.             EXEC("del /F /Q ".$okfilename)
  33.        END IF
  34.     END IF
  35.  
  36.  
  37.  
  38.  
  39. $filename="test.txt"
  40. $length=TEXTLENGTH($filename)
  41. $filename_without_extension=TEXTCUT($filename,1,$length-3)
  42. $filename_ending_in_ok=$filename_without_extension."ok"
  43.  
  44. # Should show test.ok
  45. PRINT($filename_ending_in_ok)