Batch scripting is a text file containing a series of commands intended to be executed by the command interpreter. Much like a unix / linux shell script, the purpose of a batch script is to sequentially process a series of commands.

Tags | |

Using a batch file to calculate dates

Batch Tutorial: Calculating dates in a batch script



Subtracting 1 year from today example:



@echo off

echo Todays date is %date:~4,2%-%date:~7,2%-%date:~10,4%

echo one year back...

set /a yearback=%date:~10,4%-1

Echo is %date:~4,2%-%date:~7,2%-%yearback%

pause



Here is what you can do with this. Say you have a "Junk" folder and you only want keep items for 1 year. Using the above method you can subtract a year in the batch file and then using the xcopy and delete commands copy all old files to a temp folder and delete them all. I have provided an example below of this.




@echo off

Echo #####################

Echo This tool will move old files to a temp folder for review

Echo #####################

set /a yearback=%date:~10,4%-1

Xcopy C:\old_folder\* /s /d:%yearback%-%date:~7,2%-%date:~10,4% C:\review_folder\

pause

exit



Comments

Feel free to leave a comment or question

Name: (Optional but appreciated):

Comment:

Use [code] [/code] for code block style



Security Code: