Reputation: 233
I am trying to store the formatted date value to an environment variable. Below is my batch script.
set now="`powershell get-date -format \"{yyyy-mm-dd}\"`"
dsjob -run -jobstatus -param current_timestamp=%now% dstage1 TheAge
This is not working. I am a Unix guy and I expect an easier way of doing this than using for loops and other solutions provided.
How to store the executed command value to an environment variable?
Upvotes: 2
Views: 2257
Reputation: 67236
Excuse me. May I offer you a different point of view?
There are several different ways to get the current date in YYYY-MM-DD format, but powershell is not precisely the best one. The powershell.exe program is a file about 465 MB size that is best suited to be loaded once in order to execute a large .ps1 script. Using this program (that includes all the advanced facilities of the powershell programming language) just to get the current date is certainly a waste of resources that may take several seconds in slow machines.
Another commonly used approach in Batch files is wmic
, a standard Windows command that provides a series of data about the computer and its elements, but it is also a file 495 MB size that perform extensive operations and is not precisely fast.
The third option is to use a VBScript or JScript script; these are programming languages preinstalled on all Windows versions from XP on. In this case the run-time support for these languages is a compiler of the same family of C, C++ and JavaScript, so it is very efficient. The VBScript/JScript compiler file (cscript.exe) is just 143 MB size and is certainly the most efficient way to get the current date in YYYY-MM-DD format independent from locale settings, that is, it may be used in a wide range of different computers.
The last phrase also suggest the last method. If the Batch file will always run in the same computer, then you don't need the advanced features of previous methods; you may get the date directly from the %date% internal variable. This method is the most efficient one, because it only uses internal Batch commands.
Below there is a Batch file that show how to use all four previous methods. If you execute this Batch file several times, you will realize that the powershell method is precisely the less efficient one.
@echo off
setlocal
rem Get current date in YYYY-MM-DD format, locale independent
rem Powershell method
for /F %%a in ('powershell get-date -format "{yyyy-mm-dd}"') do set "now=%%a"
echo Powershell: %now%
echo/
rem WMIC method
for /F "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "now=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%"
echo WMIC: %now%
echo/
rem JScript method
echo var d=new Date();WScript.Echo(d.getFullYear()+"-"+(101+d.getMonth()).toString().substr(1)+"-"+(100+d.getDate()).toString().substr(1));>now.js
for /F "delims=" %%a in ('now.js') do set "now=%%a"
echo JScript: %now%
echo/
rem Get current date in YYYY-MM-DD format via %date% variable, locale dependent
for /F "tokens=1-3 delims=/" %%a in ("%date%") do set "now=%%c-%%a-%%b"
echo Pure Batch: %now%
echo/
Upvotes: 4
Reputation: 47832
You would be better off using a PowerShell script to do the whole thing; it will be simpler and PowerShell's operators and syntax are more bash-like (certainly not a drop in replacement, but still).
From within PowerShell, this could be a single line:
dsjob -run -jobstatus -param current_timestamp=$(Get-Date -Format yyyy-MM-dd) dstage1 TheAge
Of course you could use a variable also:
$now = Get-Date -Format yyyy-MM-dd
dsjob -run -jobstatus -param current_timestamp=$now dstage1 TheAge
Is there a reason you must use a batch file?
Upvotes: 2