SFTP GetFile supports case sensitive

0 votes
asked Aug 7 by RK (180 points)

Hi,

I am trying to copy a file from a SFTP location and paste in on a FTP location but the file name casing is causing a problem.
Eg: If the file name on the sftp location is with the name "Test_Sample Letter_123.pdf"
and if I use the Sftp.GetFile(..) method with the pdf name as "Test_sample letter_123.pdf" (s and l are small) then the method doesn't file the file. It only finds the file if the exact file name casing is passed.
Is there anyway by which I can get the pdf irrespective of the pdf name casing?
The GetFile should find the file irrespective of the casing.
Any suggestions or assistance will be of great help.

Regards
RK

Applies to: Rebex SFTP

1 Answer

+1 vote
answered Aug 7 by Lukas Pokorny (113,730 points)
selected Aug 12 by RK
 
Best answer

Hi, this seems to be the root of the issue:

The GetFile should find the file irrespective of the casing.

That is not actually how the method behaves. The GetFile method just takes whatever path it has been called with, and asks the server to retrieve a file from that particular path. So in practice, SFTP servers running on Unix-like systems are case-sensitive, while SFTP servers running on Windows are case-insensitive.

To achieve the behavior you prefer, you would first have to 'resolve' the supplied file path into a form recognized by the server. To do this, you would have to iterate through all files in the directory to find out if there is a case-insensitive match, and retrieve the 'canonical' name this way. For example:

// connect to the server
var sftp = new Sftp();
sftp.Connect("test.rebex.net");
sftp.Login("demo", "password");

// input path and file name
string path = "/pub/example";
string myFileName = "README.txt";

// try matching the supplied file name to an actual file name at the SFTP server
string fileName = myFileName;
foreach (string actualFileName in sftp.GetNameList(path))
{
    if (myFileName.Equals(actualFileName, StringComparison.CurrentCultureIgnoreCase))
    {
        // use the actual file name instead of input file name
        fileName = actualFileName;
        break;
    }
}

// download the file
sftp.GetFile(path + "/" + fileName, myFileName);

However, watch out for culture-related issues when comparing those strings. For example, for most users, "I" and "i" represent different casings of the same letter. But for a Turkish user, it's different: "I" is an uppercase of "ı", while "İ" is an uppercase of "i". Depending on your scenario, using InvariantCultureIgnoreCase instead of CurrentCultureIgnoreCase migth be a better option, as it produces consistent results.

commented Aug 7 by RK (180 points)
Awesome, that worked like a bomb... Thank you for your quick response..
asked Aug 20 by RK (180 points)
edited Aug 20 by RK
Faster SFTP to FTP download
...