Batch files are a nice and simple way on Windows to create one click solutions for tasks that you do on a regular basis. In my case, I wanted to find a quick and simple way to create a file I create on a daily basis and add the current date on it when I create it at the same time, of course as quick as possible.
I have a master file I want to copy it from, so with that I do:
COPY “C:\Users\Savas\Desktop\Testfolder\masterfile.txt” /y
ren “masterfile.txt” “File1 – %date:/=.%.txt”
There’s obviously many ways to do this above so you can choose your way suited for you.. The result will basically be creating a file in the same folder as the .BAT file I created, it will name the file “File1 – date.txt” Because my computer date is set to 27/04/2015 standard (you can find this out by simply doing “echo %date%” and the result should tell you your date format. Simple!
Now what about yesterdays date, as far as I can find, there doesn’t seem to be a simple one line way to just do yesterday date.. So we have to set some variables and have the batch file get the date in DD format then minus off one day and then join the date back together.
@set /a c=%dd%-%b%
COPY “C:\Users\Savas\Desktop\Testfolder\testfile.txt” /y
ren “testfile.txt” “File 2 – %c%.%mm%.%yyyy%.txt”
The %dd% takes the date and then takes off all, but the DD format. So instead of having 27/04/2015, we end up with just 27. Then “@set /a c=%dd%–%b%” line simply takes the number 27 and minuses the number in the “@set b=1” variable, and in this case it’s “1” so we end up with the result in %c% declaration. Put the month and year and we have the date for yesterday!
Of course if you’re using these batch files on separate systems and these systems have different date formats, you’re going to reach some minor issues and that means you’ll need to be more specific with your date formatting. In my case, 6,4% takes a sub string, starting at position 8, and keeping 4, which is 6,4.
In my case, I don’t have this issue, I don’t need my batch file to be some complicated and long winded method to pull the date and when I create my file, I will see if the date is wrong immediately. I hope this helped.