2014-02-19 57 views
-1

有人可以幫我創建兩個.bat文件,幫助我自動化我的任務在手? 這是一個偉大的開始,但我需要更復雜的東西: Creating folder using bat file整蠱.bat創建文件夾,散裝文件。

1,我想在this folder創建,多個文件夾有一個日期格式的名字在其中(例如「2014_01_01_Lo​​gs」,' 2014_01_02_Logs'等等)在start dateend date之間。每個文件夾應該包含一個文件,與日期格式類似(2014_01_01_Lo​​gs中的示例爲'2014_01_01_primary_log.xml','2014_01_02_Logs'中爲'2014_01_02_primary_log.xml',等等)。

2,第二個.bat文件應該將.xml文件和文件夾的文件系統中的日期設置爲有問題的日期。時間部分可以設置爲23:59:59。 (例如'2014_01_01_primary_log.xml'的最後修改日期應該是2014.01.01 35:59:59,與'2014_01_01_Lo​​gs'中的相同)。這也應該有參數this folderstart dateend date

謝謝你的幫助,我真的很感激。從這裏Sziro

+0

什麼喬說。你試過什麼了?你獲得了什麼結果? –

+0

我可以創建文件夾和文件。我已經將解決​​方案與此相關聯。我需要一個循環,從開始日期到結束日期,創建可以提供給創建方法的循環變量等。 – Sziro

+0

如果您發佈代碼,我們可能會幫助完成它... – Aacini

回答

1

下面的批號生成給予start dateend date的日期範圍。這個版本不管理閏年,但對此進行必要的修改很簡單。

編輯閏年管理添加

@echo off 
setlocal EnableDelayedExpansion 

rem Parameters: startDate endDate in YYYY/MM/DD format 
for /F "tokens=1-3 delims=/" %%a in ("%1") do set /A year=%%a, month=1%%b, day=1%%c 
for /F "tokens=1-3 delims=/" %%a in ("%2") do set /A endY=%%a, endM=1%%b, endD=1%%c 

set m=100 
for %%a in (31 28 31 30 31 30 31 31 30 31 30 31) do (
    set /A m+=1 
    set daysPerMonth[!m!]=1%%a 
) 

set /A leap=year%%4 
:nextMonth 
    set lastDay=!daysPerMonth[%month%]! 
    if %month% equ 102 if %leap% equ 0 set lastDay=129 
    if %year%%month% equ %endY%%endM% set lastDay=%endD% 
    for /L %%d in (%day%,1,%lastDay%) do (
     set DD=%%d 
     echo %year%_%month:~1%_!DD:~1! 
    ) 
    set /A month+=1, day=101 
    if %month% gtr 112 set /A year+=1, leap=year%%4, month=101 
if %year%%month%%day% leq %endY%%endM%%endD% goto nextMonth 
exit /B 

輸出例如:

C:\> test 2013/12/30 2014/03/02 
2013_12_30 
2013_12_31 
2014_01_01 
2014_01_02 
2014_01_03 
. . . . 
2014_01_29 
2014_01_30 
2014_01_31 
2014_02_01 
2014_02_02 
2014_02_03 
. . . 
2014_02_26 
2014_02_27 
2014_02_28 
2014_03_01 
2014_03_02 
0

答案是接近的好:https://superuser.com/questions/483045/how-do-i-write-a-batch-script-to-generate-folders-for-each-month-day-and-year

不過這將是更好的,如果一些日曆功能的存在。

@echo off & setlocal 
set year=%1 
if "%year%"=="" set /p year=Year? 
if "%year%"=="" goto :eof 
set /a mod=year %% 400 
if %mod%==0 set leap=1 && goto :mkyear 
set /a mod=year %% 100 
if %mod%==0 set leap=0 && goto :mkyear 
set /a mod=year %% 4 
if %mod%==0 set leap=1 && goto :mkyear 
set leap=0 

:mkyear 
call :mkmonth 01 Jan 31 
call :mkmonth 02 Feb 28+leap 
call :mkmonth 03 Mar 31 
call :mkmonth 04 Apr 30 
call :mkmonth 05 May 31 
call :mkmonth 06 Jun 30 
call :mkmonth 07 Jul 31 
call :mkmonth 08 Aug 31 
call :mkmonth 09 Sep 30 
call :mkmonth 10 Oct 31 
call :mkmonth 11 Nov 30 
call :mkmonth 12 Dec 31 
goto :eof 

:mkmonth 
set month=%1 
set mname=%2 
set /a ndays=%3 
for /l %%d in (1,1,9)  do mkdir %year%\%year%-%month%-%mname%\%year%-%month%-0%%d 
for /l %%d in (10,1,%ndays%) do mkdir %year%\%year%-%month%-%mname%\%year%-%month%-%%d 

第二個:

@echo off & setlocal 
set year=%1 
if "%year%"=="" set /p year=Year? 
if "%year%"=="" goto :eof 
set /a mod=year %% 400 
if %mod%==0 set leap=1 && goto :mkyear 
set /a mod=year %% 100 
if %mod%==0 set leap=0 && goto :mkyear 
set /a mod=year %% 4 
if %mod%==0 set leap=1 && goto :mkyear 
set leap=0 

:mkyear 
call :mkmonth 01 Jan 31 
touch -t %year%01%year%\%year%-01-Jan 
call :mkmonth 02 Feb 28+leap 
touch -t %year%02%year%\%year%-02-Feb 
call :mkmonth 03 Mar 31 
touch -t %year%03%year%\%year%-03-Mar 
call :mkmonth 04 Apr 30 
touch -t %year%04%year%\%year%-04-Apr 
call :mkmonth 05 May 31 
touch -t %year%05%year%\%year%-05-May 
call :mkmonth 06 Jun 30 
touch -t %year%06%year%\%year%-06-Jun 
call :mkmonth 07 Jul 31 
touch -t %year%07%year%\%year%-07-Jul 
call :mkmonth 08 Aug 31 
touch -t %year%08%year%\%year%-08-Aug 
call :mkmonth 09 Sep 30 
touch -t %year%09%year%\%year%-09-Sep 
call :mkmonth 10 Oct 31 
touch -t %year%10%year%\%year%-10-Oct 
call :mkmonth 11 Nov 30 
touch -t %year%11%year%\%year%-11-Nov 
call :mkmonth 12 Dec 31 
touch -t %year%12%year%\%year%-12-Dec 
touch -t %year%01%year% 
goto :eof 

:mkmonth 
set month=%1 
set mname=%2 
set /a ndays=%3 
for /l %%d in (1,1,9)  do touch -t %year%%month%0%%d2359 %year%\%year%-%month%-%mname%\%year%-%month%-0%%d 
for /l %%d in (10,1,%ndays%) do touch -t %year%%month%%%d2359 %year%\%year%-%month%-%mname%\%year%-%month%-%%d 
相關問題