在 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
bool 值不是函数
Boolean exists Read only Property
If true, this object refers to a file or file-system alias that actually exists in the file system.
更新:实际上“神秘客人先生”是对的(见评论)。
File('~/Desktop/does-not-exist.txt').exists
即使文件不存在,在 macOS 10.12.2 和 PS CC2017 上也会返回 true。使用
new File('~/Desktop/does-not-exist.txt').exists
似乎工作正常。
更新2:
此错误似乎是 Photoshop 特有的问题。在 ESTK 和 InDesign 中 File('~/Desktop/does-not-exist.txt').exists
返回 false
关于javascript - 判断文件是否存在或者是否为0字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42023886/