?
Solved

Rename a file

Posted on 2005-03-07
4
Medium Priority
?
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

752 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