Topic "Deleting local files after successful transfer via script"

Author Message
Guest




Hi, I apologize in advance if this I have missed this information somewhere, but is there a way to remove local files once they have been succefully updated via a script?

For example, right now I have a machine automatically uploading to a remote linux server from a Windows workstation. I would like the first workstation to delete the local files once they have been uploaded, but I do not know what the command/syntax would be to accomlish this (if it exists)?

For example, on the first machine I might have something simple like this running via task scheduler:

option batch on
option confirm off
open user:user@255.255.255.255
option transfer binary
put C:\import
%%% I would like a command to delete the files in C:\import here Smile %%%
close
exit

Can anyone tell me how I can accomplish this?

Thanks!
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
You can run the WinSCP script from batch file that will remove the local file after the WinSCP finishes. Next version of WinSCP will maybe support execution of local commands directly.
_________________
Martin Prikryl
Guest




prikryl wrote:
You can run the WinSCP script from batch file that will remove the local file after the WinSCP finishes. Next version of WinSCP will maybe support execution of local commands directly.


Thanks for your reply. I think that would be a useful feature for situations like this; I thought maybe it was possible and I was just overlooking it since local files can be deleted via the GUI...

Any idea of a good way of deleting these after they have tranferred via a batch file? I am concerned about them getting deleted before the upload completes. I supposed I could just have scheduler delete whatever is there the next morning; but if for some reason the upload failed (interrupted internet access, power outage, etc) I would be concerned that it might result in lost files.

Maybe this is just a concern I will have to deal with; but if you had something in mind I would appreciate it if you would share it.

Thanks!
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
Quote:
Any idea of a good way of deleting these after they have tranferred via a batch file? I am concerned about them getting deleted before the upload completes. I supposed I could just have scheduler delete whatever is there the next morning; but if for some reason the upload failed (interrupted internet access, power outage, etc) I would be concerned that it might result in lost files.

As I've wrote before, run winscp from windows batch file (.bat) and once it successfully finishes (exit code 0), remove the local files using windows command 'del'.
_________________
Martin Prikryl
Guest




Thanks for the addional help, I understand what you are saying about accomplishing my goal via batch file, but I guess I just do not have the knowledge of HOW to do it.

Something like this?

@ECHO OFF
CD\PROGRA~1\WINSCP3
WINSCP3 /CONSOLE /SCRIPT=UPLOAD.TXT UPLOAD
IF ERRORLEVEL 0 GOTO SUCCESS
IF ERRORLEVEL 1 GOTO FAILURE

:SUCCESS
ECHO FILES SUCCESSFULLY UPLOADED!
DEL C:\FILES\*.tif
GOTO END

:FAILURE
ECHO ERRORS DURING TRANSFER. FILES _NOT_ UPLOADED.

:END

This didn't work (because I don't know what I am doing I suspect), I believe the problem is that I don't know enough about exit/error codes and how to implement them properly into a batch file to do this.

I realize this is not your fault or problem; but if you know right off, I would appreciate it if you could elaborate. If not, thanks anyway for the help offered already.
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
The first problem I see is that "IF ERRORLEVEL 0" is always true as the "IF ERRORLEVEL N" succeeds when "the last application returned exit code N or greater". Just try to swap order of the two "IF ERRORLEVEL" commands. Let me know if it does not help.
_________________
Martin Prikryl
Guest




Thanks for your reply. I have done as you suggested (unless I misunderstood you), so the batch would now be:

@ECHO OFF
CD\PROGRA~1\WINSCP3
WINSCP3 /CONSOLE /SCRIPT=UPLOAD.TXT docstar
IF ERRORLEVEL 1 GOTO FAILURE
IF ERRORLEVEL 0 GOTO SUCCESS

:SUCCESS
ECHO FILES SUCCESSFULLY UPLOADED!
DEL C:\import\*.tif
GOTO END

:FAILURE
ECHO ERRORS DURING TRANSFER. FILES _NOT_ UPLOADED.

:END

However, if the DSL line is out, I receive a message, "Copying files to remote side failed....session 'upload' failed....No session." At this point, the batch file continues on to delete the client side files and display "FILES UPLOADED SUCCESSFULLY!"
So, it would seem that for whatever reason (probably an error in my batch file) that an error code is not being picked up and a branch is not occuring as I would like.

Thanks again for your assistance!
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
I have tried your batch file on following dummy script without opening session first, so that "upload" fails with "no session":
Code:
option batch on
upload xxx
exit

And it works as expected. Can you try this simple case?

BTW, what version of WinSCP are you using? Error exit code is returned only since version 3.7.1. Also does your command winscp3 (in bach file) execute winscp3.exe or winscp3.com? If winscp3.com, you need to use at least 3.7.3.
_________________
Martin Prikryl
Guest




Excellent, Great! Sometimes it is the simple things, eh? I was using version 3.7.1, so that was the problem. I installed 3.7.4 and it worked just as expected.

Thanks so much for your assistance with this issue, I think it has been resolved!
Loas

Guest


Guys, it is possible to remove a local file in the script?

Thanks in advance.


Loas
saikrishna

Guest


Hi,

I have requirement that i need to upload files(two or four) from unix(SFTP Serevr) and windows machine, after successful transfer, we need to remove the local files.

As i am very new to this, Could you please suggest and provide sample code.

Thanks & Regards
Saikrishna
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
saikrishna wrote:
I have requirement that i need to upload files(two or four) from unix(SFTP Serevr) and windows machine, after successful transfer, we need to remove the local files.

As i am very new to this, Could you please suggest and provide sample code.

This topic covers your question. What do you need more?

See also this article, which covers similar topic:
https://winscp.net/eng/docs/script_local_move_after_successful_upload
_________________
Martin Prikryl
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
saikrishna wrote:
I have requirement that i need to upload files(two or four) from unix(SFTP Serevr) and windows machine, after successful transfer, we need to remove the local files.

As i am very new to this, Could you please suggest and provide sample code.

This topic covers your question. What do you need more?

See also this article, which covers similar topic:
https://winscp.net/eng/docs/script_local_move_after_successful_upload
_________________
Martin Prikryl
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License