我在使用此代码块时遇到了麻烦,该代码的目的是基于今天的日期创建目录名称。
@echo on
::
:: Global config variables
::
setlocal EnableDelayedExpansion
setlocal EnableExtensions
set BASE_DIR=S:\TRANS
::
:: Directories will end with MM(MON)YYYY eg: 08(Aug)2019
::
:: Script block will define MONTH_YEAR_STR
::
set MONTHS_STR=JanFebMarAprMayJunJulAugSepOctNovDec
for /f "tokens=*" %%t in ('WMIC os GET LocalDateTime') do (
if defined LINENO (
set /a LINENO+=1
) ELSE (
set LINENO=1
)
if !LINENO! equ 2 (
set OS_DATETIME=%%t
)
)
set OS_DATE_DAY_STR=%OS_DATETIME:~6,2%
set OS_DATE_MONTH_STR=%OS_DATETIME:~4,2%
set /A "OS_DATE_MONTH=(1%OS_DATE_MONTH_STR%%%100)"
set /A MONTHS_STR_OFFSET=(%OS_DATE_MONTH%-1)*3"
set OS_DATE_YEAR=%OS_DATETIME:~0,4%
set MONTH_STR=!MONTHS_STR:~%MONTHS_STR_OFFSET%,3!
set MONTH_YEAR_STR=%OS_DATE_MONTH_STR%^(%MONTH_STR%^)%OS_DATE_YEAR%
set DATE_MONTH_STR=%OS_DATE_DAY_STR%-%MONTH_STR%
::
:: Pre-execution stuff
::
set DL_DIR=%BASE_DIR%\DH1_Files\%MONTH_YEAR_STR%\%DATE_MONTH_STR%\
if not exist "%DL_DIR%" (
mkdir %DL_DIR%
)
exit /b
代码大部分都不错,DL_DIR已正确设置为S:\TRANS\DH1_Files\08(Aug)2019\07-Aug\
但是,问题来自下面的if not exist
块,在运行脚本时回显了该脚本,并输出了以下错误:
S:\Trans>set DL_DIR=S:\TRANS\DH1_Files\08(Aug)2019\07-Aug\
2019\07-Aug\ was unexpected at this time.
S:\Trans> mkdir S:\TRANS\DH1_Files\08(Aug)2019\07-Aug\
几乎无法正确解析括号字符,但是我看不到是什么原因引起的。
有什么想法吗?
谢谢!
答案 0 :(得分:0)
代码中双引号在哪里?
尝试一下:
@echo off
setlocal EnableDelayedExpansion
setlocal EnableExtensions
set BASE_DIR=S:\TRANS
set MONTHS_STR=JanFebMarAprMayJunJulAugSepOctNovDec
for /f "tokens=*" %%t in ('WMIC os GET LocalDateTime') do (
if defined LINENO (
set /a LINENO+=1
) ELSE (
set LINENO=1
)
if !LINENO! equ 2 (
set OS_DATETIME=%%t
)
)
set OS_DATE_DAY_STR=%OS_DATETIME:~6,2%
set OS_DATE_MONTH_STR=%OS_DATETIME:~4,2%
set /A "OS_DATE_MONTH=(1%OS_DATE_MONTH_STR%%%100)"
set /A MONTHS_STR_OFFSET=(%OS_DATE_MONTH%-1)*3"
set OS_DATE_YEAR=%OS_DATETIME:~0,4%
set MONTH_STR=!MONTHS_STR:~%MONTHS_STR_OFFSET%,3!
set "MONTH_YEAR_STR=%OS_DATE_MONTH_STR%^(%MONTH_STR%^)%OS_DATE_YEAR%"
set "DATE_MONTH_STR=%OS_DATE_DAY_STR%-%MONTH_STR%"
::
:: Pre-execution stuff
::
set "DL_DIR=%BASE_DIR%\DH1_Files\%MONTH_YEAR_STR%\%DATE_MONTH_STR%\"
if not exist "%DL_DIR%" (
mkdir %DL_DIR%
)
exit /b