简体   繁体   English

判断文件是否是FTP服务器上的目录

[英]Tell if a file is a directory on an FTP server

I am writing a c++ program that interfaces with an Apache FtpServer using libcurl. 我正在编写一个使用libcurl与Apache FtpServer接口的c ++程序。 I was originally using the LIST command to get the contents of a directory but it was giving me a lot of information I didn't but had to parse any ways which lead to a lot unneeded overhead (especially when I was working with hundreds of thousands of files). 我本来是使用LIST命令来获取目录的内容的,但是它却为我提供了许多我没有的信息,但是不得不解析任何导致很多不必要开销的方法(尤其是当我与成千上万的人一起工作时)文件)。 In addition I needed a valid time stamp and it was giving me a shorthand that didn't include the year (so on January 1 all of the files on my computer looked outdated compared to the FTP server's). 另外,我需要一个有效的时间戳,这给了我一个不包括年份的简写(因此,在1月1日,与FTP服务器相比,我计算机上的所有文件都显得过时了)。 My solution was to use the NLST command to get only the names, then download the timestamps of each using MDTM . 我的解决方案是使用NLST命令仅获取名称,然后使用MDTM下载每个名称的时间戳。 This worked awesome but then I ran into the major problem of not being able to tell if a file was a directory or not. 这很棒,但是后来我遇到了一个主要问题,即无法分辨文件是否是目录。

I am thinking the easiest way to do this is using the permissions to see if the first flag is set to d . 我在想最简单的方法是使用权限来查看第一个标志是否设置为d FTP doesn't appear to have this functionality. FTP似乎没有此功能。 Is there an easy way to tell if a filename is a directory or file? 有没有一种简单的方法来判断文件名是目录还是文件?

You can try to use the CWD command on the file to test if it is a directory or not. 您可以尝试在文件上使用CWD命令来测试它是否是目录。 But failure may mean lack of permission, so you need to check the error code. 但是失败可能意味着缺少权限,因此您需要检查错误代码。 For example: 例如:

ftp> cd atom.xml
550 Can't change directory to atom.xml: Not a directory

Alternatively, you can use the NLST command again on the file you want to test. 或者,您可以在要测试的文件上再次使用NLST命令。 If it is a plain file, you will just get the filename back. 如果它是纯文件,则只需取回文件名。 Otherwise, you will get a list of contents of the directory. 否则,您将获得目录内容的列表。

ftp> nlist atom.xml
200 PORT command successful
150 Connecting to port 53912
atom.xml
226 1 matches total

ftp> mkdir foo
257 "foo" : The directory was successfully created
ftp> nlist foo
200 PORT command successful
150 Connecting to port 53928
226 0 matches total

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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