Passing a file name(s) to a function


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Passing a file name(s) to a function

  1. #1
    Join Date
    Apr 2007
    Posts
    1

    Passing a file name(s) to a function

    Below is a function from Tim Hastings (http://www.nonhostile.com/). This is a post from his blog. At the bottom of his code you can see he demonstrates his function with some html fed into the function.

    I would like to know how you could use a function similar to this but instead of some html passed into the function...be able to pass the function a file or a group of files. How would you pass this function a file name or group of file names?

    I'm thinking something like this...
    Code:
    Dim HTMLFile As String
    HTMLFile = Dir("C:\TEMP\" & "*.html")
    HTMLClean (HTMLFile)
    Of course, this doesn't work. I think it has to do with the byVal reference in the function but I'm not sure and don't know what it should be. Any help is greatly appreciated. Thanks.
    Code:
    Private Function HTMLClean(ByVal strText As String) As String
    
        Dim objRegEx As VBScript_RegExp_55.RegExp
        
        ' replace <br>'s for a newline
        strText = Replace$(strText, "<br>", Chr$(10), 1, -1, vbTextCompare)
        
        ' replace non-breaking spaces for a space
        strText = Replace$(strText, "&nbsp;", Chr$(32), 1, -1, vbTextCompare)
        
        ' create new regex object
        Set objRegEx = New VBScript_RegExp_55.RegExp
        objRegEx.Global = True ' don't just operate on first find.
        
        ' remove HTML tags
        objRegEx.Pattern = "<[^>]*>"
        strText = objRegEx.Replace(strText, "")
        
        ' ditch excessive white space
        objRegEx.Pattern = "\s+"
        strText = objRegEx.Replace(strText, " ")
    
     
    
        ' thanks, bye
        Set objRegEx = Nothing
    
     
    
        ' named-entities
        strText = Replace$(strText, "&gt;", ">", 1, -1, vbTextCompare)
        strText = Replace$(strText, "&lt;", "<", 1, -1, vbTextCompare)
    
     
    
        ' insert your favourite named-entities here.
        strText = Replace$(strText, "&amp;", "&", 1, -1, vbTextCompare)  ' must do last
        
        ' return
        HTMLClean = strText
        
    End Function
    
    
    Debug.Print HTMLClean("<greets>Hello &lt;oh&gt;&nbsp;&amp;" & vbNewLine & _
    
                          "<b>I</b> <a href=""#"">wonder</a>  " & vbNewLine & _
    
                          "   what<br>will become of all this?</greets>")
    
    
    Hello <oh> &I wonder what will become of all this?

  2. #2
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    You have to open each file and read the contents into a string. Then you can send the string to the HTMLClean function which returns a cleaned string. You can then display the cleaned data or write it back to the file. If you want a routine that handles an entire file, write a new function that uses the HTMLClean function. Like HTMLCleanFile which open the file, reads the data sends it to HTMLClean and then writes the data back to the file, and then closes the file. So one routine handles the file and the other cleans the text.

  3. #3
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    The Dir function only returns a single file name, so there's no way to use it the way you suggest. You could call the Dir function in a loop to fill an array with file names, then pass the array to the function. Or, you could call the function from within the loop, passing a single file name; open the file and read its contents in the function.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

Similar Threads

  1. How to pass string to concatenate routine.
    By haran415 in forum VB Classic
    Replies: 4
    Last Post: 04-15-2007, 11:40 AM
  2. Search System Tray
    By MyPlague in forum .NET
    Replies: 2
    Last Post: 03-20-2006, 06:18 PM
  3. wav file fade in out help
    By jase_dukerider in forum C++
    Replies: 2
    Last Post: 04-14-2005, 08:48 PM
  4. Replies: 0
    Last Post: 06-13-2002, 01:41 AM
  5. Getting a GUI to function
    By Eric in forum Java
    Replies: 1
    Last Post: 11-27-2001, 07:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center