[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
将以下代码保存的 批处理文件放在 szt 所属的父目录中
  1. @echo off & setlocal enabledelayedexpansion
  2. REM 备份原始日期时间格式
  3. for /f "tokens=2*" %%a in ('reg query "HKCU\Control Panel\International" /v sShortDate^|find/i"sSh"') do set "sShortDate=%%~b"
  4. for /f "tokens=2*" %%a in ('reg query "HKCU\Control Panel\International" /v sTimeFormat^|find/i"sTi"') do set "sTimeFormat=%%~b"
  5. REM 修改日期时间格式
  6. >nul reg add "HKCU\Control Panel\International" /v sShortDate /t REG_SZ /d "yyyyMMdd" /f
  7. >nul reg add "HKCU\Control Panel\International" /v sTimeFormat /t REG_SZ /d "HH:mm:ss" /f
  8. set "timeField=C"
  9. set /p "timeField=请选择时间类型, 默认=!timeField! (C 创建时间; A 上次访问时间; W 上次写入的时间):"
  10. if exist "%~dp0szt\列表文件.txt" del /f /q "%~dp0szt\列表文件.txt"
  11. > "列表文件.txt" (
  12.   for /f "tokens=1-3*" %%a in (
  13.     'dir szt /a-d /od /t!timeField! /-c^|findstr "^[1-9].*[0-9][0-9]:[0-9][0-9]"') do (
  14.     <nul set /p "=%%d|%%c|%%a|%%b"
  15. ) )
  16. move "列表文件.txt" "%~dp0szt\"
  17. start "" "%~dp0szt\列表文件.txt"
  18. REM 还原原始日期时间格式
  19. >nul reg add "HKCU\Control Panel\International" /v sShortDate /t REG_SZ /d "!sShortDate!" /f
  20. >nul reg add "HKCU\Control Panel\International" /v sTimeFormat /t REG_SZ /d "!sTimeFormat!" /f
复制代码

TOP

回复 3# tousyo88

17 行改为
  1. echo,%%d^|%%c^|%%a^|%%b
复制代码
即可换行输出

文件时间的秒域输出, 未找到不利用工具或其它脚本的方法.

TOP

谢谢 5 楼 gawk 提醒了我 wmic, 改了下
  1. @echo off & setlocal enabledelayedexpansion
  2. set "fn=列表文件.txt"
  3. set "fdir=szt"
  4. REM 备份原始日期时间格式
  5. for /f "tokens=2*" %%a in ('reg query "HKCU\Control Panel\International" /v sShortDate^|find/i"sSh"') do set "sShortDate=%%~b"
  6. for /f "tokens=2*" %%a in ('reg query "HKCU\Control Panel\International" /v sTimeFormat^|find/i"sTi"') do set "sTimeFormat=%%~b"
  7. REM 修改日期时间格式
  8. >nul reg add "HKCU\Control Panel\International" /v sShortDate /t REG_SZ /d "yyyyMMdd" /f
  9. >nul reg add "HKCU\Control Panel\International" /v sTimeFormat /t REG_SZ /d "HH:mm:ss" /f
  10. set "timeField=C"
  11. set /p "timeField=请选择时间类型, 默认=!timeField! (C 创建时间; A 上次访问时间; W 上次写入的时间):"
  12. for %%a in ("C CreationDate" "A LastAccessed" "W LastModified"
  13.   ) do for /f "tokens=1-2" %%b in ("%%~a") do if /i %%b==!timeField! set "filetime=%%c"
  14. if exist "%~dp0!fdir!\!fn!" del /f /q "%~dp0!fdir!\!fn!"
  15. > "!fn!" (
  16.   for /f "tokens=1-3*" %%a in (
  17.     'dir !fdir! /a-d /od /t!timeField! /-c^|findstr "^[1-9].*[0-9][0-9]:[0-9][0-9]"') do (
  18.     <nul set /p "=%%d|%%c|%%a|"
  19.     set "pth=%~dp0!fdir!\%%d"
  20.     for /f %%t in ('wmic datafile where name^="!pth:\=\\!" get !filetime! ^| find "."') do (
  21.       set "ft=%%t"
  22.       echo !ft:~8,2!:!ft:~10,2!:!ft:~12,2!
  23. ) ) )
  24. move "!fn!" "%~dp0!fdir!\"
  25. start "" "%~dp0!fdir!\!fn!"
  26. REM 还原原始日期时间格式
  27. >nul reg add "HKCU\Control Panel\International" /v sShortDate /t REG_SZ /d "!sShortDate!" /f
  28. >nul reg add "HKCU\Control Panel\International" /v sTimeFormat /t REG_SZ /d "!sTimeFormat!" /f
  29. exit
复制代码

TOP

本帖最后由 neorobin 于 2012-4-20 15:32 编辑

回复 12# tousyo88
WinXP CHS 32 位环境下
%%~ti 的方式也是不能输出 秒 的

下面仍使用 wmic datafile, 不受日期时间格式影响, 如果需要特定的排序方式, 可以在 16 行用 dir 适当的参数修改一下
工作目录使用全路径, 批处理文件 可以放在 除 工作目录之内 的任何地方
  1. @echo off & setlocal enabledelayedexpansion
  2. set "fn=列表文件.txt"
  3. set "fdir=d:\szt" & rem 不含引号的工作目录全路径
  4. set "timeField=C"
  5. set /p "timeField=请选择时间类型, 默认=!timeField! (C 创建时间; A 上次访问时间; W 上次写入的时间):"
  6. for %%a in ("C CreationDate" "A LastAccessed" "W LastModified"
  7.   ) do for /f "tokens=1-2" %%b in ("%%~a") do if /i %%b==!timeField! set "filetime=%%c"
  8. cd /d "!fdir!"
  9. if exist "!fn!" del /f /q "!fn!"
  10. if exist "TempWmicBatchFile.bat" del /f /q "TempWmicBatchFile.bat"
  11. <nul > "!temp!\!fn!" (
  12.   for /f "tokens=*" %%a in ('dir /b /a-d') do (
  13.     set "pth=%%~fa"
  14.     set /p "=%%~nxa|%%~za|"
  15.     for /f %%t in ('wmic datafile where name^="!pth:\=\\!" get !filetime! ^| find "."') do (
  16.       set "ft=%%t"
  17.       echo !ft:~0,8!^|!ft:~8,2!:!ft:~10,2!:!ft:~12,2!
  18.     )
  19.   )
  20. )
  21. move "!temp!\!fn!" "!fdir!"
  22. start "" "!fdir!\!fn!"
复制代码

TOP

返回列表