Replace invalid characters in filename

Posted on 2006-05-24
Last Modified: 2008-02-01
Hello All,

Can somebody please provide me some code which would enable me to replace all invalid characters in a filename which I would like to save. For example the string given to the function may be

"Information about butter/hooks" but the output should be:

"Information about butter_hooks"

But obviously this could happen with any of the following symbols:

\ / : * ? < > |

Can somebody help me? I am using VB6 / VBA. Thanks

Question by:butterhook
    LVL 76

    Accepted Solution

    Not sure what difficulty you are having, but here is some code:

    Const InvalidChars = "\ / : * ? < > |"
    dim i as integer
    for i = 1 to len(InvalidChars)
        strFileName = Replace(strFileName, Mid$(InvalidChars, i, 1), "_")
    Next i
    LVL 29

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now