| |
| |
| |
| Dim strFolder, strType, SubDir |
| Dim strComputer, objWMIService, fso, strFilter |
| |
| strFolder = "r:\jpg" |
| strType = "bmp,jpg,png" |
| SubDir = 0 |
| |
| if (WScript.Arguments.Length) then strFolder = WScript.Arguments(0) |
| |
| strComputer = "." |
| Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _ |
| & strComputer & "\root\cimv2") |
| Set fso = CreateObject("Scripting.FileSystemObject") |
| |
| strFilter = "" |
| if (strType<>"*" AND strType<>"*.*") then |
| strFilter = " AND (Extension='" _ |
| & Replace(strType, ",", "' OR Extension='") & "')" |
| end if |
| |
| EnumViaWMI strFolder |
| rem 为什么不直接用fso遍历文件?因为fso有着与cmd的for相同的bug: |
| rem 遍历过程中如果文件名有变动,会造成重复遍历。(循环被扰乱?) |
| |
| Sub EnumViaWMI(strFolder) |
| Dim strQuery, colFiles, objFile, colFolders, objFolder |
| Dim strName, strExtension, strTime, n, strTail, strSuffix |
| |
| |
| strQuery = "SELECT * FROM CIM_DataFile " _ |
| & " WHERE (Drive='" & Left(strFolder, 2) & "' " _ |
| & " AND Path='" & Replace(Mid(strFolder, 3), "\", "\\") & "\\' " _ |
| & strFilter & ")" |
| |
| |
| if 0 then |
| Set colFiles = objWMIService.ExecQuery(strQuery, , 48) |
| For Each objFile in colFiles |
| strName = objFile.FileName & "." & objFile.Extension |
| n = Empty |
| do while 10 = objFile.Rename(strFolder & "\" _ |
| & "powerbat@bathome" & n & "_" & strName) |
| n = n + 1 |
| loop |
| Next |
| end if |
| |
| Set colFiles = objWMIService.ExecQuery(strQuery, , 48) |
| For Each objFile in colFiles |
| |
| strExtension = objFile.Extension |
| strTime = MyTime(objFile.LastModified) |
| n = Empty : strTail = Empty |
| if objFile.FileName <> strTime then |
| |
| |
| |
| |
| |
| rem WMI 判断文件是否已存在效率太低了,借用fso |
| do while fso.FileExists(strFolder & "\" _ |
| & strTime & strTail & "." & strExtension) |
| n = n + 1 : strTail = "_" & n |
| loop |
| end if |
| objFile.Rename(strFolder & "\" & strTime & strTail & "." & strExtension) |
| Next |
| |
| if (Not SubDir) then Exit Sub |
| Set colFolders = objWMIService.ExecQuery( _ |
| "ASSOCIATORS OF {Win32_Directory='" & strFolder & "'} WHERE " _ |
| & " AssocClass = Win32_SubDirectory ResultRole = PartComponent", , 48) |
| For Each objFolder in colFolders |
| EnumViaWMI objFolder.Name |
| Next |
| End Sub |
| |
| function MyTime(DateTime) |
| MyTime = Left(DateTime, 4) & "-" _ |
| & Mid(DateTime, 5, 2) & "-" _ |
| & Mid(DateTime, 7, 2) & " " _ |
| & Mid(DateTime, 9, 2) & "." _ |
| & Mid(DateTime, 11, 2) & "." _ |
| & Mid(DateTime, 13, 2) |
| end functionCOPY |