使用javascript表达式重命名文件名

时间:2014-02-06 20:36:00

标签: javascript regex replace split string-concatenation

我对javascript很新,我需要重命名或添加到css和.png文件的扩展名。该脚本嵌入在ETL过程中。我有一个变量" prd"它保存了style.css和picture.png文件派生自的文件名的值,我还需要在扩展的末尾添加日期或时间戳。基本上我想要连接prd+style_02_06_14.png

期望的结果:

Prd = sales_report   
File = style.css.      
Result = "sales_report_style_02_06_14.png" and "sales_report_style_02_06_14.css"

这是我的代码

var sourceCssFile = outputfolder + "style.css";
var destinationCssFile = outputfolder + css_pic;

if(isFolder(destinationCssFile) == false) {
    createFolder(destinationCssFile);
    var testvar = "inside";
}

destinationCssFile = destinationCssFile + "/style.css";


moveFile(sourceCssFile, destinationCssFile, true);

var sourceImageFile = outputfolder + "picture.png";
var destinationImageFile = outputfolder + css_pic + "/picture.png";
moveFile(sourceImageFile, destinationImageFile, true);

var cont = loadFileContent(output);
var replaceCss = css_pic + "tt+style.css";
var replaceImg = css_pic + "tt + picture.png";
cont = cont.replace("style.css", replaceCss);
cont = cont.replace("picture.png", replaceImg);
var filename = outputfolder + new_str;

1 个答案:

答案 0 :(得分:0)

试试这个:

function formatResult(prd, filename, date, extension) {
    return prd + '_' + filename + '_' + formatDate(d) + '.' + extension;
}

function formatDate(d) {
    var year = d.getFullYear().toString().substr(2,2);

    var month = (d.getMonth()+1) + '';
    if (month.length == 1) {
        month = "0" + month;
    }

    var day = d.getDate() + '';
    if (day.length == 1) {
        day = "0" + day;
    }

    return month + '_' + day + '_' + year;
}

演示

http://jsfiddle.net/WHpuU/5/

诺塔

alert( 'style.css'.replace(/\.css$/i, '') ); // shows 'style'