1

I have a batch file where I have a variable which prints date in YYYYMMDD - 20160403

for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set ldt=%ldt:~0,4%%ldt:~4,2%%ldt:~6,2%
echo Local date is %ldt%

then I am downloading a file which has the date a the end of the url

curl "http://www.example.com/cgi-bin/abc.xyz/%ldt%.txt" -o file-name-%ldt%.txt

where %ldt% has the date for eg: 20160403

but now I am not able to print the date. Any suggesstions ?

Deepankar
  • 138
  • 1
  • 14
  • 1
    your way to get `ldt` is much too complicated: `for /f "tokens=2 delims==" %%i in ('wmic os get localdatetime /value') do set ldt=%i` and `set ldt=%ldt:~0,8%` is enough. – Stephan Apr 03 '16 at 11:23
  • I suppose your posted block is embedded in a larger block and therefore you need delayed expansion. Accessing environment variables modified or defined within a block defined with `(` ... `)` must be done using delayed expansion. Read [example of delayed expansion in batch file](http://stackoverflow.com/questions/10558316/), or open a command prompt window, run `set /?` and read output help explaining delayed expansion on an __IF__ and a __FOR__ example. – Mofi Apr 03 '16 at 12:07
  • M sory it went all over my head. Isn't there a simple solution to this ? I just want to print the value in %ldt% to the url's end that's it. – Deepankar Apr 03 '16 at 15:25

1 Answers1

1

%date% is an internal windows variable. It is not wise to overwrite it. Use another variable name (for example %myDate%).

By the way: you can restore the original windows variable by (no joke) deleting it: set "date="

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • I am not sure this solves my question. I have no variable named %date% my date is being stored in %ldt% . The problem in not the variable. I am not able to print the date in curl URL to download it – Deepankar Apr 03 '16 at 11:39