Topic "How to move file using winSCP when file exist?"

Author Message
suravi
[View user's profile]

Joined: 2014-01-23
Posts: 2
Location: srilanka
I'm developing sftp file transferring project using winSCP (C#). i want to move files in server.here is the code.

Code:
session.MoveFile(server.RemoteDownloadPath + fileInfo.Name, server.DoneFilePath);
Console.WriteLine("Move File {0} to {1}", fileInfo.Name, server.DoneFilePath);


if current moving file exist in target path it not move.so i want to rename file and move it. is there any way to do that?
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
My answer to your question on stackoverflow.com:

Quote:
If you are asking if WinSCP can do this automatically for you, the answer is "no".

You need to explicitly check target file existence and rename it before you move the file.

Note that rename and move is the same operation from WinSCP .NET Assembly API perspective. So you use Session.MoveFile method for both:
http://winscp.net/eng/docs/library_session_movefile

From your question, it's not clear if server.DoneFilePath is path to target directory (ending with slash /) or part to target file (ending with file name). Assuming is is path to target directory only:

Code:
string doneFile = server.DoneFilePath + fileInfo.Name;
if (session.FileExists(doneFile))
{
    session.MoveFile(doneFile, doneFile  + ".bak");
}

// carry on with your move code:
session.MoveFile(server.RemoteDownloadPath + fileInfo.Name, server.DoneFilePath);


http://stackoverflow.com/questions/21300315/how-to-move-rename-file-using-winscp-net-assembly-when-target-file-exist-alread
suravi
[View user's profile]

Joined: 2014-01-23
Posts: 2
Location: srilanka
Yes than you.


prikryl wrote:
My answer to your question on stackoverflow.com:

Quote:
If you are asking if WinSCP can do this automatically for you, the answer is "no".

You need to explicitly check target file existence and rename it before you move the file.

Note that rename and move is the same operation from WinSCP .NET Assembly API perspective. So you use Session.MoveFile method for both:
http://winscp.net/eng/docs/library_session_movefile

From your question, it's not clear if server.DoneFilePath is path to target directory (ending with slash /) or part to target file (ending with file name). Assuming is is path to target directory only:

Code:
string doneFile = server.DoneFilePath + fileInfo.Name;
if (session.FileExists(doneFile))
{
    session.MoveFile(doneFile, doneFile  + ".bak");
}

// carry on with your move code:
session.MoveFile(server.RemoteDownloadPath + fileInfo.Name, server.DoneFilePath);


http://stackoverflow.com/questions/21300315/how-to-move-rename-file-using-winscp-net-assembly-when-target-file-exist-alread
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