确定文件是否存在或为 0 字节

Determine if file exists or is 0 Bytes

在 Photoshop 中,我可以读取文本文件。

function does_file_exist(f)
{
    var lines = "";
    var aFile = new File(f);
    aFile.open("r");
    while(!aFile.eof)
    {
     var line = aFile.readln();
     if (line != null && line.length >0)
     {
        lines += line + "\n";
     }
    }
    aFile.close();

    if (lines.length == 0)
    {
        alert(f + "\ndoes not exist!");
        return false;
    }
    else
    {
        var trunc = lines.substring(0,256);
        alert(f + " exists!\nHere's proof:\n\n" + trunc + "...")
        return lines;
    }
}

如果返回的字符串长度为 0,我们假设该文件根本不存在。这工作正常,但是如果有一个 0 字节的空文件会发生什么?我可以访问文件大小属性吗?还是有另一种方法可以解决这个问题? file.exists()

似乎有问题
File(f).exists

布尔值不是函数

Boolean exists Read only Property
If true, this object refers to a file or file-system alias that actually exists in the file system.

更新: 其实 "Mr. Mystery Guest" 是对的(见评论)。

File('~/Desktop/does-not-exist.txt').exists

returns 在 macOS 10.12.2 和 PS CC2017 上对我来说是正确的,即使该文件不存在。使用

new File('~/Desktop/does-not-exist.txt').exists

似乎正常工作。

更新二:

这个错误似乎是 Photoshop 特有的问题。在 ESTK 和 InDesign 中 File('~/Desktop/does-not-exist.txt').exists returns false