-2

Currently the line of code looks like this:

Dim files() As String = System.IO.Directory.GetFiles(path, filehead & ".*.*.fsi")
Dim seqfsi() As Integer
ReDim seqfsi(files.GetUpperBound(0))
Dim args() As String
Dim file As String = ""
For Each file In files
    args = Split(file, ".")
    If args.Length = 4 Then
        seqfsi(System.Array.IndexOf(files, file)) = CInt(args(args.GetUpperBound(0) - 1))
    End If

The problem is, sometimes, in my case, the path looks something like:

C:\Users\c.brummett\Downloads

and the split causes a split in the username. How can I avoid this problem but still split by periods? I'm sorry I don't how to make this more relatable.

My idea was to use a DirectoryInfo and do something like:

Dim di As DirectoryInfo
di = New DirectoryInfo(path)
Dim files() As String = di.GetFiles(filehead & ".*.*.fsi")

Edit: The problem with this second bit of code, is that it returns the error

Value of type '1-dimensional array of System.IO.FileInfo' cannot be converted to '1-> dimensional array of String' because 'System.IO.FileInfo' is not derived from 'String'.`

Humayun Shabbir
  • 2,961
  • 4
  • 20
  • 33
Commmett
  • 39
  • 1
  • 1
  • 8
  • Could you explain what are you trying to achieve with that code? – Steve Jul 30 '14 at 20:32
  • there are better ways to parse files (if thats what you are after). `System.Io.FileInfo` and `System.Io.Path` will get the file name, ext, path etc for a file – Ňɏssa Pøngjǣrdenlarp Jul 30 '14 at 20:33
  • @Steve I'm trying to increment log files extension. Their names look like `Spider.01.000.fsi` Once I can get this snippet to exclude the period in the directory tree, it should split it up and in the end make another file called `Spider.01.001.fsi` and then `Spider.01.002.fsi` and so on. – Commmett Jul 30 '14 at 20:34
  • @Plutonix I have where they are located. I just need to exclude the directory from `files()`. – Commmett Jul 30 '14 at 20:36
  • 1
    `System.Io.FileInfo` would give you the name w/o path when you are collecting them without having to split;`System.Io.Path` can be used to parse a filename you already have...so, you could use them to get the filename then split to do the incrementing part – Ňɏssa Pøngjǣrdenlarp Jul 30 '14 at 20:40
  • Split a substring of the file name starting at the LastIndexOf("\") – rheitzman Jul 30 '14 at 20:43
  • 1
    `DirectoryInfo` returns an array of `FileInfo` object which could be iterated to get just the filename to chop up and increment the parts: `Public Function GetFiles(searchPattern As String) As System.IO.FileInfo( )` – Ňɏssa Pøngjǣrdenlarp Jul 30 '14 at 20:52
  • @Plutonix Sorry, I am new to .VB and honestly, new to programming in general. I'm trying to implement `System.IO.FileInfo` and/or `System.IO.Path` and am struggling. – Commmett Jul 30 '14 at 21:02
  • `Dim files() As FileInfo` each element will be a FileInfo object, one property of which will be FileName and another Extension etc – Ňɏssa Pøngjǣrdenlarp Jul 30 '14 at 21:07

2 Answers2

2

You can forget about getting an array of file names (you don't need that anyway) and iterate on the array of FileInfo:

Dim files() As FileInfo =  New DirectoryInfo(path).GetFiles(filehead & ".*.*.fsi")
Dim seqfsi() As Integer
ReDim seqfsi(files.GetUpperBound(0))
Dim args() As String

For Each file As FileInfo In files
    args = Split(file.Name, ".")
    If args.Length = 4 Then
        seqfsi(System.Array.IndexOf(files, file)) = CInt(args(args.GetUpperBound(0) - 1))
    End If
Alireza
  • 4,976
  • 1
  • 23
  • 36
  • +1 the string array can also be disposed of if you increment and rename in the loop instead of saving for later. – Ňɏssa Pøngjǣrdenlarp Jul 30 '14 at 21:08
  • 1
    @Plutonix exactly. This code can be improved in some ways, but I tried to remain faithful to the original code – Alireza Jul 30 '14 at 21:11
  • 1
    you buy them books, pack them a lunch, send them to school...and they eat the teacher – Ňɏssa Pøngjǣrdenlarp Jul 30 '14 at 21:18
  • I tried this code, and it resulted in almost the same error I had earlier. The error I receive is `Value of type '1-dimensional array of String' cannot be converted to '1-dimensional array of System.IO.FileInfo' because 'String' is not derived from 'System.IO.FileInfo' ` – Commmett Jul 30 '14 at 21:21
  • @Commmett `Dim files() As STRING <----- ` is **not** the same as `Dim files() As FileInfo <---` – Ňɏssa Pøngjǣrdenlarp Jul 30 '14 at 21:29
  • @Commmett My mistake. I'm REALLY sorry. Should have used `DirectoryInfo.GetFiles`. Updated the answer. – Alireza Jul 30 '14 at 21:29
  • 1
    And it does not involve playing with strings which is always a source for bugs – Alireza Jul 30 '14 at 21:31
  • @Plutonix MSDN is silent about that matter. It just says: `searchPattern can be a combination of literal and wildcard characters, but doesn't support regular expressions.` Maybe we should look in the Win32 API documentation – Alireza Jul 30 '14 at 21:44
1

Note AllDirectories and change in the line doing the splitting. I didn't look at your array structure stuff.

    Dim files() As String = System.IO.Directory.GetFiles("C:\temp", "*.doc", IO.SearchOption.AllDirectories)
    Dim seqfsi() As Integer
    ReDim seqfsi(files.GetUpperBound(0))
    Dim args() As String
    Dim file As String = ""
    For Each file In files
        args = file.Substring(file.LastIndexOf("\") + 1).Split(".")
        If args.Length = 4 Then
            seqfsi(System.Array.IndexOf(files, file)) = CInt(args(args.GetUpperBound(0) - 1))
        End If
    Next file
rheitzman
  • 2,247
  • 3
  • 20
  • 36