This will work as a standalone batch file or as a function tacked onto the end of your own batch file and executed with a CALL statement.Īs an external function, you might CALL it from within your own batch file like this:įor /f %%a in ('tomorrow') do set tomorrow=%%aĪs a function added to your own code, you might do something like this:įor /f %%a in ('call :tomorrow') do set tomorrow=%%a Set d=0%d% set m=0%m% echo %d:~-2%/%m~-2%/%y%Īs it stands, the batch file's only output is a date in D/M/YY format (or DD/MM/YY if you have added the leading zeros). ![]() For example, the following should do the job quite nicely: If you want to output this as DD/MM/YYYY then you will need to add the leading zeros yourself. Notice the order in which D, M and Y are assembled - they match the input.Īlso note that either D or M may consist of one or two digits. No real need to assign it to a variable such as tomorrow although you can if you wish to. If, on the other hand, the date is invalid, the ' ||' ( logical OR operator) passes control to the GOTO instruction and the process is repeated by jumping back the beginning of the loop. If the date in ' /d:' is valid and resolvable, then XCOPY continues ( to do 'nothing') and control is passed to the next line of code in the batch file. ![]() The same principle is then applied to the month value should it exceed 12.įor information on validating dates using the XCOPY command please see my article: Advanced Batch File Programming: Validating Dates In all cases, should the day value exceed the maximum range a day may reach - which is 31, it is reset to '1' and the month value is incremented. In the worst-case scenario, where February 28 falls on a non-leap year, the loop will execute just four times to wit: 28+1 is invalid, 29+1 is invalid, 30+1 is invalid, 31+1 becomes 1, which is valid). If today is the last day in the month, then: If there are 30 days in the current month, the loop will execute twice. The majority of times, the loop will execute just the once. The trick is in the end-of-month handling. If the resultant date is valid, it is returned to the user. Basically, '1' is added to the current day value. This is because leading zeros are trimmed off during arithmetic assignment. When assigning values to variables, we use using SET /A (rather than just SET) to overcome the problem of attempting to perform octal arithmetic on '08' and '09' later on. You are reminded: This is an advanced topic and therefore, I would expect you to know how to do that. ![]() If your system's date format differs from DD/MM/YYYY then you will need to edit these three lines or provide some other method of delivering the date to the batch file in order for the rest of the code to work correctly. The code receives input from the system's %DATE% whose format is DD/MM/YYYY. The input section is pretty straightforward. The crux of this batch file revolves around the XCOPY command - a technique I discovered while looking for an easy method for validating dates that avoids a series of complex mathematical procedures. ![]() TOMORROW.BAT is inspired by a question I get asked over and over again that is, " How can I use batch file commands to obtain tomorrow's date?"
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |