Why does 'dir' return an empty structure for specific FTP servers?

13 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2022 年 6 月 22 日
回答済み: MathWorks Support Team 2022 年 9 月 15 日
I am trying to fetch file names, as a structure, from an FTP object using the "dir" command. Why is the resulting structure "allFiles" empty?
% generic server name
fserver = 'ftp.host.domain';
% open the FTP
f = ftp(fserver);
% go to the folder where the file is located
f_dir = cd(f,'/location/');
% look at all files & get their names
allFiles = dir(f_dir);

採用された回答

MathWorks Support Team
MathWorks Support Team 2022 年 6 月 22 日
This can occur if the specific FTP server returns an extra line at the start of the directory listing e.g. "total x".
To work around this issue please try the following steps:
1. Define a custom "dir" parser function:
function names = myCustomDirParser(entries, serverLocale)
  % Ignore the first line in the dir output.
  entries(1) = [];
  entries = join(entries, newline);
  % Read the rest of the entries using the normal FTP Unix dir parser.
  import matlab.io.ftp.parseDirListingForUnix
  names = parseDirListingForUnix(entries, 1, false, "unix", serverLocale);
end
2. Implement the function when fetching file names:
fserver = 'ftp.host.domain';
f = ftp(fserver, DirParserFcn=@myCustomDirParser);
allFiles = dir(f, '/location/');

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by