asc2010
asked on
Find A File With A Dynamic Name In VB.NET
I have a text file which part of the name will change on a daily basis.
I already have a brilliant piece of code written by Idle_Mind that will parse the file once I find it. I need to know how to find the a file based on part of the file name then rename it after it is processed.
dlylicex-03112011-1401.txt
The file is located in the same directory as the application.I already have a brilliant piece of code written by Idle_Mind that will parse the file once I find it. I need to know how to find the a file based on part of the file name then rename it after it is processed.
Sub ParseMe()
' pass data to array, rearrange
Dim Path As String = strCurrentDir
Dim DataFile As String = strFileName
Dim V_File As String = strFileNameNew
Dim strLicensePlate As String
Dim intLicensePlateLength As Integer
Dim intFixedWidthLength As Integer = 11
Dim intPlateAndWhiteSpaceLength As Integer
Using sw_V As New StreamWriter(System.IO.Path.Combine(Path, V_File), False)
Using sr As New StreamReader(System.IO.Path.Combine(Path, DataFile))
While Not sr.EndOfStream
Dim values As New List(Of String)
values.AddRange(sr.ReadLine.Split(";")) 'find semicolon and make column
If values(0).ToUpper = "ST" Or values(0).ToUpper = "SV" Then
'if first column is "ST" or "SV", then
' move column 10 to index 0 and add fixed-width white space of 11 characters.
' replace all semicolons with a single white space
strLicensePlate = values(10) ' set plate characters into string var
intLicensePlateLength = Len(strLicensePlate) ' find length of string (number of characters)
intPlateAndWhiteSpaceLength = intFixedWidthLength - intLicensePlateLength ' find fixed-width length
'MessageBox.Show(intRemainLength)
values.Insert(0, values(10) & Space(intPlateAndWhiteSpaceLength)) ' rearrange columns with fixed-width white space
values.RemoveAt(11) ' remove unwanted column
Dim output As String = String.Join(" ", values.ToArray) ' add to array
sw_V.WriteLine(output) ' write to file
End If
End While
End Using
End Using
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi there Idle_Mind!! Yes, the "dlycilex" is constant. Sorry about that!
mas_oz2003 pretty much gave you a working solution then. Do you need help converting that to VB.Net?
ASKER
Idle_Mind, your help is always appreciated. Should I ask for your help converting this to VB.NET in a separate question?
ASKER
I also need to be able to differentiate between older files and the most recent file based on the file name.
We can help you here no problem.
So there will be multiple files in the folder and you need just the newest one based on the date contained within the NAME itself?
So there will be multiple files in the folder and you need just the newest one based on the date contained within the NAME itself?
ASKER
There should only be a single file with this file name in the directory. However, in the event that the program does not run and multiple files exist, I need to use only the most current.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Idle_Mind,
Thanks again for your help!! You are a genius!!
Thanks again for your help!! You are a genius!!
Is the "dlylicex" part of the file constant? You have to let us know which parts stay the same and which parts change so we can help you find the dynamic filename! =)