标题: [数值计算] [已解决]批处理转换秒数格式出错,求助 看看哪里的问题? [打印本页]
作者: hnfeng 时间: 2024-3-5 15:27 标题: [已解决]批处理转换秒数格式出错,求助 看看哪里的问题?
本帖最后由 hnfeng 于 2024-3-7 08:35 编辑
想写个“秒数”转换为“hh:mm:ss” 格式的时间的函数,但是运行时显示“此时不应有 else”,不知道哪里的问题,请高手看看:- ::@echo off
- setlocal EnableDelayedExpansion&cd /d "%~dp0"&title %~nx0 &color 07
-
- call :Sec2Time 8 hhmmss
- echo 8 %hhmmss%
- pause
-
- call :Sec2Time 68 hhmmss
- echo 68 %hhmmss%
- pause
-
- call :Sec2Time 168 hhmmss
- echo 168 %hhmmss%
- pause
-
- call :Sec2Time 3668 hhmmss
- echo 3668 %hhmmss%
- pause
-
- call :Sec2Time 13668 hhmmss
- echo 13668 %hhmmss%
- pause
-
- exit /b
-
-
- ::==================================
- :Sec2Time
- setlocal ENABLEEXTENSIONS
- set hh=0
- set mm=0
- set ss=0
- if %1 LSS 60 (
- endlocal & set %2=!hh!:!mm!:%1
- ) else (
- if %1 LSS 3600 (
- set /a mm=%1 / 60
- set /a ss=$1 - (!mm! * 60)
- endlocal & set %2=!hh!:!mm!:!ss!
- ) else (
- set /a hh=%1 / 3600
- set /a mm=(%1 - (!hh! * 3600)) / 60
- set /a ss=%1 - !hh! * 3600 - !mm! * 60
- endlocal & set %2=!hh!:!mm!:!ss!
- )
- )
- goto :EOF
复制代码
作者: 77七 时间: 2024-3-5 15:50
- if %1 LSS 60 (
- endlocal & set %2=!hh!:!mm!:%1
- ) else (
- if %1 LSS 3600 (
- set /a mm=%1 / 60
- set /a ss=$1 - ^(!mm! * 60^)
- endlocal & set %2=!hh!:!mm!:!ss!
- ) else (
- set /a hh=%1 / 3600
- set /a mm=^(%1 - ^(!hh! * 3600^)^) / 60
- set /a ss=%1 - !hh! * 3600 - !mm! * 60
- endlocal & set %2=!hh!:!mm!:!ss!
- )
- )
复制代码
作者: hnfeng 时间: 2024-3-5 16:24
回复 2# 77七
厉害,一下子就看到问题了,多谢多谢
可能还有其他问题, 第一个运行结果正确,其他的输出结果不正确, 不知哪里的问题:- 8 0:0:8
- 请按任意键继续. . .
- 68 0:0:0
- 请按任意键继续. . .
- 168 0:0:0
- 请按任意键继续. . .
- 3668 0:0:0
- 请按任意键继续. . .
- 13668 0:0:0
- 请按任意键继续. . .
复制代码
作者: ppll2030 时间: 2024-3-5 16:34
回复 1# hnfeng
前面开了延时,跳转出就不要取消,不然你设置了时分秒都不会变了。
另外set /a 作为计算运算时,特殊符号要记得用转义符。
添加双零,在只有分秒的时候美观一些。- :Sec2Time
-
- set hh=00
- set mm=00
- set ss=00
- if %1 LSS 60 (
- set %2=!hh!:!mm!:%1
- ) else (
- if %1 LSS 3600 (
- set /a mm=%1 / 60
- set /a ss=%1 - ^(!mm! * 60^)
- set %2=!hh!:!mm!:!ss!
- ) else (
- set /a hh=%1 / 3600
- set /a mm=^(%1 - ^(!hh! * 3600^)^) / 60
- set /a ss=%1 - !hh! * 3600 - !mm! * 60
- set %2=!hh!:!mm!:!ss!
- )
- )
- goto :EOF
复制代码
作者: ppll2030 时间: 2024-3-5 16:36
回复 3# hnfeng
你原来的代码,38行,输错了一个$
作者: hnfeng 时间: 2024-3-5 16:48
回复 5# ppll2030
是的是的,这个失误不应该
作者: hnfeng 时间: 2024-3-5 16:49
多谢前面两位帮忙,计算没问题了。我再试试把 1位 的前面添加 0
作者: hnfeng 时间: 2024-3-5 17:16
本帖最后由 hnfeng 于 2024-3-5 17:40 编辑
初步可以了,但是因为想在 批处理面使用延迟变量,又不想 函数 里面的变量与从主代码里面的变量冲突,有没有办法呢?- @echo off&setlocal EnableDelayedExpansion&cd /d "%~dp0"&title %~nx0 &color 07
-
- call :Sec2Time 8 hhmmss
- echo 8 %hhmmss%
-
- call :Sec2Time 68 hhmmss
- echo 68 %hhmmss%
-
- call :Sec2Time 168 hhmmss
- echo 168 %hhmmss%
-
- call :Sec2Time 3668 hhmmss
- echo 3668 %hhmmss%
-
- call :Sec2Time 13668 hhmmss
- echo 13668 %hhmmss%
-
- call :Sec2Time 83668 hhmmss
- echo 83668 %hhmmss%
-
- echo;&pause
- exit /b
-
-
- ::==================================
- :Sec2Time
- set hh=00
- set mm=00
- set ss=00
- if %1 LSS 60 (
- set ss=0%1
- set %2=!hh!:!mm!:!ss:~-2!
- ) else (
- if %1 LSS 3600 (
- set /a mm=%1 / 60
- set /a ss=%1 - ^(!mm! * 60^)
- set mm=0!mm!
- set ss=0!ss!
- set %2=!hh!:!mm:~-2!:!ss:~-2!
- ) else (
- set /a hh=%1 / 3600
- set /a mm=^(%1 - ^(!hh! * 3600^)^) / 60
- set /a ss=%1 - !hh! * 3600 - !mm! * 60
- if !hh! LSS 10 set hh=0!hh!
- set mm=0!mm!
- set ss=0!ss!
- set %2=!hh!:!mm:~-2!:!ss:~-2!
- )
- )
- goto :EOF
复制代码
- 8 00:00:08
- 68 00:01:08
- 168 00:02:48
- 3668 01:01:08
- 13668 03:47:48
- 83668 23:14:28
-
- 请按任意键继续. . .
复制代码
作者: 77七 时间: 2024-3-5 17:21
可以用for /f把变量带出来- :Sec2Time
- setlocal enabledelayedexpansion
- set hh=0
- set mm=0
- set ss=0
- if %1 LSS 60 (
- endlocal & set %2=%hh%:%mm%:%1
- ) else if %1 LSS 3600 (
- set /a mm=%1 / 60
- set /a "ss=%1 - (mm * 60)"
- for /f "tokens=1-2" %%a in ("!mm! !ss!") do (
- endlocal
- set %2=%hh%:%%a:%%b
- )
- )
- goto :EOF
复制代码
也可以简单点
- set /a hh=%1/3600,mm=%1/60%%60,ss=%1%%60
复制代码
set /a 自带变量延迟扩展特性,不需要用!!包裹变量,这里如果将两个set /a 写成一行,set /a mm=%1/60,ss=%1-(!mm!*60),容易出错。crlf大佬的教程 [分享]Crlf 的拙作与收集的教程 提到了这点。
作者: 77七 时间: 2024-3-5 17:53
回复 8# hnfeng
- @echo off
- setlocal enabledelayedexpansion
- call :Sec2Time 3668 hhmmss
- echo %hhmmss%
- pause
- exit /b
-
- :Sec2Time
- setlocal
- set /a hh=%1/3600+100,mm=%1/60%%60+100,ss=%1%%60+100
- endlocal & set %2=%hh:~-2%:%mm:~-2%:%ss:~-2%
- exit /b
复制代码
作者: aloha20200628 时间: 2024-3-5 18:18
本帖最后由 aloha20200628 于 2024-3-5 18:20 编辑
回复 8# hnfeng
一楼的代码除了明显的几个错误如 ENABLEEXTENSIONS,$1,...已被指出外,真正致命的是如下两个错误》
一。在用括号包裹的if语块内,set/a 句式若含括号,须用双引号包裹,例如 set/a "v=5-(100/2)";
二。局部变量续命给全局变量的句式 endlocal & set %2=!v! 字面量应为 endlocal & set %2=%v% 但更关键的是该句式不能在复合语块(如if或for)内使用(建议用法见订正代码)
如下代码是订正后的示例代码(主要针对子过程部分),已测试通过- @echo off
- call :Sec2Time 13668 hhmmss
- echo 13668 %hhmmss%
- exit /b
-
- :Sec2Time
- setlocal enabledelayedexpansion
- set hh=0
- set mm=0
- set ss=0
- if %1 LSS 60 (
- set v=!hh!:!mm!:%1
- ) else (
- if %1 LSS 3600 (
- set /a mm=%1/60
- set /a ss=%1-!mm!*60
- set v=!hh!:!mm!:!ss!
- ) else (
- set /a hh = %1 / 3600
- REM 关键点之一如下
- set /a "mm=(%1-!hh!*3600)/60"
- set /a ss=%1-!hh!*3600-!mm!*60
- set v=!hh!:!mm!:!ss!
- )
- )
- REM 关键点之二如下
- endlocal & set %2=%v%
- exit/b
复制代码
作者: hnfeng 时间: 2024-3-5 20:37
可以用for /f把变量带出来
也可以简单点
set /a 自带变量延迟扩展特性,不需要用!!包裹变量,这里 ...
77七 发表于 2024-3-5 17:21
能否讲讲 mm=%1/60%%60 和 ss=%1%%60 中的 %%60 是什么意思?看不懂
作者: hnfeng 时间: 2024-3-5 20:38
回复 hnfeng
77七 发表于 2024-3-5 17:53
太精练了,多谢多谢
作者: hnfeng 时间: 2024-3-5 20:38
回复 hnfeng
一楼的代码除了明显的几个错误如 ENABLEEXTENSIONS,$1,...已被指出外,真正致命的是如下 ...
aloha20200628 发表于 2024-3-5 18:18
学习了,多谢指教
作者: 77七 时间: 2024-3-5 21:30
回复 12# hnfeng
算除以60的余数
作者: hnfeng 时间: 2024-3-6 08:22
回复 hnfeng
算除以60的余数
77七 发表于 2024-3-5 21:30
明白了,求余数。谢谢。 set /? 居然没有
在CMD窗口中 set /a mm=122%%60 会出错显示“找不到操作数”,在批处理里面却能运行
作者: ppll2030 时间: 2024-3-6 08:32
回复 16# hnfeng
cmd下,删掉一个 "%" 试试
作者: hnfeng 时间: 2024-3-6 08:36
本帖最后由 hnfeng 于 2024-3-6 08:39 编辑
回复 hnfeng
cmd下,删掉一个 "%" 试试
ppll2030 发表于 2024-3-6 08:32
删掉一个 % 貌似也行啊
CMD 里面使用 set /a mm=122^%60 貌似也可以取余数的
作者: Batcher 时间: 2024-3-6 08:55
回复 16# hnfeng
这里有:[一日一教学](44)设置变量:set
http://bbs.bathome.net/thread-14927-1-1.html
作者: hnfeng 时间: 2024-3-6 09:06
回复 hnfeng
这里有:[一日一教学](44)设置变量:set
Batcher 发表于 2024-3-6 08:55
学习学习,受益匪浅
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |