Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Renaming Files

215 views
Skip to first unread message

Carl Muffoletto

unread,
Mar 6, 2004, 2:21:09 PM3/6/04
to
I have hundreds of files that have an underscore in the file name. I need to
rename the files removing the underscore and replacing it with a space. Like
this "file_file_file.txt" to "file file file.txt" Can I do this with a batch
fileor windows script? If not can someone suggest a way this can be done.

Thank you.


Michael Harris \(MVP\)

unread,
Mar 6, 2004, 4:06:46 PM3/6/04
to


'===========================================================
' DemoRecursiveFileSearch.vbs
'
' Demonstrates recursive file system search. This
' example searches the folder tree defined by the
' RootPath variable.
'
' In this example, RootPath = "C:\Program Files"
'
' SearchFolders takes a folder object argument and
' walks the Files collection of the folder, passing
' each file object to a SelectFile routine and then
' walks the SubFolders collection of the folder,
' calling itself recursively for each subfolder.
'
' The SelectFile routine applies whatever selection
' criteria is appropriate and adds the file object
' to a global SelectedFiles collection (dictionary).
'
' In this example, files with an underscore (_) in the
' file name are selected.
'
' On return to the mainline script, the SelectedFiles
' collection is walked and each selected file is
' passed to the ProcessFile routine.
'
' In this example, the full path of each file is
' simply echoed to the console window.
'
'===========================================================
' To adapt this example...
'
' * change the RootPath variable value
' * change the SelectFile selection logic
' * change the ProcessFile processing logic
'
'===========================================================
'
'================================== begin mainline
'
Option Explicit

Dim RootPath
RootPath = "C:\Program Files"

Dim SelectedFiles
Set SelectedFiles = CreateObject("Scripting.Dictionary")

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Dim RootFolder
Set RootFolder = fso.GetFolder(RootPath)

Call SearchFolders(RootFolder)

WScript.Echo SelectedFiles.Count & " files selected..."

Dim SelectedFile
For Each SelectedFile In SelectedFiles.Items
Call ProcessFile(SelectedFile)
Next

WScript.Echo "Done..."
WScript.Quit
'
'================================== End mainline

'================================== Begin procedures
'
'================================== ProcessFile
'
Sub ProcessFile(argFile)

WScript.Echo argFile.Path

End Sub

'================================== SelectFile
'
Sub SelectFile(argFile)

If InStr(argFile.Name, "_") Then
Set SelectedFiles(argFile.Path) = argFile
End If

End Sub

'================================== SearchFolders
'
Sub SearchFolders(argFolder)
Dim file, subfolder

For Each file In argFolder.Files
Call SelectFile(file)
Next

For Each subfolder In argFolder.SubFolders
Call SearchFolders(subfolder)
Next

End Sub
'
'================================== End procedures

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US

0 new messages