0

What is the Windows/batch version of this Bash script below:

MY_VAR=`date`
echo $MY_VAR

affecting the value of an operation back into a var?

In windows I been trying:

set MY_VAR=%date /T%
set MY_VAR=!date /T!
...
echo %MY_VAR%

I have been looking at: Windows batch files: How to set a variable with the result of a command? without any success...

Community
  • 1
  • 1
Alain
  • 702
  • 2
  • 13
  • 33

2 Answers2

2

You can say:

for /f "delims=" %i in ('date /T') do set MY_VAR=%i

If you're using a batch file, then you'd need to escape the % by using a double percent sign %%.

foxidrive
  • 40,353
  • 10
  • 53
  • 68
devnull
  • 118,548
  • 33
  • 236
  • 227
  • Thanks I just tried it and it works! But is there a more succint way of doing the same operation? – Alain Nov 29 '13 at 10:29
  • +1 Using a for /f is the usual method to capture text to a variable. `find` and `findstr` are often also used to filter certain lines from the output or text file. – foxidrive Nov 29 '13 at 13:01
2

You could just use the default date variable:

set var=%date%
Echo %var%

Or directly:

Echo %date%

That will do exactly the same thing with half the effort.

Mona.

Monacraft
  • 6,510
  • 2
  • 17
  • 29