标题: [日期时间] 批处理如何每月每天排除周六和周日生成日期文件夹? [打印本页]
作者: kmcc123 时间: 6 天前 19:01 标题: 批处理如何每月每天排除周六和周日生成日期文件夹?
每月每天排除周六和周日生成日期文件夹,好像那里出错,没有排除周六和周日- @echo off
- setlocal enabledelayedexpansion
-
- :: 设置起始年份和结束年份
- set START_YEAR=2024
- set END_YEAR=2024
-
- :: 设置起始月份和结束月份
- set START_MONTH=11
- set END_MONTH=12
-
- for /l %%y in (%START_YEAR%, 1, %END_YEAR%) do (
- for /l %%m in (%START_MONTH%, 1, %END_MONTH%) do (
- set /a days=0
- set /a days=!days!+1
- set /a days=!days!+1
- for /l %%d in (1, 1, 30) do (
- set /a days=!days!+1
- set /a w=!days!%%7
- if !w! GEQ 1 if !w! LEQ 5 (
- mkdir "%%y\%%y-%%m\%%y-%%m-%%d" >nul 2>nul
- )
- )
- if %%m==2 (
- set /a days=!days!+1
- set /a w=!days!%%7
- if !w! GEQ 1 if !w! LEQ 5 (
- mkdir "%%y\%%y-%%m\%%y-%%m-29" >nul 2>nul
- )
- )
- if %%m==4 (
- set /a days=!days!+1
- set /a w=!days!%%7
- if !w! GEQ 1 if !w! LEQ 5 (
- mkdir "%%y\%%y-%%m\%%y-%%m-30" >nul 2>nul
- )
- )
- if %%m==6 (
- set /a days=!days!+1
- set /a w=!days!%%7
- if !w! GEQ 1 if !w! LEQ 5 (
- mkdir "%%y\%%y-%%m\%%y-%%m-30" >nul 2>nul
- )
- )
- if %%m==9 (
- set /a days=!days!+1
- set /a w=!days!%%7
- if !w! GEQ 1 if !w! LEQ 5 (
- mkdir "%%y\%%y-%%m\%%y-%%m-30" >nul 2>nul
- )
- )
- if %%m==11 (
- set /a days=!days!+1
- set /a w=!days!%%7
- if !w! GEQ 1 if !w! LEQ 5 (
- mkdir "%%y\%%y-%%m\%%y-%%m-30" >nul 2>nul
- )
-
- )
-
- )
-
- )
-
- endlocal
- echo. & pause
复制代码
作者: hnfeng 时间: 6 天前 19:48
如果你的系统支持powershell的话,感觉用powershell来处理会更方便一点
作者: went 时间: 5 天前 00:37
写了几个通用函数- @echo off & cd /d ""%~dp0
- setlocal enabledelayedexpansion
-
- :: 设置起始年份和结束年份
- set START_YEAR=2024
- set END_YEAR=2024
-
- :: 设置起始月份和结束月份
- set START_MONTH=11
- set END_MONTH=12
-
- for /l %%y in (%START_YEAR%, 1, %END_YEAR%) do (
- for /l %%m in (%START_MONTH%, 1, %END_MONTH%) do (
- call :get_monthday %%y %%m
- set "END_DAY=!_RET_!"
- for /l %%d in (1,1,!END_DAY!) do (
- call :get_weekday %%y %%m %%d
- echo %%y-%%m-%%d week!_RET_!
- rem 跳过周六周日
- if not !_RET_!==6 if not !_RET_!==7 (
- echo mkdir %%y\%%y-%%m\%%y-%%m-%%d
- mkdir %%y\%%y-%%m\%%y-%%m-%%d >nul 2>nul
- )
- echo -------------------------
- )
- )
- )
-
- pause&exit
-
- rem 获取某一天是星期几 _RET_[1-7]:get_weekday(year,month,day)
- :get_weekday
- call :get_days %1 %2 %3
- set /a "_RET_=%_RET_%%%7"
- if,%_RET_%==0 set "_RET_=7"
- goto :eof
-
- rem 获取总天数 _RET_:get_days(year,month,day)
- :get_days
- set /a "y=%1-1"
- set /a "y1=%y%/4-%y%/100+%y%/400"
- set /a "m1=0,m2=m1+31"
- call :check_r %1 && (set /a m3=m2+29) || (set /a m3=m2+28)
- set /a "m4=m3+31,m5=m4+30,m6=m5+31,m7=m6+30,m8=m7+31,m9=m8+31,m10=m9+30,m11=m10+31,m12=m11+30,m13=m12+31"
- call set /a "_RET_=%y%*365+%y1%+%%m%2%%+%3"
- goto :eof
-
- rem 获取某年里某个月有几天 _RET_[28-31]:get_monthday(year,month)
- :get_monthday
- set /a "m1=31"
- call :check_r %1 && (set /a m2=29) || (set /a m2=28)
- set /a "m3=31,m4=30,m5=31,m6=30,m7=31,m8=31,m9=30,m10=31,m11=30,m12=31"
- call set "_RET_=%%m%2%%"
- goto :eof
-
- rem 判断闰年 BOOL:check_r(year)
- :check_r
- set /a "1/(%1%%400)" >nul 2>nul || (exit /b 0)
- set /a "1/(%1%%100)" >nul 2>nul && (set /a "1/(%1%%4)" >nul 2>nul && (exit /b 1) || (exit /b 0) ) || (exit /b 1)
- goto :eof
复制代码
作者: new_user 时间: 5 天前 15:09
# 获取当前日期
$currentDate = Get-Date
# 获取本月的第一天和最后一天
$firstDayOfThisMonth = (Get-Date -Year $currentDate.Year -Month $currentDate.Month -Day 1)
$lastDayOfThisMonth = $firstDayOfThisMonth.AddMonths(1).AddDays(-1)
# 获取下月的第一天和最后一天
$firstDayOfNextMonth = $firstDayOfThisMonth.AddMonths(1)
$lastDayOfNextMonth = $firstDayOfNextMonth.AddMonths(1).AddDays(-1)
# 定义文件夹路径(根据需要修改)
$folderBasePath = "."
# 函数:创建文件夹
function CreateFolder ($date) {
$folderName = $date.ToString("yyyy-MM-dd")
$folderPath = "$folderBasePath\$folderName"
if (-not (Test-Path -Path $folderPath)) {
New-Item -Path $folderPath -ItemType Directory
Write-Host "文件夹 '$folderName' 已创建!"
} else {
Write-Host "文件夹 '$folderName' 已经存在!"
}
}
# 遍历本月的每一天,从今天开始
$currentDay = $currentDate
while ($currentDay -le $lastDayOfThisMonth) {
if ($currentDay.DayOfWeek -ne 'Saturday' -and $currentDay.DayOfWeek -ne 'Sunday') {
CreateFolder $currentDay
}
$currentDay = $currentDay.AddDays(1)
}
# 遍历下月的每一天
$currentDay = $firstDayOfNextMonth
while ($currentDay -le $lastDayOfNextMonth) {
if ($currentDay.DayOfWeek -ne 'Saturday' -and $currentDay.DayOfWeek -ne 'Sunday') {
CreateFolder $currentDay
}
$currentDay = $currentDay.AddDays(1)
}
作者: aloha20200628 时间: 5 天前 15:29
回复 1# kmcc123
关键是两个算点》一是判断某天是星期几,二是判断某年是否为闰年(即2月份有29天),前者用 zeller 公式判断至为简捷,后者采用国际标准判据。以下代码存为 test.bat 运行,当前代码中假定目标年份=2024,目标月份=2(均可在代码中第2行自定义),结果会在当前目录下生成 2024\2024-2\2024-2-1, ..., 2024\2024-2\2024-2-29 共21个子目录,其中排除了周六-周日...- @echo off &setlocal enabledelayedexpansion
- set/a "y=_y=2024, m=_m=2"
- set/a "m1=m3=m5=m7=m8=m10=m12=31,m2=28,m4=m6=m9=m11=30"
- set/a "y1=y%%4,y2=y%%100,y3=y%%400"
- if %y1% equ 0 if %y2% neq 0 (set m2=29) else if %y3% equ 0 (set m2=29)
- if %m% lss 3 set/a "_y-=1,_m+=12"
- for /l %%d in (1,1,!m%m%!) do for /f %%m in (
- 'set/a "w=(%%d+2*%_m%+3*(%_m%+1)/5+%_y%+%_y%/4-%_y%/100+%_y%/400)%%7,w+=1" '
- ) do if %%m lss 6 md "%y%\%y%-%m%\%y%-%m%-%%d" 2>nul
- endlocal&pause&exit/b
复制代码
作者: kmcc123 时间: 4 天前 11:23
多谢各好友指导
作者: 77七 时间: 前天 17:29
- @echo off & cd /d "%~dp0" & fltmc >nul || powershell -Command "Start-Process '%~f0' -Verb RunAs" && exit /b
- :: 设置起始年份和结束年份
- set START_YEAR=2024
- set END_YEAR=2024
- :: 设置起始月份和结束月份
- set START_MONTH=11
- set END_MONTH=12
- set datenow=%date:~0,10%
- setlocal enabledelayedexpansion
- (for /l %%l in (%START_YEAR% 1 %END_YEAR%) do (
- for /l %%m in (1%START_MONTH% 1 1%END_MONTH%) do (
- set m=%%m
- for /l %%n in (101 1 131) do (
- set d=%%n
- echo %%l/!m:~-2!/!d:~-2!|date
- if not errorlevel 1 (
- if "!date:六=!" equ "!date!" (
- if "!date:日=!" equ "!date!" (
- md "%%l\%%l-!m:~-2!\%%l-!m:~-2!-!d:~-2!"
- )
- )
- )
- )
- )
- )) 1>nul 2>nul
- endlocal
- echo %datenow%|date 1>nul 2>nul
- pause
复制代码
短暂的修改下系统日期,获取到周几,可能会影响其它程序
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |