批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程
[批处理文件精品]批处理版照片整理器[批处理文件精品]纯批处理备份&还原驱动在线第三方下载
返回列表 发帖

[原创] vbs脚本调用wmi将文件重命名为文件修改时间(精确到秒)

如需转载请注明出处:
http://www.bathome.net/thread-15803-1-1.html
  1. 'vbs脚本调用wmi将文件重命名为文件修改时间(精确到秒)
  2. 'By powerbat @ www.bathome.net 批处理之家 vbs脚本调用wmi
  3. Dim strFolder, strType, SubDir
  4. Dim strComputer, objWMIService, fso, strFilter
  5. strFolder = "r:\jpg" '文件所在目录
  6. strType = "bmp,jpg,png" '文件类型
  7. SubDir = 0 '是否包含子目录
  8. if (WScript.Arguments.Length) then strFolder = WScript.Arguments(0)
  9. strComputer = "."
  10. Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
  11.     & strComputer & "\root\cimv2")
  12. Set fso = CreateObject("Scripting.FileSystemObject")
  13. strFilter = ""
  14. if (strType<>"*" AND strType<>"*.*") then
  15.   strFilter = " AND (Extension='" _
  16.     & Replace(strType, ",", "' OR Extension='") & "')"
  17. end if
  18. EnumViaWMI strFolder
  19. rem 为什么不直接用fso遍历文件?因为fso有着与cmd的for相同的bug:
  20. rem 遍历过程中如果文件名有变动,会造成重复遍历。(循环被扰乱?)
  21. Sub EnumViaWMI(strFolder)
  22.   Dim strQuery, colFiles, objFile, colFolders, objFolder
  23.   Dim strName, strExtension, strTime, n, strTail, strSuffix
  24.   'WScript.Echo(strFolder)
  25.   strQuery = "SELECT * FROM CIM_DataFile " _
  26.     & " WHERE (Drive='" & Left(strFolder, 2) & "' " _
  27.     & " AND Path='" & Replace(Mid(strFolder, 3), "\", "\\") & "\\' " _
  28.     & strFilter & ")"
  29.   '这次重命名目的:当文件修改时间相同且新文件名可能已存在时,编号可以连续。
  30.   if 0 then
  31.     Set colFiles = objWMIService.ExecQuery(strQuery, , 48)
  32.     For Each objFile in colFiles
  33.       strName = objFile.FileName & "." & objFile.Extension
  34.       n = Empty
  35.       do while 10 = objFile.Rename(strFolder & "\" _
  36.           & "powerbat@bathome" & n & "_" & strName)
  37.         n = n + 1
  38.       loop
  39.     Next
  40.   end if
  41.   Set colFiles = objWMIService.ExecQuery(strQuery, , 48)
  42.   For Each objFile in colFiles
  43.     'WScript.Echo objFile.Name
  44.     strExtension = objFile.Extension
  45.     strTime = MyTime(objFile.LastModified)
  46.     n = Empty : strTail = Empty
  47.     if objFile.FileName <> strTime then
  48.       '返回码为10表示文件已存在
  49.       'do while 10 = objFile.Rename(strFolder & "\" _
  50.       '    & strTime & strTail & "." & strExtension)
  51.       '  n = n + 1 : strTail = "_" & n
  52.       'loop
  53.       rem WMI 判断文件是否已存在效率太低了,借用fso
  54.       do while fso.FileExists(strFolder & "\" _
  55.           & strTime & strTail & "." & strExtension)
  56.         n = n + 1 : strTail = "_" & n
  57.       loop
  58.     end if
  59.     objFile.Rename(strFolder & "\" & strTime & strTail & "." & strExtension)
  60.   Next
  61.   if (Not SubDir) then Exit Sub
  62.   Set colFolders = objWMIService.ExecQuery( _
  63.     "ASSOCIATORS OF {Win32_Directory='" & strFolder & "'} WHERE " _
  64.     & " AssocClass = Win32_SubDirectory ResultRole = PartComponent", , 48)
  65.   For Each objFolder in colFolders
  66.     EnumViaWMI objFolder.Name
  67.   Next
  68. End Sub
  69. function MyTime(DateTime)
  70. MyTime = Left(DateTime, 4) & "-" _
  71.     & Mid(DateTime, 5, 2) & "-" _
  72.     & Mid(DateTime, 7, 2) & " " _
  73.     & Mid(DateTime, 9, 2) & "." _
  74.     & Mid(DateTime, 11, 2) & "." _
  75.     & Mid(DateTime, 13, 2)
  76. end function
复制代码
1

评分人数

至少XP绝对是有这个bug的。可能后来的系统修正了。
  1. strFolder = "D:\jpg"
  2. Set fso = CreateObject("Scripting.FileSystemObject")
  3. Set fc = fso.GetFolder(strFolder).Files
  4. n = 0
  5. for each f in fc
  6.     if InStr(1, Right(f.name,3), "jpg", 1) then
  7.         wsh.echo f.path
  8.         n = n + 1
  9.         f.name = n & "_" & f.name
  10.     end if
  11. next
复制代码
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

C:\Documents and Settings\Administrator>md D:\jpg

C:\Documents and Settings\Administrator>cd /d D:\jpg

D:\jpg>for /l %a in (1 1 11) do @cd.>%a.jpg

D:\jpg>cscript D:\fsoren.vbs
D:\jpg\1.jpg
D:\jpg\10.jpg
D:\jpg\11.jpg
D:\jpg\1_1.jpg
D:\jpg\2.jpg
D:\jpg\3.jpg
D:\jpg\4.jpg
D:\jpg\5.jpg
D:\jpg\6.jpg
D:\jpg\7.jpg
D:\jpg\8.jpg
D:\jpg\9.jpg

D:\jpg>

和cmd一样,这个bug不是必现的,还没找到规律。

TOP

rem 为什么不直接用fso遍历文件?因为fso有着与cmd的for相同的bug:
rem 遍历过程中如果文件名有变动,会造成重复遍历。(循环被扰乱?)

我经常用fso重命名文件,为啥没碰到过?

TOP

返回列表