FP2000: How to Remove Spaces from File Names and Folder Names by Using VBA

ID: Q242307


The information in this article applies to:
  • Microsoft FrontPage 2000


SUMMARY

The sample code in the "More Information" section of this article replaces spaces in file names and folder names of the current Web with underscore characters. To run this sample code, paste the code into a module in the Visual Basic Editor and run the myProcedure macro.


MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp

For more information about using the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:
Q212536 OFF2000: How to Run Sample Code from Knowledge Base Articles


This method is not suitable for use with discussion Webs. Renaming files or folders in a discussion Web generates a run-time error.


Sub myProcedure()
       
   'Set up a variable to hold the result of a message box
   Dim myMsgResults As VbMsgBoxResult

   'Check to see if a web is open.
   If Application.ActiveWeb Is Nothing Then

       'If no web is open, display a message box.
       MsgBox "Please open a web before running this macro.", _
          vbOKOnly + vbExclamation

       '. . . and end the macro.
       Exit Sub

   End If
       
   '////////////////////////////////////////////////////////////////// 
   'In order for us to update the HTML in our files, there must be no
   'pages open.  To check for this, we check the Count property of
   'the PageWindows collection.  The PageWindows collection's Count
   'property is equal to the number of files that are currently open.
   '////////////////////////////////////////////////////////////////// 

   'Get the count of the PageWindows collection
   If ActiveWeb.ActiveWebWindow.PageWindows.Count <> 0 Then

       'Assign the result of a message box to the variable
       myMsgResults = _
         MsgBox("Please close all files before running this macro.", _
           vbOKOnly + vbExclamation)

       'If the user clicks OK in our message box, exit the macro.
       'This variable will equal vbOK as soon as the user clicks
       'the OK button in the message box.
       If myMsgResults = vbOK Then Exit Sub

   End If

   'Set up a variable for the WebFolder we're passing to the
   'RemoveSpace procedure
   Dim myTempFolder As WebFolder
       
   'Set the myWebFolder variable to equal the rootfolder of
   'the active web
   Set myTempFolder = ActiveWeb.RootFolder
       
   'Now call the RemoveSpace procedure and pass myTempFolder to it.
   RemoveSpace myTempFolder
       
   'Display a dialog saying we're done.
   myMsgResults = MsgBox _
     ("Removal of spaces completed.", vbOKOnly + vbInformation)
       
End Sub

Sub RemoveSpace(myWebFolder As WebFolder)
       
   'Set up all variables
   Dim myFiles As WebFiles
   Dim myFile As WebFile
   Dim myFolders As WebFolders
   Dim myFileName As String
       
   'Set myFiles equal to the current folder's Files collection
   Set myFiles = myWebFolder.Files

   'Set myFolders equal to the current folder's Folders collection
   Set myFolders = myWebFolder.Folders
          
   'This For loop loops through each file in the root folder
   For Each myFile In myFiles

       'Assign the name of the current file to myFileName
       myFileName = myFile.Name

       If InStr(1, myFileName, " ") Then
           
           'Rename the file to tempname so we can recalculate hyperlinks
           Call myFile.Move("tempname", True, True)
                
           'Now rename the name in memory
           myFileName = Replace(myFileName, " ", "_")
                
           '...and rename to the original name with spaces replaced 
           'with underscores
           Call myFile.Move(myFileName, True, True)
                
            
       End If

   Next myFile
       
   '////////////////////////////////////////////// 
   '//  Loop through the subfolders
   '////////////////////////////////////////////// 
               
   'Set up a variable for the folder
   Dim myLoopFolder As WebFolder
       
   'This For loop loops through each folder that is a subfolder
   'off of the root folder.
   For Each myLoopFolder In myFolders

       'Check to make sure that the folder is not a subweb.  If it's not,
       'go ahead and remove the spaces.
       'We use MakeRel so that the folder retains its current
       'relative URL.
       If myLoopFolder.IsWeb = False Then
                
           Call myLoopFolder.Move(Replace(MakeRel _
             (ActiveWeb, myLoopFolder.Url), " ", "_"), True, True)
                   
       End If

   Next myLoopFolder
       
   'Since folder names may have changed, we need to refresh the
   'web so we see everything
   ActiveWeb.Refresh
       
   'Now we have to recursively call the RemoveSpace procedure so
   'that we can loop through all folders in the web.
   Dim mySubFolder As WebFolder
       
   For Each mySubFolder In myWebFolder.Folders
       
       'Only call RemoveSpace if the current subfolder is not a subweb
       If mySubFolder.IsWeb = False Then RemoveSpace mySubFolder
       
   Next mySubFolder
       
End Sub 

Additional query words: filename foldername VBA vb vbe

Keywords : kbdtacode
Version : WINDOWS:
Platform : WINDOWS
Issue type :


Last Reviewed: December 8, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.