Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Multiple File Re-Name

Reply
 
Old 01-10-2009   #1 (permalink)
Hal


 
 

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 SpecsSystem Spec
Old 01-10-2009   #2 (permalink)
Pegasus \(MVP\)


 
 

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
>
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:
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 SpecsSystem Spec
Old 01-10-2009   #3 (permalink)
Hal


 
 

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 SpecsSystem Spec
Old 01-11-2009   #4 (permalink)
Pegasus \(MVP\)


 
 

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 SpecsSystem Spec
Old 01-11-2009   #5 (permalink)
ekkehard.horner


 
 

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:
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
ProcessFolder oFSO.GetFolder( sSourceDir )
Quote:

> 07.
> 08. Sub ProcessFolder(sDir)
Sub ProcessFolder( oDir )
Quote:

> 09. For Each oFile In oFSO.GetFolder(sDir).Files
For Each oFile In oDir.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
For Each oFolder In oDir.SubFolders
Quote:

> 18. ProcessFolder sDir & oFolder.Name & "\"
ProcessFolder oFolder
Quote:

> 19. Next
> 20. End Sub
[...]
My System SpecsSystem Spec
Old 01-11-2009   #6 (permalink)
Hal


 
 

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 SpecsSystem Spec
Old 01-11-2009   #7 (permalink)
Hal


 
 

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
> ProcessFolder oFSO.GetFolder( sSourceDir )
Quote:

> > 07.
> > 08. Sub ProcessFolder(sDir)
> Sub ProcessFolder( oDir )
Quote:

> > 09. For Each oFile In oFSO.GetFolder(sDir).Files
> For Each oFile In oDir.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
> For Each oFolder In oDir.SubFolders
Quote:

> > 18. ProcessFolder sDir & oFolder.Name & "\"
> ProcessFolder oFolder
Quote:

> > 19. Next
> > 20. End Sub
>
> [...]
>
My System SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46