Search code examples
filedatetimeautoit

How to write date and time to a file name?


My AutoIt script doesn't work. It should rename files by actual date and time.

#include <Date.au3> 
#include <File.au3>
FileMove("C:\file.csv", "C:\file" & _NowDate() & _NowTime() & ".csv")

There is no syntax error, but the file isn't renamed. I think Windows rejects symbols like : or /. How to convert time to this format: hh-mm-ss_dd-mm-yyyy?


Solution

  • you can sanitize the output inline

    #include <Date.au3>
    FileMove("file.csv", "file" & stringreplace(_NowTime(5) , ":" , "-") & "_" & 
    stringregexpreplace(_NowCalcDate() , "(\d\d\d\d)/(\d\d)/(\d\d)" , "$3-$2-$1") & 
    ".csv")