
When %date% is used in a batch file, it displays the date in the following format: Sun. This example also has a - ( hyphen) in-between each token to separate the month, day, and year in the file name. rename "hope.txt" %%e-%%f-%%g.txt - Rename the file "hope.txt" to the tokens e,f, and g with a.The rename command can be substituted for anything else. in ("%date%") - The data, which is the %date% (date) of the computer.Since there are 5 tokens in this example it would be d,e,f,g, and h. %%d - The beginning character used for the token.
Delims is short for delimiters and break up the date in this example, the / ( forward slash) and a space (space before the quote).
"tokens=1-5 delims=/ " - How many tokens the incoming data (the date) will be broken into 1-5 is five different tokens. for /f - The for command and the /f switch. Date for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "hope.txt" %%e-%%f-%%g.txtīelow is a breakdown of the above command and what it all means. Each of the for commands listed in this page would be placed into a batch file. The example below shows how you can use the date command in the for command to extract the current date and use that data to rename the file. The perl rename utility is effectively a specialised scripting language that allows you to use ANY perl code to rename files, from simple s/search/replace/ regular expression operations (which suffices for most renaming tasks) to complex multi-line scripts.Į.g.There are a few different methods of how this can be done. Like any batch file renaming operation, this should be done with the perl rename utility, not with some klunky shell for loop. My original answer above still works, but the updated answer below is better. My answer to this question has been bothering me for years (well, only when I remember it - on days like today when it gets another upvote) so I'm finally updating it. Here's a version of goldschrafe's one-liner that:Ĭorrectly copes with any spaces in the filenamesĪlso copes with filenames beginning with a dashįor f in * do mv - "$f" "$f-$(date -r "$f" +%Y%m%d)" done