Click here to Skip to main content
15,888,070 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I have a text file on my local drive and I wanted to copy that file on FTP server, If it copied successfully then the file should move to the another folder called "Backup folder" and if it fails to copy then file should move to "Error Folder" on same drive, Can you help me to get out off this?

What I have tried:

I tried, but it didn't worked. As I am only able to copy file from local drive to FTP server, But I am not able to take backup of copied file, so could you help me
Posted
Updated 30-May-18 1:15am

1 solution

It depends on which application you are using for uploading files via FTP. Some of those applications can be scripted or support uploads with a single command line call. Then call such a script / command line from a batch (Windows) or shell (Linux) script, check the return value, and act accordingly (move file).

For WinSCP this is well documented with examples: Automate file transfers (or synchronization) to FTP server or SFTP server :: WinSCP[^]. See there the last section Checking script results for an example batch file where you only have to replace the sendmail calls by move commands and use an appropriate WinSCP script file as described also on that page.
 
Share this answer
 
Comments
Akshay Gate 30-May-18 7:39am    
I want to do this overall process using SQL...
Jochen Arndt 30-May-18 7:56am    
Then you should specify which database engine you are using and how you are actually uploading the file.

But it is similar to my solution when spawning a command shell and executing a command (which is probably a shell script):
Just get the return value in your SQL stored procedure.

When for example using T-SQL with xp_cmdshell:
DECLARE @result int;
EXEC @result = xp_cmdshell @cmd;
IF (@result = 0)
PRINT 'Success'
ELSE
PRINT 'Failure';

Just execute additional shell commands instead of printing to move the file.
Akshay Gate 31-May-18 1:43am    
DECLARE @CopyFromFTPScriptPath VARCHAR(500)
DECLARE @CMD VARCHAR(5000)
DECLARE @RetVal int
SET @CopyFromFTPScriptPath = ( SELECT Value1 FROM AppConfig WHERE ConfigId= 41 )



--********************************Copy files from FTP *********************************
CREATE TABLE #tempfile (fileuploaded VARCHAR(8000))
SET @CMD=''
SET @cmd = 'ftp.exe -i -s:' + @CopyFromFTPScriptPath
INSERT INTO #tempfile
EXEC MASTER.dbo.xp_cmdshell @cmd
select * from #tempfile
TRUNCATE TABLE #tempfile

I am using this script to copy file from local drive to FTP, Now I want to know If my file is copied successfully or not, if it copied successfully then that file should move to the another folder of same drive i.e "BK folder" or in case it fails to copy file then that file should move to "Error folder" of same drive.

Could you please help me to solve this??

Thanks in advance.
Jochen Arndt 31-May-18 2:42am    
Check the documentation of your used FTP application ftp.exe about the return value. Then get the return value as shown in my above comment. If the FTP application does not provide a return value when the used command fails, you have to use another FTP application.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900