Creating a Formatted Numeric Prefix to Rename Files

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:
T00#-
or
T0#-

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#-
T001-Filename.txt
T002-Filename.txt
....
T020-Filename.txt

WITH T0#-
T01-Filename.txt
T02-Filename.txt
....
T20-Filename.txt

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.
JOSHSKORNAsked:
Who is Participating?
 
tonybranConnect With a Mentor Commented:
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)
        MsgBox(output)
----------------------------------------------
Note that "number" has to come from somewhere.

Regards,
Tony
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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?
0
 
JOSHSKORNAuthor Commented:
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.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
JOSHSKORNAuthor Commented:
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.
0
 
tonybranCommented:
Apologies, you also need:
Imports System.Text.RegularExpressions

Regards,
Tony
0
 
JOSHSKORNAuthor Commented:
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?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.