Batch file get variable value
The set command can also run from the Windows Recovery Console, using different parameters. If command extensions are enabled the default and you run set with a value, it displays all of the variables that begin with that value. If you use quotation marks to enclose a string that contains one of the special characters, the quotation marks are set as part of the environment variable value.
Use environment variables to control the behavior of some batch files and programs and to control the way Windows and the MS-DOS subsystem appears and works. The set command is often used in the Autoexec. If you use the set command without any parameters, the current environment settings are displayed. I'm sure this can be improved upon further but this gives the date into 3 variables for Day dd , Month mm and Year yyyy.
You can then use these later in your batch script as required. While I understand an answer has been accepted for this question this alternative method may be appreciated by many looking to achieve this without using the WMI console, so I hope it adds some value to this question.
And both also get the day of the week and none of them requires admin permissions! But it is built-in in everything from Windows Vista and above:. And three more ways that uses other Windows script languages. They will give you more flexibility e. JScript is available on every system from Windows NT and above, as a part of Windows Script Host though can be disabled through the registry it's a rare case :.
PowerShell - can be installed on every machine that has. NET - download from Microsoft v1, v2, and v3 only for Windows 7 and above. Self-compiled jscript. NET so I think this is a pretty portable :. Logman This cannot get the year and day of the week. It's comparatively slow, also creates a temp file and is based on the time stamps that logman puts on its log files. Will work everything from Windows XP and above. It probably will be never used by anybody - including me - but it is one more way In this approach, you can run the code multiple times and not worry about the old date value 'sticking around' because it's already defined.
Each time you run this batch file, it will output an ISO compatible combined date and time representation. You could either have a separate label for each copy, or just put this code into its own batch file and call it from your source file wherever necessary.
It is possible to use PowerShell and redirect its output to an environment variable by using a loop. A good idea is to use some API to retrieve the data structure and parse as you wish.
WMIC is a good choice. If you don't mind an one-time investment of 10 to 30 minutes to get a reliable solution that doesn't depend on Windows' region settings , please read on.
Let's free our minds. Do you want to simplify the scripts to just look like this? Source code of FormatNow. Just copy and paste the codes below, build the program once, and then you have a reliable date formatter for all future uses. In general, when dealing with complicated logics, we can make it simpler for by building a very small program and calling the program to capture the output back to a batch script variable instead.
We are not students and we're not taking exams requiring us to follow the batch-script-only rule to solve problems. Add a comment. Active Oldest Votes. Community Bot 1 1 1 silver badge. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. But this version will get fooled by the line Autoconfiguration IPv4 Address.
Comments are closed. I wrote a book Ground rules Disclaimers and such My necktie's Twitter. Reading the output of a command into a batch file variable Raymond. July 31st, Read next How do I find the most recently created file in a directory from a batch file?
0コメント