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 ?

3
  • 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. Commented Apr 3, 2016 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, or open a command prompt window, run set /? and read output help explaining delayed expansion on an IF and a FOR example. Commented Apr 3, 2016 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. Commented Apr 3, 2016 at 15:25

1 Answer 1

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="

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.