[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[日期时间] 删除10天之前的文件,如何更改日期格式为M/d/yyyy

请教各位大虾!下面是删除10天之前文件的代码,如何把日期格式yyyy-M-d成功更改为M/d/yyyy,谢谢!
  1. @echo off
  2. set FilePath=C:\test\
  3. set DaysAgo=10
  4. reg add "HKCU\Control Panel\International" /v sShortDate /t REG_SZ /d yyyy-M-d /f
  5. call :DateToDays %date:~0,4% %date:~5,2% %date:~8,2% PassDays
  6. set /a PassDays-=%DaysAgo%
  7. call :DaysToDate %PassDays% DstYear DstMonth DstDay
  8. set DstDate=%DstYear%-%DstMonth%-%DstDay%
  9. for /r "%FilePath%" %%a in (*.*) do (if "%%~ta" leq "%DstDate%" (if exist "%%a" (del /a /f /s /q "%%a")))
  10. :DateToDays %yy% %mm% %dd% days
  11. setlocal ENABLEEXTENSIONS
  12. set yy=%1&set mm=%2&set dd=%3
  13. if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
  14. set /a dd=100%dd%%%100,mm=100%mm%%%100
  15. set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
  16. set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
  17. endlocal&set %4=%j%&goto :eof
  18. :DaysToDate %days% yy mm dd
  19. setlocal ENABLEEXTENSIONS
  20. set /a a=%1+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
  21. set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
  22. set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
  23. (if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
  24. endlocal&set %2=%yy%&set %3=%mm%&set %4=%dd%&goto :eof
复制代码

把日期格式yyyy-M-d成功更改为M/d/yyyy

这样做的目的是什么

TOP

回复 2# DAIC

目的是我这儿有的电脑不支持中文,英文倒每台机都已默认安装,M/d/yyyy是英文格式,能否在不安装中文的前提下,更改为M/d/yyyy实现同样的删除文件的功能。

TOP

本帖最后由 apang 于 2014-12-4 15:42 编辑

第9行:
  1. set "DstDate=%DstMonth%/%DstDay%/%DstYear%"
复制代码
第6行:
  1. call :DateToDays %date:~6,4% %date:~0,2% %date:~3,2% PassDays
复制代码
第5行: yyyy-M-d 改成 M/d/yyyy

------------------------------------------------------------------------
卧槽,又弄错了
  1. @echo off
  2. set FilePath=D:\test\
  3. set DaysAgo=10
  4. reg add "HKCU\Control Panel\International" /v sShortDate /t REG_SZ /d "M/d/yyyy" /f
  5. call :DateToDays %date:~6,4% %date:~0,2% %date:~3,2% PassDays
  6. set /a PassDays-=%DaysAgo%
  7. call :DaysToDate %PassDays% DstYear DstMonth DstDay
  8. set "DstDate=%DstYear%-%DstMonth%-%DstDay%"
  9. for /r "%FilePath%" %%a in (*) do (
  10.     for /f "tokens=1-3 delims=/ " %%i in ("%%~ta") do (
  11.         if "%%k-%%i-%%j" leq "%DstDate%" if exist "%%a" del /a /f /s /q "%%a"
  12.     )
  13. )
  14. pause
  15. :DateToDays %yy% %mm% %dd% days
  16. setlocal ENABLEEXTENSIONS
  17. set yy=%1&set mm=%2&set dd=%3
  18. if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
  19. set /a dd=100%dd%%%100,mm=100%mm%%%100
  20. set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
  21. set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
  22. endlocal&set %4=%j%&goto :eof
  23. :DaysToDate %days% yy mm dd
  24. setlocal ENABLEEXTENSIONS
  25. set /a a=%1+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
  26. set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
  27. set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
  28. (if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
  29. endlocal&set %2=%yy%&set %3=%mm%&set %4=%dd%&goto :eof
复制代码
1

评分人数

    • sinun: 谢谢支持!!!技术 + 1

TOP

回复 3# sinun


    日期的格式和是否安装中文没有什么关系吧

TOP

本帖最后由 apang 于 2014-12-5 20:48 编辑

来个vbs,不用动注册表,不过隐藏文件删不掉
  1. Dim strFolder, intAgo, objWMI, objDate, strDate
  2. ''目标文件夹路径
  3. strFolder = "D:\Test"
  4. ''10天之前
  5. intAgo = 10
  6. Set objDate = CreateObject("WbemScripting.SWbemDateTime")
  7. objDate.SetVarDate Now-intAgo, true
  8. strDate = Left(objDate.Value, 8)
  9. Set objDate = Nothing
  10. Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
  11. call EnumFiles(strFolder)
  12. Set objWMI = Nothing
  13. Set colFolders = Nothing
  14. Set colFiles = Nothing
  15. MsgBox "OK"
  16. Sub EnumFiles(strFolder)
  17.         Dim strDrv, strPath, colFiles, objFile, colFolders, objFolder
  18.         strDrv =  Left(strFolder, 2)
  19.         strPath = Replace(Mid(strFolder, 3), "\", "\\") & "\\"
  20.         Set colFiles = objWMI.ExecQuery("select * from CIM_DataFile where Drive='" & strDrv & "' and Path='" & strPath & "'")
  21.         For Each objFile in colFiles
  22.                 If strDate >= Left(objFile.LastModified, 8) Then objFile.Delete
  23.         Next
  24.         Set colFolders = objWMI.ExecQuery("Associators of {Win32_Directory='" & strFolder & "'} where AssocClass = Win32_SubDirectory ResultRole = PartComponent")
  25.         For Each objFolder in colFolders
  26.                 call EnumFiles(objFolder.Name)
  27.         Next
  28. End Sub
复制代码

TOP

回复 5# DAIC


如果不安装中文,yyyy-M-d将不能认识别

TOP

回复 7# sinun


改成 月/日/年 的格式会导致 if "%%~ta" leq "%DstDate%" 这个地方发生误判
比如 12/06/2013 会大于 12/05/2014

如果你的系统支持 forfiles 命令或者 powershell 的话,建议不要使用顶楼这个方案。

TOP

回复 8# DAIC


    谢谢指导!

TOP

来个vbs,不用动注册表,不过隐藏文件删不掉
apang 发表于 2014-12-5 20:42


目录也对,怎么10天前的文件删除不了呢?请求支持!

TOP

回复 10# sinun


    不能删除是个什么状况?报错了没有?你用什么系统测试的呢?

TOP

回复 10# sinun


    你该不会保存的是 .bat 文件吧?

TOP

返回列表