简体   繁体   中英

WinSCP .NET assembly - GetFiles root directory non-recursive (without subdirs)

I'm trying to download files from a directory, without the other directories in the wanted directory.

I've searched over the Internet for an answer, and the only thing I found is to use FileMask "|*/" in TransferOptions , which isn't working, and downloads nothing.

Using the latest version (5.7.5)

TransferOptions t = new TransferOptions { FileMask = "|*/" };

session.GetFiles("/", @"C:\bla", false, t);

Your code to exclude subdirectories is correct.

See also WinSCP FAQ How do I transfer directory non-recursively?

TransferOptions transferOptions = new TransferOptions();
transferOptions.FileMask = "|*/";

session.PutFiles(@"d:\toupload\*", "/home/user/", false, transferOptions);

It's an equivalent to your code.


You have other problems in your code.

  1. You ask WinSCP to download the root directory, yet to exclude all directories. So nothing is downloaded. You need to ask to download all files in the root directory: /* .

    Quoting the documentation for the remotePath parameter of the GetFiles :

    Full path to remote directory followed by slash and wildcard to select files or subdirectories to download. To download all files in a directory, use mask * .

  2. I assume that the C:\\bla is a target directory. So you have to tell WinSCP to download the files to the directory by appending a backslash: C:\\bla\\ . Otherwise WinSCP will try to save all files to C:\\ with name bla . Failing, if there's already a directory with that name. Or overwriting the files one with another, if there's not.

    Documentation for the localPath parameter of the GetFiles :

    Full path to download the file to. When downloading multiple files, the filename in the path should be replaced with operation mask or omitted ( path ends with backslash ).

  3. You should check for errors, either by inspecting the returned TransferOperationResult or by calling the .Check() directly.

So the correct code is:

TransferOptions t = new TransferOptions { FileMask = "|*/" };

session.GetFiles("/*", @"C:\bla\", false, t).Check();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM