Pages

Wednesday, December 23, 2009

Replacing restricted strings in file paths

There has been a lot of occasions on which I have to provide users an option to specify a file path to export certain criteria from my application.

Though they would work fine, the export would fail abruptly if user enters a few strings like *, ? etc in the file path and that will lead to a lot of nuisance and noise X-)

The following fragment of code shall take care of those issues if implemented

'**********************************************************************************'@Author : Karthikeyan A
'@Purpose : replace the strings /\|?><*:" from any strings concerned with naming OS files especially '@Type : Function '@Name : replaceRestrictedStrings '@Param : text As String, customValue As String '@Return : String '**********************************************************************************

Function replaceRestrictedStrings(text As String, customValue As String) As String
Dim restrictedStrings(0To 8) As String
restrictedStrings(0)={\}
restrictedStrings(1)={/}
restrictedStrings(2)={:}
restrictedStrings(3)={*}
restrictedStrings(4)={?}
restrictedStrings(5)={"}
restrictedStrings(6)={<} restrictedStrings(7)={>}
restrictedStrings(8)={|}

'find the occurences of the above set of strings and replace them by customValue provided by the user
Forall vall In restrictedStrings
While Instr(text,vall)
text=Replace(text,vall,customValue)
Wend
End Forall

'calculate the return value of the function
replaceRestrictedStrings=text
End Function

No comments:

Post a Comment