LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

InetFTPGetDirList - how to distinguish between file and folder?

Hello,

 

I'm currently writing a small application, using CVI 9.0, that should download all files from an FTP server including all subdirectories.


For that I have written a recursion that steps through each directory and retrieves each file list using InetFTPGetDirList.

If it is a folder it steps into it further, if it's a file it downloads the file.

 

In theory this worked fine, until I tested it on another FTP server, because I could not distinguish between files and folders.

 

Server A returned a list that was like this:

./

../

dir1/

dir2/

file1.txt

file2.txt

 

Server B returned the list like this:

.

..

dir1

dir2

file1.txt

file2.txt

 

In the case of server A I could look for the '/' to declare this entry as a folder, but server B did not return the slashes. So if someone uploads a file without a file extension, I suddenly would think it was a folder instead of a file.


Is there anything I have missed to further distinguish the entries returned by the InetFTPGetDirList function?

 

So far the only idea I had is to try to switch into the directory using InetFTPChangeDir function and in case of error assume it's a file and try to download it with InetFTPRetrieveFile instead.

0 Kudos
Message 1 of 3
(3,277 Views)

You can issue a ChangeDir command on the file or directory. The call will succeed if it is a directory and fail if it is a file. If the call succeeds, you can change back to the parent directory. You will need to disable 'break-on-library-errors' around this call if you are running a debug build. See the DoChangeDir function in samples/internet/ftpclnt/ftpclnt.c.

0 Kudos
Message 2 of 3
(3,253 Views)

Hello Excaliburn,

 

I am not a FTP expert, but as far as I understand there is no way to tell from the string returned by FTP's NLST command if an item name is a file or a directory.

 

The standard says:

[...] The server will return a stream of names of files and no other information. [...]  ( https://tools.ietf.org/html/rfc959Page 32)

 

It is not specified that the server has to format directory names in a specific way.

 

I found some workarounds implemented in other languages using the same solution that you already pointed out(i.e. trying to cd into the item and see if that produces an error).

NLST seems not to be the ideal solution to list directories. There are extensions for FTP such as MLST and MLSD that provide detailed information for directory items, however these commands are not implemented in CVI's Internet Library and not all FTP server might understand them. However, they can still be used with the InetFTPCommand function provided in CVI.

 

It might be useful to download Filezilla, connect to ftp.ni.com and just observe the communication with the server to get an idea how the commands work.

 

Regards,

  Georg

 

 

 

0 Kudos
Message 3 of 3
(3,252 Views)