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...
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.
Dim HTMLFile As String
HTMLFile = Dir("C:\TEMP\" & "*.html")
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, " ", 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
strText = Replace$(strText, ">", ">", 1, -1, vbTextCompare)
strText = Replace$(strText, "<", "<", 1, -1, vbTextCompare)
' insert your favourite named-entities here.
strText = Replace$(strText, "&", "&", 1, -1, vbTextCompare) ' must do last
HTMLClean = strText
Debug.Print HTMLClean("<greets>Hello <oh> &" & 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?
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.
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.
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
By haran415 in forum VB Classic
Last Post: 04-15-2007, 11:40 AM
By MyPlague in forum .NET
Last Post: 03-20-2006, 06:18 PM
By jase_dukerider in forum C++
Last Post: 04-14-2005, 08:48 PM
By wind_son in forum .NET
Last Post: 06-13-2002, 01:41 AM
Last Post: 11-27-2001, 07:53 AM
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL