Creating a Formatted Numeric Prefix to Rename Files

Posted on 2006-05-10
Last Modified: 2008-01-09
I've written a program to copy files from one drive to the next.  It works great so far, but I'm having trouble creating the file prefix I want to make.  My question is on how to do this.  Please read through the entire post.

I have a textbox in which the user will specify what the prefix can be.  The user can type in something like:

Basically, I want to be able to check the string to make sure it contains exactly one valid format (i.e. #, 0#, 00#, etc. but not a 0 by itself) and use that to format the number using the function: Format(expression,format).  The positioning of the format does not matter, as long as it's part of the string (the string value is specified from the textbox explained from the previous paragraph).  I also want to attach a before and after string to this number (Note in this example, the letter "T" before the zero(s) and the dash ('-') after the pound sign).  Before and after strings can be null (i.e. if I wanted to make the prefix a number only).

Here is sample output with the filename originally being Filename.txt.

WITH T00#-


Overall, my question is, how do I take the textbox value, extract the format, validate the format and then reattach all of it together?  What is the easiest way to do this?  Please provide some code.  Thanks.

Random Thought:  Do I need a beginning, middle and end string? An array?

Note that I'm using the File.Copy method to copy from source to destination.
Question by:JOSHSKORN
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Will it ALWAYS be ONE letter, followed by two or three paceholders, followed by a dash?

    You need to be very specific as to what is allowed in the textbox if you want help in writing code that will determine if it is a "valid" entry...

    What is the complete range of "formats" the user can enter?

    Author Comment

    For a format to be considered valid, it must have at least one pound sign.  If there are any zeros in the format, they MUST occur BEFORE the pound sign.

    There is no guarantee that there is always only one letter before and after the format and also, there is no guarantee that only a dash will follow the numeric format.  Therefore, you cannot conclude there being only one character before and after the desired numeric format entered.

    Another thing to take into consideration, is to make sure all characters entered in the prefix string are acceptable characters for filenames.  These characters include:  / \ : " ? * <> |

    I also want to disallow usage of the period, even though after some testing with a text file, you ARE allowed to put in periods.
    LVL 3

    Accepted Solution

    You can use something along the following lines in the validation routine for the textbox.
            Dim re As New Regex("^[^0\\/:""?*<>|.]*0*#[^0\\/:""?*<>|.]*$")
            Dim m As Match
            Dim input As String = textbox.Text
            Dim output as String

            m = re.Match(input)
            If Not m.Success Then
                MsgBox("Failed to match")
                Exit Sub
            End If

            re = New Regex("0*#")
            m = re.Match(input)
            output = input.Substring(0, m.Index) & Format(number, input.Substring(m.Index, m.Length)) & input.Substring(m.Index + m.Length)
    Note that "number" has to come from somewhere.


    Author Comment

    Tony, this code has errors.  Note that I'm using VB .NET 2003.  As much as I'd love to close this question and award you points, I cannot do that yet.

    The following two lines have errors (there are others but I see how to fix them because they deal with variables I would be using in my program anyway):

    Dim re As New Regex("^[^0\\/:""?*<>|.]*0*#[^0\\/:""?*<>|.]*$")  ' --- Type 'Regex' not defined.
    Dim m As Match ' --- Type 'Match' not defined.
    LVL 3

    Expert Comment

    Apologies, you also need:
    Imports System.Text.RegularExpressions


    Author Comment

    Thanks Tony.  I've awarded you your points.

    Note to Moderators - tonybran's first answer was almost correct but he accidentally left out a line.  Can his second reply be noted as an Assisted Answer?

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    745 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

    16 Experts available now in Live!

    Get 1:1 Help Now