Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Rename a file

Posted on 2005-03-07
4
Medium Priority
?
250 Views
Last Modified: 2010-04-23
Hi,

I get a text file daily which is comprised of the date so the name of the file changes daily.  For example, 030705asl.txt and 030805asl.txt etc...  How can I rename this daily file to just asl.txt?   I have tried the file.copy but apparently wildcard characters do not work.

Mark
0
Comment
Question by:maralans
  • 3
4 Comments
 
LVL 12

Accepted Solution

by:
S-Twilley earned 2000 total points
ID: 13483126
Hi.. i test this code and it works...

        Dim di As New IO.DirectoryInfo("D:\My Documents\Test\")
        Dim fi As IO.FileInfo

        For Each fi In di.GetFiles("*asl.txt")
            MsgBox(fi.Name)
        Next

=============

so you can change it to something like

        Dim di As New IO.DirectoryInfo("D:\My Documents\Test\")
        Dim fi As IO.FileInfo

        For Each fi In di.GetFiles("*asl.txt")
            Dim newFilename As String = fi.DirectoryName & "\asl.txt"

            If IO.File.Exists(newFilename) Then
                'that name exists
            Else
                fi.MoveTo(newFilename)
            End If
        Next
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13483130
I meant to say "I tested"... let me know if thats ok
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13483163
An alternative is this... this strictly tests for files with the pattern ######asl.txt where # is a number (I think my previous example would have picked up "asl.txt" as well, this one is stricter... you could even be more strict by making sure its a valid date! )

        Dim di As New IO.DirectoryInfo("D:\My Documents\Test\")
        Dim fi As IO.FileInfo
        Dim patternTest As New System.Text.RegularExpressions.Regex("\d\d\d\d\d\dasl.txt")
        For Each fi In di.GetFiles()
            If patternTest.Match(fi.Name).Success Then
                Dim newFilename As String = fi.DirectoryName & "\asl.txt"

                If IO.File.Exists(newFilename) Then
                    'that name exists.. do what you like here... either overwrite or alert the user
                Else
                    fi.MoveTo(newFilename)
                End If
            End If
        Next
0
 
LVL 5

Author Comment

by:maralans
ID: 13483338
I haven't tried the second one yet, but the first one worked great.  Thanks for all your help.
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

580 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