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>
4
  • 1
    To replace your space , simply use string substitution Echo %date: =_%
    – LotPings
    Nov 16, 2018 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. Nov 17, 2018 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, 2018 at 1:54
  • run for /? and read it's output
    – phuclv
    Nov 17, 2018 at 4:42

1 Answer 1

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.

5
  • If you need to post additional information to your question, please edit it to do so. But posting this as an answer? Nope. Nov 17, 2018 at 5:10
  • This is not additional information to be added to the question;  this is the answer (albeit woefully terse). Nov 17, 2018 at 5:24
  • @Scott I am utterly unable to understand how this is an answer. Nov 17, 2018 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.) Nov 17, 2018 at 19:37
  • 1
    @Scott Makes sense. Edited to add that bit of info. Nov 17, 2018 at 22:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .