batch-file - 批处理脚本生成特定年月、周和工作日的文件夹

标签 batch-file

最近几天我一直在寻找这个问题的答案。有很多关于全年批量文件夹创建的答案,但不够具体,所以我非常感谢答案。

所以我想要这样的文件夹结构,如下所示

2017 (for example main folder for year 2017)  
01_JAN....12_DEC (subfolders with names of month )  
01JAN/01_01_SUN, 02_01_MON, 03_01_TUE...etc  (subsub folders with week day name at the end)

如果可能的话,也可以在这里包含周数,尽管我对工作日感到满意,即

01JAN/01_01_SUN-52, 02_01_MON-01, 03_01_TUE-01...09_01_MON-02 etc with weekdays starts with monday

我计划在条形码自动化生产项目中使用它

谢谢!
附注 我得到的最接近的是我设法找到的这个例子 How do I write a batch script to generate folders for each month, day and year?

最佳答案

这个批处理文件可以完成您想要的操作。

编辑:我添加了以下修改: 1-年份是从键盘读取的,而不是从参数中获取的。 2- 在放置批处理文件的同一文件夹中创建年份文件夹。 3-修复了“第53周”中的一个小错误。

@echo off
setlocal EnableDelayedExpansion

set /P "year=Enter desired year: "

rem Select the base folder
cd "%~P0"

rem Initialize all variables
set i=0
for %%a in (Jan:31 Feb:28 Mar:31 Apr:30 May:31 Jun:30 Jul:31 Aug:31 Sep:30 Oct:31 Nov:30 Dec:31) do (
   for /F "tokens=1,2 delims=:" %%m in ("%%a") do (
      set /A i+=1
      set "month[!i!]=%%m"
      set "dpm[!i!]=%%n"
   )
)
set /A "dpm[2]+=^!(year%%4), d=((year+4799)*1461/4-(year+4899)/100*3/4+1)%%7*3, rot=^!d+((d-15)>>31)+1, week=101+51*rot"
if %d% equ 3 set "week=100"
set "dow=SunMonTueWedThuFriSat"
set "dow=!dow:~%d%!!dow:~0,%d%!"

rem Create the folders
md %year%
cd %year%
for /L %%m in (1,1,12) do (
   set "m=0%%m"
   set "m=!m:~-2!"
   md !m!_!month[%%m]!
   cd !m!_!month[%%m]!
   for /L %%d in (1,1,!dpm[%%m]!) do (
      set "d=0%%d"
      if "!dow:~0,3!" equ "Mon" set /A week+=1 & if !week!!rot! equ 1531 set "week=101"
      md !d:~-2!_!m!_!dow:~0,3!-!week:~1!
      set "dow=!dow:~3!!dow:~0,3!"
   )
   cd ..
)

关于batch-file - 批处理脚本生成特定年月、周和工作日的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45269929/

相关文章:

windows - 如何从 SQLCMD 输出中删除空白

windows - 如何从命令提示符*不带换行符*发送 EOF?

batch-file - 批处理文件 : <part of command line> was unexpected at this time

java - 如何从 .bat 脚本在单独的窗口中启动 JAR?

windows - 用于读取输入文本文件并为输入文本文件的每一行创建文本文件的批处理脚本

batch-file - 如何监视特定命令行错误 (Windows)?

php - Composer 完成执行后退出批处理文件

windows - 如何验证文件是否存在于批处理文件中?

c# - 在批处理文件中包含一个批处理文件

angularjs - 使用 Protractor 进行端到端测试不起作用