棘手的.bat批量创建文件夹,文件。

时间:2014-02-19 15:19:00

标签: date batch-file directory

有人可以帮我创建两个.bat文件,帮助我自动完成手头的任务吗? 这是一个很好的开始,但我需要更复杂的东西: Creating folder using bat file

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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%01012359 %year%\%year%-01-Jan
call :mkmonth 02 Feb 28+leap
touch -t %year%02012359 %year%\%year%-02-Feb
call :mkmonth 03 Mar 31
touch -t %year%03012359 %year%\%year%-03-Mar
call :mkmonth 04 Apr 30
touch -t %year%04012359 %year%\%year%-04-Apr
call :mkmonth 05 May 31
touch -t %year%05012359 %year%\%year%-05-May
call :mkmonth 06 Jun 30
touch -t %year%06012359 %year%\%year%-06-Jun
call :mkmonth 07 Jul 31
touch -t %year%07012359 %year%\%year%-07-Jul
call :mkmonth 08 Aug 31
touch -t %year%08012359 %year%\%year%-08-Aug
call :mkmonth 09 Sep 30
touch -t %year%09012359 %year%\%year%-09-Sep
call :mkmonth 10 Oct 31
touch -t %year%10012359 %year%\%year%-10-Oct
call :mkmonth 11 Nov 30
touch -t %year%11012359 %year%\%year%-11-Nov
call :mkmonth 12 Dec 31
touch -t %year%12012359 %year%\%year%-12-Dec
touch -t %year%01012359 %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