Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search Folder Rename File Type 1

Status
Not open for further replies.

thec0dy

IS-IT--Management
Apr 16, 2010
41
US
I have this script below that works on the C: drive just fine. It does not work on the D: drive at all.

Basically it searches a folder root and sub folders for file types of .trace. Once it finds them it renames the extension to a .txt . I am not sure why it does not like the D:. It is probably pretty simple.


strComputer = "."
strFolderRoot = "D:\Test 2\"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
("Select * from Win32_Directory where Name Like '" & Replace(strFolderRoot, "\", "\\") & "%'")
For Each objFolder in colFolders
strTMP = Replace(Mid(objFolder.Name,4,Len(objFolder.Name)),"\","\\")
strQuery = "SELECT * FROM CIM_DATAFILE WHERE Drive = D:' " & _
"AND Path = '\\" & strTMP & "\\'"
Set colFiles = objWMIService.ExecQuery(strQuery)
For Each objFile In colFiles
MsgBox objFile.Extension
If objFile.Extension = "trace" Then
strNewName = objFile.Drive & objFile.Path & _
objFile.FileName & "." & "txt"
errResult = objFile.Rename(strNewName)
Wscript.Echo errResult
End If
Next
Next

MsgBox "Done
 
Please, define : It does not work

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
It does not rename the files on the D: drive. It does not popup the message box that says it found certain file extensions. If I create a similar directory structure on the C: drive it will work without an issue. It is strange that it only does it on the D: drive.

Drive = 'C:' will work, but Drive = 'D:' will not work.
 
That's a weird one, I can reproduce the problem, but I don't use WMI for this purpose, so I can't explain the behavior. I do notice, however, that it is exceedingly slow, so I guess it is parsing through every folder on the drive to find the ones you want...

I would use fso for this, especially since you know the base folder to start in, and can ignore all other folders. This code is modified from mrmovie's recursion FAQ: faq329-5515. Should do what you want.

Code:
Option Explicit
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Dim strDir, objDir
strDir = "d:\Test 2"
Set objDir = FSO.GetFolder(strDir)
RenameFiles(objDir)

Sub RenameFiles(pCurrentDir)
	Dim aItem, sBaseName
	
   For Each aItem In pCurrentDir.Files
      If fso.GetExtensionName(aItem.Path) = "trace" Then
         sBaseName = fso.GetParentFolderName(aItem.path) & _
         	"\" & fso.GetBaseName(aItem.path)

         'Wscript.echo "File will be renamed from " & _
         '	sBaseName & ".trace" & " to " & sBaseName & ".txt"

         fso.MoveFile sBaseName & ".trace", sBaseName & ".txt"
      End If
   Next

   For Each aItem In pCurrentDir.SubFolders
      RenameFiles(aItem)
   Next
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top