【问题标题】:Tricky .bat to create folders, files in bulk.棘手的 .bat 批量创建文件夹、文件。
【发布时间】:2014-02-19 15:19:00
【问题描述】:

有人可以帮我创建两个 .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 date 和一个end date

感谢您的帮助,我真的很感激。西罗

【问题讨论】:

  • 乔说的。你试过什么了?取得了什么成果?
  • 我可以创建文件夹和文件。我已经将解决​​方案与此相关联。我需要一个周期,从开始日期到结束日期,创建可以提供给创建方法的周期变量等。
  • 如果您发布您的代码,我们可能会帮助完成它...

标签: date batch-file directory


【解决方案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

【讨论】:

    【解决方案2】:

    这里的答案已经接近了: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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-09
      • 1970-01-01
      • 2013-01-29
      • 1970-01-01
      • 2023-04-09
      • 2021-09-17
      • 1970-01-01
      相关资源
      最近更新 更多