Using dates in a batch file


















Main part of this batch is done in the first three lines beginning with the command SET. We will analyze further first line:. Now we need just part of this output the date string for out variable named DAY. Also, we counted position of that information. We will now do the trick. The x and y are number position and length. We will indicate position of first character with value of X. In our example, this is 7. With second value we will indicate length of the substring.

In our example that will return We do the same for a month and the year. Eventually we have three new variables for every element of the today date. We can do that with following command:. We just need to write other variables one behind other and the batch interpreter will collect concatenate their values in one unique string.

In our example, we collected 27 th day of the 5 th month of the year in the string with value of Our demo batch file is just printed this on the screen with ECHO command. However, we can use it to create a directory the folder of the day, like with the command:. Not quite what I need. I found an even easier way to do this, but have left the original post below.

You can use a combination of environmental variables and substring commands to do the rename right inline. To see where I found this information, check it out here Original Post You can use a simple batch script to rename a file and add a datestamp to it. The code is below. This would certainly not be my preference. Your Batch file would essentially be a loop, looking if the date required has been achieved. If the value comparison if true, it would execute possibly a sub: and then return to the start of the loop.

Here are 2 solutions: 1. The second line formats datetime in the form I needed for what I used it for. You can modify this line to format however suits your needs. Great information here. I am in need of a way to get the full 24 hr time displayed live. I am looking for a way to make that happen automatically without me hitting enter constantly.

Thanks in advance! Thank you very much Srini for this tutorial. I created my own Windows 10 Defender Virus scan batch command and used schedule task to run it and used your commands to create log files with date and time stamp. Steve Knight IT Consultancy. Author Commented: Some more prebuilt example scripts for common requests. Commented: Here's another way using the wmic utility. Bill Prew Test your restores, not your backups Experts with Gold status have received one of our highest-level Expert Awards, which recognize experts for their valuable contributions.

Expert of the Year This award recognizes experts who help improve Experts Exchange with their contributions to the site, leadership and mentorship efforts, and set an example within the community.



0コメント

  • 1000 / 1000