代码之家  ›  专栏  ›  技术社区  ›  user2804628

检查是否存在具有不同扩展名的文件?

  •  1
  • user2804628  · 技术社区  · 10 年前

    我从photoshop开始 .tif 文件我运行一个脚本,添加一些层等,然后将文件保存为 .psd 在新文件夹中。

    我遇到的问题是检查 .psd(磅/平方英寸) 同名文件已存在。我的目标是简单地关闭 .tif格式 如果 .psd(磅/平方英寸) 具有相同名称的。

    这是我的保存代码:

    //Save document
    var savePath = Folder(doc.path.parent) + "/new_folder/";
    saveFile = new File(savePath);
    saveOptions = new PhotoshopSaveOptions;
    saveOptions.embedColorProfile = true;
    if ( WHAT SHOULD I BE ASKING HERE? ) {
        doc.saveAs(saveFile, saveOptions, false, Extension.LOWERCASE);
    } else {
        doc.close(SaveOptions.DONOTSAVECHANGES);    
    }
    

    我被添加到 if 作用我试过了 .exists 但它不工作,因为当前文件仍在 .tif格式 模式,尚未保存到 .psd(磅/平方英寸) 然而所以它只是继续保存并覆盖先前保存的 .psd(磅/平方英寸)

    欢迎任何帮助。:)

    编辑:

    我以为我已经做好了,但还是没有运气:

    //Strip .tif and add .psd to file name
    var docName = doc.name;
    PSDName = docName.substr(0,docName.length-3);    
    PSDName = PSDName + "psd";  
    
    
    //Save document
    var savePath = Folder(doc.path.parent) + "/new_folder/";
    saveFile = new File(savePath);
    saveOptions = new PhotoshopSaveOptions;
    saveOptions.embedColorProfile = true;
    var savedFile = savePath + "/" + PSDName
    if (! savedFile.exists ) {
        doc.saveAs(saveFile, saveOptions, false, Extension.LOWERCASE);
    } else {
        doc.close(SaveOptions.DONOTSAVECHANGES);
    
    }
    

    这个 如果 语句每次都返回false,文档未保存。如果我拿走 ! 它每次都能节省时间。

    1 回复  |  直到 10 年前
        1
  •  1
  •   Mark Setchell    10 年前

    使用要测试的文件名(即 .PSD 文件并使用它。例如,剥离 TIF 并将其替换为 PSD 然后使用 .exists .

    var ImageName = activeDocument.name;
    PSDName = ImageName.substr(0,ImageName.length-3);    // Strip "TIF" from end
    PSDName = PSDName + "psd";                           // Add on "PSD" instead
    

    如果需要调试脚本,可以执行以下操作:

    // Change Debug=1 for extra debugging messages, Debug=0 for no messages
    var Debug=1;
    ...
    if(Debug)alert(PSDName);
    ...
    if(Debug)alert("File exists");