![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Multiple File Re-Name Greetings, I was browsing the online scripting guide and this newsgroup looking for a solution to handle renaming multiple files over several subdirectories. I did not have any luck location a solution :-( The scripting guide gave the example below. Can this be modified to look at a mapped drive, parse the files starting at G:\blah\blah\blah\ and rename three specific characters which exist in all the file names? e.g. M3O needs to be changed to R3T. As always, your patience and help is greatly appreciated. strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colFiles = objWMIService.ExecQuery _ ("SELECT * FROM Cim_Datafile WHERE Name = " _ & "'c:\\scripts\\toggle_service.vbs'") For Each objFile in colFiles errResult = objFile.Rename("c:\scripts\toggle_service.old") Wscript.Echo errResult Next |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Multiple File Re-Name "Hal" <Hal@xxxxxx> wrote in message news:065D50D3-A8BA-43A8-95ED-C0B711364AA9@xxxxxx Quote: > Greetings, > > I was browsing the online scripting guide and this newsgroup looking for a > solution to handle renaming multiple files over several subdirectories. I > did > not have any luck location a solution :-( > > The scripting guide gave the example below. Can this be modified to look > at > a mapped drive, parse the files starting at G:\blah\blah\blah\ and rename > three specific characters which exist in all the file names? e.g. M3O > needs > to be changed to R3T. > > As always, your patience and help is greatly appreciated. > > > strComputer = "." > Set objWMIService = GetObject("winmgmts:" _ > & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") > Set colFiles = objWMIService.ExecQuery _ > ("SELECT * FROM Cim_Datafile WHERE Name = " _ > & "'c:\\scripts\\toggle_service.vbs'") > For Each objFile in colFiles > errResult = objFile.Rename("c:\scripts\toggle_service.old") > Wscript.Echo errResult > Next > FSO object is much faster, even though you have to write your own recursive code. Here is what you could do: 01. sOld = "txt" 02. sNew = "XYZ" 03. sSourceDir = "d:\Some Folder\" 04. Set oFSO = CreateObject("Scripting.FileSystemObject") 05. 06. ProcessFolder sSourceDir 07. 08. Sub ProcessFolder(sDir) 09. For Each oFile In oFSO.GetFolder(sDir).Files 10. If InStr(1, oFile.Name, sold, 1) > 0 Then 11. WScript.Echo "Renaming """ & sDir & oFile.Name _ 12. & """ to """ & Replace(oFile.Name, sOld, sNew, 1, -1, 1) & """" 13. ' oFile.Name = Replace(oFile.Name, sOld, sNew, 1, -1, 1) 14. End If 15. Next 17. For Each oFolder In oFSO.GetFolder(sDir).SubFolders 18. ProcessFolder sDir & oFolder.Name & "\" 19. Next 20. End Sub Adjust Lines 1, 2 and 3 to suit your environment, then use cscript.exe to run the code. When you're happy with the result, activate the code by uncommenting Line 13. |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Multiple File Re-Name Pegasus, With a move of the "\" string at the end of line 18, the script worked as needed in my trial environment. I think I'll add some InputBox functionality to cover the variables. Thanks for your expertise sOld = "M3O" sNew = "R3T" sSourceDir = "I:\RenTest" Set oFSO = CreateObject("Scripting.FileSystemObject") ProcessFolder sSourceDir Sub ProcessFolder(sDir) For Each oFile In oFSO.GetFolder(sDir).Files If InStr(1, oFile.Name, sold, 1) > 0 Then WScript.Echo "Renaming """ & sDir & "\" & oFile.Name _ & """ to """ & Replace(oFile.Name, sOld, sNew, 1, -1, 1) & """" oFile.Name = Replace(oFile.Name, sOld, sNew, 1, -1, 1) End If Next For Each oFolder In oFSO.GetFolder(sDir).SubFolders ProcessFolder sDir & "\" & oFolder.Name Next End Sub Quote: > 01. sOld = "txt" > 02. sNew = "XYZ" > 03. sSourceDir = "d:\Some Folder\" > 04. Set oFSO = CreateObject("Scripting.FileSystemObject") > 05. > 06. ProcessFolder sSourceDir > 07. > 08. Sub ProcessFolder(sDir) > 09. For Each oFile In oFSO.GetFolder(sDir).Files > 10. If InStr(1, oFile.Name, sold, 1) > 0 Then > 11. WScript.Echo "Renaming """ & sDir & oFile.Name _ > 12. & """ to """ & Replace(oFile.Name, sOld, sNew, 1, -1, 1) & """" > 13. ' oFile.Name = Replace(oFile.Name, sOld, sNew, 1, -1, 1) > 14. End If > 15. Next > 17. For Each oFolder In oFSO.GetFolder(sDir).SubFolders > 18. ProcessFolder sDir & oFolder.Name & "\" > 19. Next > 20. End Sub > > Adjust Lines 1, 2 and 3 to suit your environment, then use cscript.exe to > run the code. When you're happy with the result, activate the code by > uncommenting Line 13. > > > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Multiple File Re-Name Thanks for the feedback. The "move" you mention became necessary because you changed my code a little. Here is what I wrote: 03. sSourceDir = "d:\Some Folder\" and here is your code: 03. sSourceDir = "I:\RenTest" See the difference? "Hal" <Hal@xxxxxx> wrote in message news:4D1B3773-9F63-48EB-B239-54AB854B93DC@xxxxxx Quote: > Pegasus, > > With a move of the "\" string at the end of line 18, the script worked as > needed in my trial environment. > > I think I'll add some InputBox functionality to cover the variables. > > Thanks for your expertise > > > sOld = "M3O" > sNew = "R3T" > sSourceDir = "I:\RenTest" > Set oFSO = CreateObject("Scripting.FileSystemObject") > > ProcessFolder sSourceDir > > Sub ProcessFolder(sDir) > For Each oFile In oFSO.GetFolder(sDir).Files > If InStr(1, oFile.Name, sold, 1) > 0 Then > WScript.Echo "Renaming """ & sDir & "\" & oFile.Name _ > & """ to """ & Replace(oFile.Name, sOld, sNew, 1, -1, 1) & """" > oFile.Name = Replace(oFile.Name, sOld, sNew, 1, -1, 1) > End If > Next > For Each oFolder In oFSO.GetFolder(sDir).SubFolders > ProcessFolder sDir & "\" & oFolder.Name > Next > End Sub > Quote: >> 01. sOld = "txt" >> 02. sNew = "XYZ" >> 03. sSourceDir = "d:\Some Folder\" >> 04. Set oFSO = CreateObject("Scripting.FileSystemObject") >> 05. >> 06. ProcessFolder sSourceDir >> 07. >> 08. Sub ProcessFolder(sDir) >> 09. For Each oFile In oFSO.GetFolder(sDir).Files >> 10. If InStr(1, oFile.Name, sold, 1) > 0 Then >> 11. WScript.Echo "Renaming """ & sDir & oFile.Name _ >> 12. & """ to """ & Replace(oFile.Name, sOld, sNew, 1, -1, 1) & """" >> 13. ' oFile.Name = Replace(oFile.Name, sOld, sNew, 1, -1, 1) >> 14. End If >> 15. Next >> 17. For Each oFolder In oFSO.GetFolder(sDir).SubFolders >> 18. ProcessFolder sDir & oFolder.Name & "\" >> 19. Next >> 20. End Sub >> >> Adjust Lines 1, 2 and 3 to suit your environment, then use cscript.exe to >> run the code. When you're happy with the result, activate the code by >> uncommenting Line 13. >> >> >> |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Multiple File Re-Name Pegasus (MVP) schrieb: Quote: > "Hal" <Hal@xxxxxx> wrote in message > news:065D50D3-A8BA-43A8-95ED-C0B711364AA9@xxxxxx Quote: >> Greetings, >> >> I was browsing the online scripting guide and this newsgroup looking for a >> solution to handle renaming multiple files over several subdirectories. I Quote: Quote: >> > While you could use WMI to perform the bulk rename, you will find that the > FSO object is much faster, even though you have to write your own recursive > code. Here is what you could do: Quote: > 01. sOld = "txt" > 02. sNew = "XYZ" > 03. sSourceDir = "d:\Some Folder\" > 04. Set oFSO = CreateObject("Scripting.FileSystemObject") > 05. > 06. ProcessFolder sSourceDir Quote: > 07. > 08. Sub ProcessFolder(sDir) Quote: > 09. For Each oFile In oFSO.GetFolder(sDir).Files Quote: > 10. If InStr(1, oFile.Name, sold, 1) > 0 Then > 11. WScript.Echo "Renaming """ & sDir & oFile.Name _ > 12. & """ to """ & Replace(oFile.Name, sOld, sNew, 1, -1, 1) & """" > 13. ' oFile.Name = Replace(oFile.Name, sOld, sNew, 1, -1, 1) > 14. End If > 15. Next > 17. For Each oFolder In oFSO.GetFolder(sDir).SubFolders Quote: > 18. ProcessFolder sDir & oFolder.Name & "\" Quote: > 19. Next > 20. End Sub |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Multiple File Re-Name In a word, Syntax counts :-) "Pegasus (MVP)" wrote: Quote: > Thanks for the feedback. The "move" you mention became necessary because you > changed my code a little. Here is what I wrote: > 03. sSourceDir = "d:\Some Folder\" > and here is your code: > 03. sSourceDir = "I:\RenTest" > See the difference? > > > "Hal" <Hal@xxxxxx> wrote in message > news:4D1B3773-9F63-48EB-B239-54AB854B93DC@xxxxxx Quote: > > Pegasus, > > > > With a move of the "\" string at the end of line 18, the script worked as > > needed in my trial environment. > > > > I think I'll add some InputBox functionality to cover the variables. > > > > Thanks for your expertise > > > > > > sOld = "M3O" > > sNew = "R3T" > > sSourceDir = "I:\RenTest" > > Set oFSO = CreateObject("Scripting.FileSystemObject") > > > > ProcessFolder sSourceDir > > > > Sub ProcessFolder(sDir) > > For Each oFile In oFSO.GetFolder(sDir).Files > > If InStr(1, oFile.Name, sold, 1) > 0 Then > > WScript.Echo "Renaming """ & sDir & "\" & oFile.Name _ > > & """ to """ & Replace(oFile.Name, sOld, sNew, 1, -1, 1) & """" > > oFile.Name = Replace(oFile.Name, sOld, sNew, 1, -1, 1) > > End If > > Next > > For Each oFolder In oFSO.GetFolder(sDir).SubFolders > > ProcessFolder sDir & "\" & oFolder.Name > > Next > > End Sub > > Quote: > >> 01. sOld = "txt" > >> 02. sNew = "XYZ" > >> 03. sSourceDir = "d:\Some Folder\" > >> 04. Set oFSO = CreateObject("Scripting.FileSystemObject") > >> 05. > >> 06. ProcessFolder sSourceDir > >> 07. > >> 08. Sub ProcessFolder(sDir) > >> 09. For Each oFile In oFSO.GetFolder(sDir).Files > >> 10. If InStr(1, oFile.Name, sold, 1) > 0 Then > >> 11. WScript.Echo "Renaming """ & sDir & oFile.Name _ > >> 12. & """ to """ & Replace(oFile.Name, sOld, sNew, 1, -1, 1) & """" > >> 13. ' oFile.Name = Replace(oFile.Name, sOld, sNew, 1, -1, 1) > >> 14. End If > >> 15. Next > >> 17. For Each oFolder In oFSO.GetFolder(sDir).SubFolders > >> 18. ProcessFolder sDir & oFolder.Name & "\" > >> 19. Next > >> 20. End Sub > >> > >> Adjust Lines 1, 2 and 3 to suit your environment, then use cscript.exe to > >> run the code. When you're happy with the result, activate the code by > >> uncommenting Line 13. > >> > >> > >> > > |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Multiple File Re-Name ekke, Nice take on the solution Pegasus provided. There truly is 'more than one way to skin the cat' "ekkehard.horner" wrote: Quote: > Pegasus (MVP) schrieb: Quote: > > "Hal" <Hal@xxxxxx> wrote in message > > news:065D50D3-A8BA-43A8-95ED-C0B711364AA9@xxxxxx Quote: > >> Greetings, > >> > >> I was browsing the online scripting guide and this newsgroup looking for a > >> solution to handle renaming multiple files over several subdirectories. I Quote: Quote: > >> > > While you could use WMI to perform the bulk rename, you will find that the > > FSO object is much faster, even though you have to write your own recursive > > code. Here is what you could do: > You can avoid the error prone string manipulation by using folder objects. > Quote: > > 01. sOld = "txt" > > 02. sNew = "XYZ" > > 03. sSourceDir = "d:\Some Folder\" > > 04. Set oFSO = CreateObject("Scripting.FileSystemObject") > > 05. > > 06. ProcessFolder sSourceDir Quote: > > 07. > > 08. Sub ProcessFolder(sDir) Quote: > > 09. For Each oFile In oFSO.GetFolder(sDir).Files Quote: > > 10. If InStr(1, oFile.Name, sold, 1) > 0 Then > > 11. WScript.Echo "Renaming """ & sDir & oFile.Name _ > > 12. & """ to """ & Replace(oFile.Name, sOld, sNew, 1, -1, 1) & """" > > 13. ' oFile.Name = Replace(oFile.Name, sOld, sNew, 1, -1, 1) > > 14. End If > > 15. Next > > 17. For Each oFolder In oFSO.GetFolder(sDir).SubFolders Quote: > > 18. ProcessFolder sDir & oFolder.Name & "\" Quote: > > 19. Next > > 20. End Sub > [...] > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Multiple file selection | General Discussion | |||
| How do I Mail a file with CC or multiple addresses? | PowerShell | |||
| Finding multiple file extensions | Vista file management | |||
| IE7 Save File option ceases to appear after multiple file download | Vista General | |||
| multiple file selection bug? | Vista file management | |||