Hi
I have earlier written an article about a FTP Automation. Now it is time to describe how to upload a file without the use of a Automation but with use of the standard FTP program.
The upload is done in 2 steps:
- Build a put script
- Use the put script to upload the file.
So how do we build a put script? Take a look here:
Char1 := 13; char2 := 10; PutScriptFile.CREATE(Ftpdir + '\put.scr'); //Do the Login PutScriptFile.WRITE(Username +FORMAT(Char1) +FORMAT(char2)); PutScriptFile.SEEK(PutScriptFile.POS-1); PutScriptFile.WRITE(FTPPassword + FORMAT(Char1) +FORMAT(char2)); PutScriptFile.SEEK(PutScriptFile.POS-1); //change to source dir PutScriptFile.WRITE('cd '+ftpdir+ FORMAT(Char1) +FORMAT(char2)); PutScriptFile.SEEK (PutScriptFile.POS-1); //Change to upload dir PutScriptFile.WRITE('lcd ' + uploaddir + FORMAT(Char1) +FORMAT(char2)); PutScriptFile.SEEK(PutScriptFile.POS-1); //Upload the files PutScriptFile.WRITE('mput *.*'+ FORMAT(Char1) +FORMAT(char2)); PutScriptFile.SEEK(PutScriptFile.POS-1); //End ftp connection PutScriptFile.WRITE('quit'+ FORMAT(Char1) +FORMAT(char2)); PutScriptFile.SEEK (PutScriptFile.POS-1); PutScriptFile.CLOSE;
Where PutScriptFile is a File.
Now that the Put Script is ready – all we have to do is use it together with the FTP command:
//First create a temp file containing the ftp command: Commandfile.CREATE(ftpdir + '\upload.cmd'); Commandfile.WRITE('ftp -i -s:' +'put.scr ' + "FTP Server IP"); Commandfile.CLOSE; //Now execute the command FileName := ftpdir + '\upload.cmd'; WaitOnReturn := TRUE; WshShell.Run(FileName,WindowType,WaitOnReturn);
Where Commandfile is a File, and WshShell is defined as ‘Windows Script Host Object Model’.WshShell
That’s all – now you can upload a file with FTP
Good job, very useful!
And what if the ftp connection fails? Is there a way to handle this error?
Bye.