?
Solved

Creating a Formatted Numeric Prefix to Rename Files

Posted on 2006-05-10
6
Medium Priority
?
396 Views
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:
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.
0
Comment
Question by:JOSHSKORN
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16654129
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
 

Author Comment

by:JOSHSKORN
ID: 16655629
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
 
LVL 3

Accepted Solution

by:
tonybran earned 2000 total points
ID: 16656580
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:JOSHSKORN
ID: 16656626
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
 
LVL 3

Expert Comment

by:tonybran
ID: 16656699
Apologies, you also need:
Imports System.Text.RegularExpressions

Regards,
Tony
0
 

Author Comment

by:JOSHSKORN
ID: 16656808
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

840 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