Help with building string variable from file name in application's folder using VB.NET

Hi,

I'm using the code below to search if file exist in my application's folder, How do I modify it to create a list (BEL,CAN,GBR,USA) if  if LinkFinalBEL, LinkFinalCAN, LinkFinalGBR and LinkFinalUSA is found?


Dim DestinationPath As String = IO.Path.Combine(Application.StartupPath + "\LinkFiles\", IO.Path.GetFileName("\LinkFiles\LinkFinalBEL.xml"))
        If IO.File.Exists(DestinationPath) Then

        Code

       Endif

Thanks,

Victor
vcharlesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
Hi,

Check below code-

Dim SourcePath as string = "c:\SomeFolder\SomeFileYouWantToCopy.txt" 'This is just an example string and could be anything, it maps to fileToCopy in your code.
Dim SaveDirectory as string = "c:\DestinationFolder"

Dim Filename as string = system.io.path.getFileName(SourcePath) 'get the filename of the original file without the directory on it
Dim SavePath as string = system.io.path.combine(SaveDirectory, Filename) 'combines the saveDirectory and the filename to get a fully qualified path.

if system.io.file.exists(SavePath) then
   'The file exists
else
    'the file doesn't exist
end if

Open in new window

0
Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
You need to check if particular file exists then modify it. Simple. The above code will give you an idea to check if the file exists or not.
0
vcharlesAuthor Commented:
Thanks for the code, how do I build the string variable BEL,CAN,GBR,USA  if those files exist?
0
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
Can you tell what kind of list you need to create? Is there any specific format?
0
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
Hi,

You want to create a list of items if files found. So code as:

' Declare ArrayList object
Dim list As New ArrayList

' Get destination path for each file separately 
Dim BELPath As String = IO.Path.Combine(Application.StartupPath + "\LinkFiles\", IO.Path.GetFileName("\LinkFiles\LinkFinalBEL.xml"))

        If IO.File.Exists(BELPath) Then

        list.Add("BEL");

       Endif

' Dim CANPath here and set path

If IO.File.Exists(CANPath) Then

        list.Add("CAN");

       Endif

' and so on

Open in new window

0
KimputerCommented:
Do you mean this?

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim countries As String() = {"BEL", "CAN", "GBR", "USA"}
        Dim log As String

        For Each country In countries
            Dim DestinationPath As String = IO.Path.Combine(Application.StartupPath + "\LinkFiles\", IO.Path.GetFileName("\LinkFiles\LinkFinal" & country & ".xml"))
            If linkfinalexists(DestinationPath) Then
                log = DestinationPath + " exists." + vbCrLf + log
            Else
                log = DestinationPath + " does not exists." + vbCrLf + log
            End If
        Next
        MsgBox(log)
    End Sub


    Private Function linkfinalexists(ByVal dest_path) As Boolean

        If IO.File.Exists(dest_path) Then
            Return True
        Else
            Return False
        End If

    End Function

Open in new window

0
Fernando SotoRetiredCommented:
Hi vcharles;

See if this sample code meets your needs.

' Create path to file location
Dim DestinationPath As String = IO.Path.Combine(Application.StartupPath, "LinkFiles\")
' Get a reference to the directory
Dim dirInfo = New System.IO.DirectoryInfo(DestinationPath)
' Get all the files that match the pattern LinkFinal*.xml
Dim fileInfos = dirInfo.GetFiles("LinkFinal*.xml")
' Build the string using Linq to Object query
Dim strCodes = fileInfos.Select(Function(f) f.Name) _
    .Aggregate("", Function(current, nextVal) current & ", " & nextVal.Substring(nextVal.IndexOf("."c) - 3, 3)) _
    .Substring(2)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Hi,

Thank you for your replies, will get back to you later.

Victor
0
vcharlesAuthor Commented:
Hi,

I am trying the code below but country abbreviation shows except for LinkFinalBEL.ny ideas why BEL is not included in the list?

Thank You for all the comments.

  ' Create path to file location
        Dim DestinationPath As String = IO.Path.Combine(Application.StartupPath, "LinkFiles\")
        ' Get a reference to the directory
        Dim dirInfo = New System.IO.DirectoryInfo(DestinationPath)
        ' Get all the files that match the pattern LinkFinal*.xml
        Dim fileInfos = dirInfo.GetFiles("LinkFinal*.xml")
        ' Build the string using Linq to Object query
        Dim strCodes = fileInfos.Select(Function(f) f.Name) _
            .Aggregate("", Function(current, nextVal) current & ", " & nextVal.Substring(nextVal.IndexOf("."c) - 3, 3)) _
            .Substring(2)
        MsgBox(strCodes)

Victor
0
vcharlesAuthor Commented:
Hi again,

How do I sort the string. For example if my string is

USA,BEL,CAN

How do I change it to

BEL,CAN,USA
0
Fernando SotoRetiredCommented:
This will do it, using Linq to Object.

str = String.Join( ",", str.Split(New Char() {","c}) _
            .OrderBy(Function(s) s).Select(Function(s) s))
   

Open in new window

0
vcharlesAuthor Commented:
It is sorting correctly. Thanks.

Any ideas why BEL does not show in the list for LinkFinalBEL?
0
Fernando SotoRetiredCommented:
Whose code are you using?
0
vcharlesAuthor Commented:
I only tried the code I  last posted,
0
KimputerCommented:
:

Haha, you should've used my code, and then you only need to adjust : dim countries
0
Fernando SotoRetiredCommented:
You state that this file is not being picked up, LinkFinalBEL, is it missing the file extension? Or may it has extra characters? Can you post a screen shot of the directory and its files.
0
vcharlesAuthor Commented:
Hi,

Thanks for your code, would like to use Linq because I don't have to hard code the countries.

Something must be wrong with the file looking further into it.

Thank You All.

Victor
0
Fernando SotoRetiredCommented:
Hi Victor;

OK, if you find all is well and the question has been answered please close the question.

Thanks;
Fernando
0
vcharlesAuthor Commented:
Thank You!
0
Fernando SotoRetiredCommented:
Not a problem Victor, glad I was able to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.