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%
这不管用。为什么?
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
FOR /F "TOKENS=1-2" %%I IN ("%DATE_WITH_SPACE%") DO SET DATE_WITHOUT_SPACE=%%I_%%J
元变量是 I 和 J,原始代码使用%I
和%J
代替%%I
和%%J
。