使用bat文件中的CMD命令创建多个文件夹

时间:2016-10-13 08:33:23

标签: batch-file

我正在尝试创建一个备份文件夹,其中包含2016年所有月份的子文件夹以及包含每个月所有日期子文件夹的子文件夹。我想使用bat文件来执行此操作。 我能做的最好的就是这个。

md "E:\cctv backup"
cd "E:\cctv backup"
md "E:\cctv backup\October 2016"
md "E:\cctv backup\November 2016"
cd "E:\cctv backup\October 2016"
md "E:\cctv backup\October 2016\01 Oct 2016"
md "E:\cctv backup\October 2016\02 Oct 2016"
md "E:\cctv backup\October 2016\03 Oct 2016"
md "E:\cctv backup\October 2016\04 Oct 2016"
md "E:\cctv backup\October 2016\05 Oct 2016"
md "E:\cctv backup\October 2016\06 Oct 2016"
md "E:\cctv backup\October 2016\06 Oct 2016"
md "E:\cctv backup\October 2016\08 Oct 2016"
md "E:\cctv backup\October 2016\09 Oct 2016"
md "E:\cctv backup\October 2016\10 Oct 2016"
md "E:\cctv backup\October 2016\11 Oct 2016"
md "E:\cctv backup\October 2016\12 Oct 2016"
md "E:\cctv backup\October 2016\13 Oct 2016"
md "E:\cctv backup\October 2016\14 Oct 2016"
md "E:\cctv backup\October 2016\15 Oct 2016"
md "E:\cctv backup\October 2016\16 Oct 2016"
md "E:\cctv backup\October 2016\17 Oct 2016"
md "E:\cctv backup\October 2016\18 Oct 2016"
md "E:\cctv backup\October 2016\19 Oct 2016"
md "E:\cctv backup\October 2016\20 Oct 2016"
md "E:\cctv backup\October 2016\21 Oct 2016"
md "E:\cctv backup\October 2016\22 Oct 2016"
md "E:\cctv backup\October 2016\23 Oct 2016"
md "E:\cctv backup\October 2016\24 Oct 2016"
md "E:\cctv backup\October 2016\25 Oct 2016"
md "E:\cctv backup\October 2016\26 Oct 2016"
md "E:\cctv backup\October 2016\27 Oct 2016"
md "E:\cctv backup\October 2016\28 Oct 2016"
md "E:\cctv backup\October 2016\29 Oct 2016"
md "E:\cctv backup\October 2016\30 Oct 2016"
md "E:\cctv backup\October 2016\31 Oct 2016"
cd "E:\cctv backup\November 2016"
md "E:\cctv backup\November 2016\01 Nov 2016"
md "E:\cctv backup\November 2016\02 Nov 2016"
md "E:\cctv backup\November 2016\03 Nov 2016"
md "E:\cctv backup\November 2016\04 Nov 2016"
md "E:\cctv backup\November 2016\05 Nov 2016"
md "E:\cctv backup\November 2016\06 Nov 2016"
md "E:\cctv backup\November 2016\07 Nov 2016"
md "E:\cctv backup\November 2016\08 Nov 2016"
md "E:\cctv backup\November 2016\09 Nov 2016"
md "E:\cctv backup\November 2016\10 Nov 2016"
md "E:\cctv backup\November 2016\11 Nov 2016"
md "E:\cctv backup\November 2016\12 Nov 2016"
md "E:\cctv backup\November 2016\13 Nov 2016"
md "E:\cctv backup\November 2016\14 Nov 2016"
md "E:\cctv backup\November 2016\15 Nov 2016"
md "E:\cctv backup\November 2016\16 Nov 2016"
md "E:\cctv backup\November 2016\17 Nov 2016"
md "E:\cctv backup\November 2016\18 Nov 2016"
md "E:\cctv backup\November 2016\19 Nov 2016"
md "E:\cctv backup\November 2016\20 Nov 2016"
md "E:\cctv backup\November 2016\21 Nov 2016"
md "E:\cctv backup\November 2016\22 Nov 2016"
md "E:\cctv backup\November 2016\23 Nov 2016"
md "E:\cctv backup\November 2016\24 Nov 2016"
md "E:\cctv backup\November 2016\25 Nov 2016"
md "E:\cctv backup\November 2016\26 Nov 2016"
md "E:\cctv backup\November 2016\27 Nov 2016"
md "E:\cctv backup\November 2016\28 Nov 2016"
md "E:\cctv backup\November 2016\29 Nov 2016"
md "E:\cctv backup\November 2016\30 Nov 2016"

1 个答案:

答案 0 :(得分:1)

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "targetdir=U:\destdir"
FOR /L %%y IN (2015,1,2017) DO (
 FOR %%m IN (January,February,March,April,May,June,
             July,August,September,October,November,December) DO (
   SET "monabbr=%%m"
   SET "monabbr=!monabbr:~0,3!"
   SET /a maxday=131
   IF "!monabbr!"=="Apr" SET /a maxday=130
   IF "!monabbr!"=="Jun" SET /a maxday=130
   IF "!monabbr!"=="Sep" SET /a maxday=130
   IF "!monabbr!"=="Nov" SET /a maxday=130
   IF "!monabbr!"=="Feb" (
    SET /a maxday=128
    SET /a leap=%%y %% 4
    IF !leap!==0 SET /a maxday=129
   )
   FOR /L %%d IN (101,1,!maxday!) DO (
    SET "daynum=%%d"
    SET "daynum=!daynum:~-2!"
    ECHO(MD "%targetdir%\%%m %%y\!daynum! !monabbr! %%y"

   )
 )
)
GOTO :EOF

您需要更改targetdir的设置以适合您的具体情况。

所需的MD命令仅用于ECHO用于测试目的。 在您确认命令正确无法后,将ECHO(MD更改为MD以实际创建目录。附加2>nul以禁止错误消息(例如,当目录已存在时)

开始,步骤和结束年份显而易见 - 在%%y

然后,对于%%m中的每个完整月份名称,使用monabbr和子字符串选择前{3}个字母,然后计算一个月中的最大天数,通常为131,除非是月份缩写表示30和2月 - 查看它是否是闰年并选择28或29.将100加到此最大值,以便开始,步骤,结束在delayedexpansion中产生3位数的日期数,然后计算该日期编号的最后2位为%%d,并按适当的顺序将元素组合在一起以创建所需的最低级子目录,记住这也将创建所需的任何中间目录。