2

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?

Mofi
  • 46,139
  • 17
  • 80
  • 143
coder05
  • 183
  • 2
  • 2
  • 8
  • 2
    Have you tried this? `for /F "delims=" %%D in ('powershell get-date -format "{yyyy-mm-dd}"') do set "CurrDate=%%~D"` (if you type this in command prompt directly rather than using it in a batch file, replace every `%%` by `%`) – aschipfl Dec 18 '15 at 03:09
  • 1
    If every programming and scripting language was the same we would only need one! You have to use a FOR /F command to capture the output of the command or redirect the output to a file and then redirect the file to a variable. – Squashman Dec 18 '15 at 03:13
  • 2
    take a look at [this](http://stackoverflow.com/a/19799236/388389) – npocmaka Dec 18 '15 at 09:58

2 Answers2

4

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/
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • This is a great collection of the various methods existing to get current date in a specified format and well explained. Many thanks by a member always interested in good date and time related answers. – Mofi Dec 18 '15 at 11:49
  • 4
    I only want to add that on last locale dependent code snippet written for date format `mm/dd/yyyy` using in __for__ `"tokens=1-3 delims=/."`(period added as delimiter) and using `"%date:~-10%"` (only last 10 characters of date string) and using `"now=%%c-%%b-%%a"` (b and a switched) would result in a code working for many Western European countries using date format `dd.mm.yyyy` or `dd/mm/yyyy` and making the code independent on presence of abbreviated weekday at beginning of locale dependent date string of environment variable __DATE__. – Mofi Dec 18 '15 at 12:01
2

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?

briantist
  • 45,546
  • 6
  • 82
  • 127
  • I ran the command on the powershell but it does not recognizes dsjob? dsjob is an exe file that is stored at a location. The same command does work on cmd though. Is there a particular way to run an executable file? – coder05 Dec 18 '15 at 04:07
  • @coder05 you can reference the full path of the executable: `C:\my\path\to\dsjob.exe -run -jobstatus ...` – briantist Dec 18 '15 at 04:08