Date variables in batch files




















We will then repack that information to one string for a today date. Then we can use it to create the directory per day or we can add it to the end of the file, like some log or the output result files. When we add the date string in such order, we can later sort the files and folders in ascending order, the day after day, in the chronological order.

Here is an example of the folders the ClamAV quarantine folder sorted by date in their name:. We can now see when we caught some viruses in an e-mail. Furthermore, we can easily find any e-mail held in the quarantine and check it further. Now that we know what we need to extract, we can use this to write our batch file. Here is the content of this 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:. These files will be smaller and easier for searching and analyzing.

On top of that, we can delete information older than X days, depending on our security policy. Bhaskara Arani Bhaskara Arani 1, 1 1 gold badge 21 21 silver badges 40 40 bronze badges. Will Will 39 1 1 bronze badge. This is an extension of Joey's answer to include the time and pad the parts with 0's. Also note that this is the UTC time. Ivellios Ivellios 88 7 7 bronze badges. If you have Python installed, you can do python -c "import datetime;print datetime. MathKid MathKid 1, 1 1 gold badge 17 17 silver badges 18 18 bronze badges.

In what way is saying "Use python" an answer to the question? This can be simplified to python -c "import datetime;print datetime. It is quite different since Python is not a standard component of any Windows build. I can always call an arbitrary high level language runtime to get a fancy timestamp string to display but certainly it is not applicable to the majority of the environments.

Chris Chris 49 1 1 bronze badge. But where is your answer different or better than the other 16? And there are already enough hints, that this solution only works for your localization — jeb. Pang 8, gold badges 82 82 silver badges bronze badges. Hillmee Bob Hillmee Bob 1. Simply build a FormatNow. Common sense: the built FormatNow. NET Framework to run. Common sense: after adding FormatNow. It also applies to any change in environment variables. Benefits: It's not slow finishes within 0.

FormatNow "ddd" to get only the day of week, FormatNow "yyyy" to get only the year It doesn't depend on Windows' region settings, so its output is much more reliable. You don't need to create so many CMD variables and pollute the variable namespace.

It would require 3 lines in the batch script to invoke the program and get the results. It should be reasonably short enough. Generic; using System. Linq; using System. Text; using System. Write DateTime. ToString format, CultureInfo. WriteLine ex. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked 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. Dear Sir I have an idea, Can you get the date and time string when you click and drag the bottom right clock of the windows taskbar, drag it to any filename box?

How would I get the date into a variable for an actual, valuable use? Remove the slashes in the date. Remove the leading space from the time. This post explains how to get current date and time from command prompt or in a batch file.

How to get date and time in a batch file Below is a sample batch script which gets current date and time Datetime. Reply Link. That last part is so helpful and outstanding! Thank you so much!!

Thanks to the author, this article certainly dusted off some old cobwebs. Hi everyone! Something that I can use to create a filename: MyFileT09;59; Leave this field empty. More Recent Posts How to check if Windows 32 or 64 bit?



0コメント

  • 1000 / 1000