0 votes
by (120 points)

Hello, I'm trying to upload a file with a path that I need retained, so when the file is uploaded it would need to create any directories that do not already exist. Here are my sample attempts:

Rebex.Net.Sftp sftpClient = new Sftp();
sftpClient.Connect(myHost, 22);
sftpClient.Login(usr, pwd);

string source = @"c:\abc\123\test.wav";
string dest = @"abc/123/test.wav";
sftpClient.PutFile(source, dest);

Exception: No such file; No such file. (me: The source does exist)

sftpClient.Upload(source, dest, TraversalMode.Recursive);

Exception: Could not find target directory ('/abc/123/test.wav').

According to the documentation I think that one of these should work but neither does, I just get the exceptions listed above.

If I copy the file to the current directory as in:

string source = @"c:\abc\123\test.wav";
string dest = @"test.wav";
sftpClient.PutFile(source, dest);

This works.

How can I go about doing what I need to do? Do I need to create a function that checks for each directory and creates it if needed?


1 Answer

0 votes
by (18.0k points)

It seems the target directory "abc/123" exists? Neither the PutFile nor the Upload method create target directories.

Note: The Upload method with the Traversal.Recursive creates directories only as a part of transfered folder structure.

Your code should look like this:

string sourceFilePath = @"c:\abc\test.wav";
string targetPath = @"abc";
string targetFilename = "test.wav";

// if the target path were deeper (e.g. 'abc/123'),
// you should create the directory individually for the each level
// (e.g. CreateDirectory('abc'); CreateDirectory('abc/123');
if (!sftpClient.DirectoryExists(targetPath))

sftpClient.PutFile(sourceFilePath, targetPath + "/" + targetFilename);
//sftpClient.Upload(sourceFilePath, targetPath);