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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.RegularExpress ions.Regex ("\d\d\d\d \d\dasl.tx t")
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
Dim di As New IO.DirectoryInfo("D:\My Documents\Test\")
Dim fi As IO.FileInfo
Dim patternTest As New System.Text.RegularExpress
For Each fi In di.GetFiles()
If patternTest.Match(fi.Name)
Dim newFilename As String = fi.DirectoryName & "\asl.txt"
If IO.File.Exists(newFilename
'that name exists.. do what you like here... either overwrite or alert the user
Else
fi.MoveTo(newFilename)
End If
End If
Next
ASKER
I haven't tried the second one yet, but the first one worked great. Thanks for all your help.