2

I am working on a project where I am using WinSCP .NET component for accessing an SFTP host. The functionality of the code is to navigate to a folder in the SFTP and checks whether the provided file name is located inside the folder.

I have the below code and everything is working fine. The below code is checking whether the folder exists in the SFTP remote path. If it's there I am iterating through all the files in the sFTP directory to check whether it has the filename that I provided.

string FolderName = row["Folder_Name"].ToString();
string FileName = row["File_Name"].ToString();

FileName = FileName.Replace("\r\n", string.Empty);

string remotePath = HomeDirectory + FolderName + "/";

if(session.FileExists(remotePath)) 
{
    RemoteDirectoryInfo directory = session.ListDirectory(remotePath);
    sFTPFiles.AddRange(directory.Files
        .Where(fileInfo => !fileInfo.IsDirectory)
        .Select(fileInfo => fileInfo.Name.ToString()));
    if(CheckFileExists(FileName, sFTPFiles) != FileExistence.EXISTS)
    {
        isDiscrepancy = true;
    } else
    {
        row["sftp_available"] = true;
        row["discrepancy_on_sftp"] = false;
    }
}

The problem that I am facing is the folder name case sensitive issues. In my database the folder names are stored in Upper case. But in the SFTP the same folder names has different case sensitive name ie. Upper Case or Sentence case or Lower Case. Something like folderNAME, FOLDERNAME, foldername, fOLDERNAME

When the if(session.FileExists(remotepath) is getting executed, it gets failed due to the case sensitive issue. When my Foldername that I am verifying is FOLDERNAME and the same in SFTP is written as FOLDERname it is getting skipped due to case sensitivity.

Hence can anyone let me know how to check the folder names with non case sensitive method using WinSCP and C#. All I need is if my Foldername in my database is FOLDERNAME, I want to navigate inside that folder in SFTP even though it is in any of the mentioned formats.

Thanks in advance for your help.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Karthik Venkatraman
  • 1,619
  • 4
  • 25
  • 55

1 Answers1

0

The SFTP file checking API follows the conventions of the remote system. So if the remote system is case-sensitive, the check will be case-sensitive. You cannot do anything about it.

All you can do is to retrieve complete remote directory listing and do case-insentitive search yourself.


For quick and dirty case-insensitive replacement of Session.FileExists, use:

if (session.ListDirectory(HomeDirectory).Files.Any(
        file => file.Name.Equals(FolderName, StringComparison.OrdinalIgnoreCase)))
{
    // ...
}

Though obviously, this will be ineffective, if you are going to check lot of files/subfolders in the same folder. It that case, you better cache the listing and reuse it.

RemoteDirectoryInfo dirInfo = session.ListDirectory(HomeDirectory);
if (dirInfo.Files.Any(
        file => file.Name.Equals(FolderName, StringComparison.OrdinalIgnoreCase)))
{
    // ...
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992