标题: [日期时间] 批处理建立目录,无法正常运行 [打印本页]
作者: zhlgx 时间: 2024-10-1 12:07 标题: 批处理建立目录,无法正常运行
- @echo off
- setlocal enabledelayedexpansion
-
- :: 提示用户输入月份
- echo 请输入月份数字(1-12):
- set /p month_input=
-
- :: 检查输入是否合法
- if not defined month_input (
- echo 请输入有效的月份数字(1-12)!
- exit /b
- )
-
- if !month_input! lss 1 (
- echo 请输入有效的月份数字(1-12)!
- exit /b
- )
-
- if !month_input! gtr 12 (
- echo 请输入有效的月份数字(1-12)!
- exit /b
- )
-
-
- :: 设置月份变量
- set month_num=!month_input!
- set month_str=!month_num!
-
-
- :: 如果月份小于 10,则前面加 0
- if !month_num! lss 10 (
- set month_str=0!month_num!
- )
-
-
- :: 获取当前年份
- for /f "tokens=2 delims==" %%a in ('wmic os get localdatetime /value') do (
- set datetime=%%a
- )
- set year=%datetime:~0,4%
-
-
- :: 判断是否为闰年
- set is_leap_year=0
- if !year! %% 400 equ 0 (
- set is_leap_year=1
- ) else if !year! %% 100 neq 0 (
- if !year! %% 4 equ 0 (
- set is_leap_year=1
- )
- )
-
- pause
- :: 根据用户输入的月份创建对应的目录
- set main_dir=!year!年!month_str!月
- md "!main_dir!" >nul 2>nul
-
- pause
- :: 根据月份创建子目录
- if !month_num! equ 2 (
- if !is_leap_year! equ 1 (
- for /l %%d in (1,1,29) do (
- set day=%%d
- if %%d lss 10 set day=0%%d
- md "!main_dir!\%%day%日" >nul 2>nul
- )
- ) else (
- for /l %%d in (1,1,28) do (
- set day=%%d
- if %%d lss 10 set day=0%%d
- md "!main_dir!\%%day%日" >nul 2>nul
- )
- )
- ) else if !month_num! equ 4 (
- for /l %%d in (1,1,30) do (
- set day=%%d
- if %%d lss 10 set day=0%%d
- md "!main_dir!\%%day%日" >nul 2>nul
- )
- ) else if !month_num! equ 6 (
- for /l %%d in (1,1,30) do (
- set day=%%d
- if %%d lss 10 set day=0%%d
- md "!main_dir!\%%day%日" >nul 2>nul
- )
- ) else if !month_num! equ 9 (
- for /l %%d in (1,1,30) do (
- set day=%%d
- if %%d lss 10 set day=0%%d
- md "!main_dir!\%%day%日" >nul 2>nul
- )
- ) else if !month_num! equ 11 (
- for /l %%d in (1,1,30) do (
- set day=%%d
- if %%d lss 10 set day=0%%d
- md "!main_dir!\%%day%日" >nul 2>nul
- )
- ) else (
- for /l %%d in (1,1,31) do (
- set day=%%d
- if %%d lss 10 set day=0%%d
- md "!main_dir!\%%day%日" >nul 2>nul
- )
- )
-
- echo Folder created.
-
- :: 复制文件到对应月份的目录
- copy "%CD%\1.xlsx" "!main_dir!\\" >nul 2>nul
-
- exit /b
复制代码
无法运行,提示:
请输入月份数字(1-12):
1
此时不应有 %。
作者: 77七 时间: 2024-10-1 13:01
- :: 判断是否为闰年
- set is_leap_year=0
- if !year! %% 400 equ 0 (
复制代码
需要先用 set /a 计算,再比较
复制代码
应为
复制代码
作者: 77七 时间: 2024-10-1 13:13
相关方法 http://bbs.bathome.net/viewthread.php?tid=69590
- @echo off
- rem 保存为ansi
- for /f %%a in ('wmic os get localdatetime ^|findstr [0-9]') do set year=%%a&call set year=%%year:~0,4%%
- set/a num=!(year%%4) ^& !(!(year%%100)) ^| !(year%%400),_2=28+num,_1=_3=_5=_7=_8=_10=_12=31,_4=_6=_9=_11=30
- :re
- set /p m=input:
- setlocal enabledelayedexpansion
- if not defined _!m! (
- echo 请输入有效的月份数字(1-12)!
- endlocal
- set m=
- goto :re
- )
- set "#m=0!m!"
- for /l %%l in (1,1,!_%m%!) do (
- set l=0%%l
- md "%year%年!#m:~-2!月\!l:~-2!日"
- )
- copy "%CD%\1.xlsx" "%year%年!#m:~-2!月\"
- endlocal
- pause
复制代码
作者: aloha20200628 时间: 2024-10-2 10:20
本帖最后由 aloha20200628 于 2024-10-2 10:42 编辑
回复 1# zhlgx
一楼代码存在变量引用等错误,现重写一个简化版本如下...
主要包含几个处理步骤:检查输入月值的合理性;平闰年2月份天数选择值;大小月份天数选择值;月日值补零占位处理...- @echo off &setlocal enabledelayedexpansion
- choice /c 123456789abc /n /m "指定一个月份值 [1,2,3,4,5,6,7,8,9,a,b,c] 其中 a=10,b=11,c=12:"
- if !errorlevel! equ 0 (exit/b) else set "m=!errorlevel!"
- set "m30=4,6,9,11," &set/a "y=%date:~,4%,r=y%%4"
- if !m! equ 2 (set "days=28" &if !r! equ 0 set "days=29") else (
- if "!m30:%m%=!"=="!m30!" (set "days=31") else set "days=30")
- if !m! leq 9 (set mm=0!m!) else (set mm=!m!)
- for /l %%n in (1,1,!days!) do (if %%n leq 9 (md "!y!年!mm!月\0%%n日") else md "!y!年!mm!月\%%n日") 2>nul
- endlocal&pause&exit/b
复制代码
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |