0
K:\bin>type get_date_without_space.bat
SET DATE_WITH_SPACE=%DATE%
FOR /F "TOKENS=1-2" %I IN ("%DATE_WITH_SPACE%") DO SET DATE_WITHOUT_SPACE=%I_%J
ECHO %DATE_WITHOUT_SPACE%

This doesn't work. Why?

K:\bin>get_date_without_space.bat
K:\bin>SET DATE_WITH_SPACE=Fri 11/16/2018
DATE_WITH_SPACEI_J was unexpected at this time.

K:\bin>FOR /F "TOKENS=1-2" DATE_WITH_SPACEI_J
K:\bin>
  • 1
    To replace your space , simply use string substitution Echo %date: =_% – LotPings Nov 16 '18 at 21:15
  • This works. But can you give me the syntax of the FOR statement, in order to do the job? I still don't find the correct statement. – Francky Leyn Nov 17 '18 at 1:42
  • As I wrote in my first comment, double the percent signs of the for meta variables %%I and %%J See ss64.com/nt/for.html – LotPings Nov 17 '18 at 1:54
  • run for /? and read it's output – phuclv Nov 17 '18 at 4:42
1
FOR /F "TOKENS=1-2" %%I IN ("%DATE_WITH_SPACE%") DO SET DATE_WITHOUT_SPACE=%%I_%%J

The meta variables are I and J and the original code used %I and %J instead of %%I and %%J.

  • If you need to post additional information to your question, please edit it to do so. But posting this as an answer? Nope. – JakeGould Nov 17 '18 at 5:10
  • This is not additional information to be added to the question;  this is the answer (albeit woefully terse). – Scott Nov 17 '18 at 5:24
  • @Scott I am utterly unable to understand how this is an answer. – JakeGould Nov 17 '18 at 18:36
  • 2
    @JakeGould: The problem was that the OP was using %I and %J in a BAT file.  The answer (shown above) is to use %%I and %%J instead of %I and %J. (I did say that it was woefully terse.) – Scott Nov 17 '18 at 19:37
  • 1
    @Scott Makes sense. Edited to add that bit of info. – JakeGould Nov 17 '18 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.