Link to home
Start Free TrialLog in
Avatar of maralans
maralans

asked on

Rename a file

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
ASKER CERTIFIED SOLUTION
Avatar of S-Twilley
S-Twilley

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of S-Twilley
S-Twilley

I meant to say "I tested"... let me know if thats ok
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
Avatar of maralans

ASKER

I haven't tried the second one yet, but the first one worked great.  Thanks for all your help.