dir
列出 SFTP 或 FTP 服务器上的文件夹内容
说明
示例
列出文件夹的内容
通过创建 SFTP 对象打开与 SFTP 服务器的连接。使用 SFTP 对象下载文件并列出服务器上子文件夹的内容。
首先,连接到示例 SFTP 服务器。
s = sftp("sftp.example_galapagos.net","maria.silva")
SFTP with properties: Host: "sftp.example_galapagos.net" Username: "maria.silva" Port: 22 ServerSystem: "unix" DatetimeType: "datetime" ServerLocale: "en_US" DirParserFcn: @matlab.io.ftp.parseDirListingForUnix RemoteWorkingDirectory: "/home/maria.silva"
列出顶层文件夹的内容。
dir(s)
air_quality fish insects README.txt birds INDEX.txt mammals reptiles climate index.html rainfall sftp.html
使用 dir
函数列出子文件夹的内容。
dir(s,"home/maria.silva/birds")
albatrosses ducks herons parrots avocets_stilts falcons kingfishers pelicans barn_owls flamingos mockingbirds penguins blackbirds frigatebirds nightjars pheasants boobies grebes northern_storm_petrels pigeons cardinal grosbeaks guineafowl osprey plovers cormorants gulls owls rails cuckoos hawks oystercatcher sandpipers
使用 cd
函数切换到子文件夹。cd
的输出是 SFTP 服务器上当前文件夹的路径,而不是当前 MATLAB 文件夹的路径。
cd(s,"home/maria.silva/birds/herons")
ans = "home/maria.silva/birds/herons"
列出当前文件夹的内容。
dir(s)
documentation great_egret_data migration_patterns great_blue_heron_data green_heron_data nesting_behaviors
解析 LIST 命令输出
解析 SFTP 服务器的 LIST
命令的原始输出。
首先,连接到示例 SFTP 服务器。
s = sftp("sftp.example_galapagos.net","maria.silva")
SFTP with properties: Host: "sftp.example_galapagos.net" Username: "maria.silva" Port: 22 ServerSystem: "unix" DatetimeType: "datetime" ServerLocale: "en_US" DirParserFcn: @matlab.io.ftp.parseDirListingForUnix RemoteWorkingDirectory: "/home/maria.silva"
列出服务器上当前文件夹的内容。将输出结构体赋给变量 a
。
a = dir(s)
a=12×1 struct array with fields:
name
isdir
bytes
date
datenum
解析 LIST 命令的原始输出。将输出字符串赋给变量 b
。
b = dir(s,"ParseOutput",false)
b = 17x1 string
"lrwxrwxrwx 1 sftp 32 Nov 05 2014 air_quality"
"-rw-r--r-- 32 sftp 1516 Mar 08 2017 birds"
"-rw-rw-r-- 1 sftp 3766 Apr 18 2020 climate"
"drwxr-xr-x 36 sftp 31 Jun 04 2016 fish"
"drwxr-xr-x 1 sftp 52 Oct 08 2009 INDEX.txt"
"drwxr-xr-x 1 sftp 75 Jan 12 2015 index.html"
"drwxrwsr-x 5 sftp 673 Jul 02 2018 insects"
"drwxrwsr-x 3 sftp 2 Jun 11 2017 mammals"
"-rw-rw-r-- 2 sftp 9036 Apr 08 2014 rainfall"
"drwxrwsr-x 1 sftp 11 Jan 10 2019 README.txt"
"-rw-rw-r-- 3 sftp 43 Sep 14 2021 reptiles"
"drwxrwxr-x 1 sftp 5328 Oct 02 2009 sftp.html"
列出内容的详细信息
列出 SFTP 服务器上内容的详细信息。dir
函数可以返回结构体数组,其中包含指定文件夹中每一项的名称、修改日期和大小。
首先,连接到示例 SFTP 服务器。
s = sftp("sftp.example_galapagos.net","maria.silva")
SFTP with properties: Host: "sftp.example_galapagos.net" Username: "maria.silva" Port: 22 ServerSystem: "unix" DatetimeType: "datetime" ServerLocale: "en_US" DirParserFcn: @matlab.io.ftp.parseDirListingForUnix RemoteWorkingDirectory: "/home/maria.silva"
返回 SFTP 服务器上顶层文件夹中各项的详细信息。有些项是文件,有些是文件夹。
listing = dir(s)
listing =12×1 struct array with fields:
name
isdir
bytes
date
datenum
显示当前文件夹(名为 air_quality
的文件夹)中第一项的详细信息。
listing(1)
ans = struct with fields:
name: 'air_quality'
bytes: 64
isdir: 1
date: '05-Nov-2014 00:00:00'
datenum: 735908
输入参数
s
— 连接到 SFTP 或 FTP 服务器
SFTP 对象 | FTP 对象
与 SFTP 或 FTP 服务器的连接,指定为 SFTP 对象或 FTP 对象。
folder
— 目标文件夹的名称
字符向量 | 字符串标量
SFTP 或 FTP 服务器上的目标文件夹的名称,指定为字符向量或字符串标量。要指定当前文件夹的上一级文件夹,请使用 '..'
。
tf
— 以字符串数组形式返回原始 LIST 命令输出
true(默认值) | false
以字符串数组形式返回原始 LIST 命令输出,指定为 true 或 false。
输出参数
listing
— 内容属性
结构体数组
内容属性,以 m
×1 结构体数组形式返回,其中 m
是文件夹中的项目数。
下表显示了结构体中的字段。
字段名称 | 描述 | 数据类型 |
---|---|---|
name | 文件或文件夹名称 |
|
bytes | 以字节为单位的项目大小 |
|
isdir | 如果 |
|
date | 修改日期时间戳 |
|
datenum | 修改日期是一个日期序列值。 |
|
提示
dir
函数可能会返回结构体数组,其中最后四个字段为空或缺失。当dir
返回信息缺失的结构体时,可能意味着没有为 SFTP 或 FTP 服务器上运行的操作系统配置 SFTP 或 FTP 对象。默认情况下,会配置一个 SFTP 或 FTP 对象,用于连接到运行 UNIX® 操作系统的服务器。
版本历史记录
在 R2006a 之前推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)