2016-05-23 140 views
2

我開發一個批處理腳本,它的當前日期-1月和格式作爲YYYYMMDD,但是當我執行我的代碼最終的變量只是年(沒有月份連接)。你能幫我找到錯誤嗎?問題來格式化當前日期(-1month),以YYYYMMDD

的代碼如下:

echo off &setlocal 
setlocal EnableDelayedExpansion 

SET _test=%date% 
SET _month=%_test:~4,2% 
SET _day=01 
SET search_year=%_test:~10,4% 


IF "%_month%" EQU 1 (
     set search_month=12 
     set /a search_year= %seach_year% -1 
) 

IF "%_month%" EQU 2 (
     set search_month=1 
) 
IF "%_month%" EQU 3 (
     set search_month=2 
) 

IF "%_month%" EQU 4 (
     set search_month=3 
) 

IF "%_month%" EQU 5 (
     set search_month=4 
) 

IF "%_month%" EQU 6 (
     set search_month=5 
) 

IF "%_month%" EQU 7 (
     set search_month=06 
) 

IF "%_month%" EQU 8 (
     set search_month=07 
) 

IF "%_month%" EQU 9 (
     set search_month=08 
) 

IF "%_month%" EQU 10 (
     set search_month=09 
) 

IF "%_month%" EQU 11 (
     set search_month=10 
) 

IF "%_month%" EQU 12 (
     set search_month=11 
) 


echo Month-1 is %search_month% 
set /a total = %search_year%%search_month% 
echo total is %total% 

感謝

回答

0

你一個月的比較是不正確的。從%_test:~4,2%獲取月份意味着當前月份(5月)返回爲05,而不是5

另外,如果比較的一側有引號,則在另一側必須有引號,因爲批量比較時包含引號。由於這兩件事情,search_month永遠不會被設置。你的比較應該是這樣的:

IF "%_month%" EQU "05" (
    set search_month=4 
) 

這就是說,12個基本一致,比較一噸的冗餘代碼,你可以把它的方式更簡單:

@echo off 
setlocal enabledelayedexpansion 

SET _test=%date% 
SET _month=%_test:~4,2% 
SET _day=01 
SET search_year=%_test:~10,4% 

if "%_month%" equ "01" (
    set search_month=12 
    set /a search_year-=1 
) else (
    REM Remove any zero-padding so that months are not considered octal. 
    REM We do this by putting a 1 in front of %_month% and then subtracting 100 
    REM because set /a returns in decimal. 
    REM And then we subtract 1 because we're getting the previous month. 
    set /a search_month=1%_month%-100-1 

    REM Replace the zero-padding since you want YYYYMM format. 
    REM Use delayed expansion here because search_month was created inside 
    REM of a code block. 
    if !search_month! lss 10 set search_month=0!search_month! 
) 

echo Month-1 is !search_month! 
set total=%search_year%!search_month! 
echo total is %total% 
+0

這正是我一直在尋找!感謝您的解釋:) – thartaras

+0

如果我想執行這個月只是15日呢? – thartaras

+0

@thartaras - 嗯,你最初提供的代碼並不是白天的因素,但是基於你有一個'_day'變量的事實,我會出去走一步,說改變該值爲15.但這只是一個猜測,因爲你的代碼是不完整的。 – SomethingDark

0

無需周圍放置%_month%報價 這是你想要的,

echo off &setlocal 
setlocal EnableDelayedExpansion 

SET _test=%date% 
SET _month=%_test:~4,2% 
SET _day=01 
SET search_year=%_test:~10,4% 


IF %_month% EQU 1 (
     set search_month=12 
     set /a search_year= %seach_year% -1 
) 

IF %_month% EQU 2 (
     set search_month=01 
) 
IF %_month% EQU 3 (
     set search_month=02 
) 

IF %_month% EQU 4 (
     set search_month=03 
) 

IF %_month% EQU 5 (
     set search_month=04 
) 

IF %_month% EQU 6 (
     set search_month=05 
) 

IF %_month% EQU 7 (
     set search_month=06 
) 

IF %_month% EQU 8 (
     set search_month=07 
) 

IF %_month% EQU 9 (
     set search_month=08 
) 

IF %_month% EQU 10 (
     set search_month=09 
) 

IF %_month% EQU 11 (
     set search_month=10 
) 

IF %_month% EQU 12 (
     set search_month=11 
) 


echo Month-1 is %search_month% 
set /a total = %search_year%%search_month% 
echo total is %total%