2013-05-14 64 views
0

我想根据数据。如果文件创建文件创建文件存在今天的日期的话,就必须不能创造。我每次都写了一些代码如何根据日期

path.exists('./LOG/BAlDate-Info' +Date()+'.txt', function(exists) { 
    if (!exists) { 
     fs.writeFile('./LOG/BALDate-Info' +Date()+'.txt',"BAl-Employee is called @ "+ Date(),function(err){ 
      if(err){ 
       console.log("error is: " + err) 
      } 
      else 
       console.log("no error found"); 
     }); 
    } 
}); 

在这种情况下,文件I运行这个代码它创建文件,但我想创建今天的日期单一文件。 请帮我在这种情况下

回答

2

一种选择是命名您的日志文件是这样的:

var now = new Date(); 
var logfile_name = './LOG/BALDate-Info-' + now.getFullYear() + "-"+ now.getMonth() + "-" + now.getDay() +'.txt' 

你的文件应该像” ./LOG/BALDate-Info-2013-4-2。 txt'

+0

感谢它正在工作... @sptirca – ana 2013-05-14 07:31:37