我正在使用here中描述的FindFirst / FindNext函数读取各种文件和目录。
我唯一的问题是,我不能确定这个文件是否是符号链接。在文件属性中,没有常量或标志,我也找不到用于测试符号链接的函数。
发布于 2012-12-06 10:33:57
你最初使用findfirst的想法是最好的,因为它是一个可移植的解决方案(windows现在也有符号链接)。唯一需要适应的是在传递给findfirst的属性中请求符号链接检查:
uses sysutils;
var info : TSearchrec;
begin
// the or fasymlink in the next file is necessary so that findfirst
// uses (fp)lstat instead of (fp)stat
If FindFirst ('../*',faAnyFile or fasymlink ,Info)=0 then
begin
Repeat
With Info do
begin
If (Attr and fasymlink) = fasymlink then
Writeln('found symlink: ', info.name)
else
writeln('not a symlink: ', info.name,' ',attr);
end;
Until FindNext(info)<>0;
end;
FindClose(Info);
end.
发布于 2012-12-05 12:20:17
您可以从BaseUnix使用fpstat:
像这样的东西
uses baseUnix;
var s: stat;
fpstat(filname, s);
if s.st_mode = S_IFLNK then
writeln('is link');
这也为您提供了许多有关该文件的其他信息(时间、大小...)
发布于 2012-12-05 14:12:53
函数fpLStat就是答案:
var
fileStat: stat;
begin
if fpLStat('path/to/file', fileStat) = 0 then
begin
if fpS_ISLNK(fileStat.st_mode) then
Writeln ('File is a link');
if fpS_ISREG(fileStat.st_mode) then
Writeln ('File is a regular file');
if fpS_ISDIR(fileStat.st_mode) then
Writeln ('File is a directory');
if fpS_ISCHR(fileStat.st_mode) then
Writeln ('File is a character device file');
if fpS_ISBLK(fileStat.st_mode) then
Writeln ('File is a block device file');
if fpS_ISFIFO(fileStat.st_mode) then
Writeln ('File is a named pipe (FIFO)');
if fpS_ISSOCK(fileStat.st_mode) then
Writeln ('File is a socket');
end;
end.
打印输出:
test_symlink
File is a link
test
File is a directory
https://stackoverflow.com/questions/13722613
复制相似问题