Reading file that have random name


(Bidhan Bastola) #1

How can we read the file that have random name. The below file contains some log and i want to extract only certain parameters from the file.
For ex :- (buffer.b59b721b18b8629039d51942cf37460ed.log)


(Karl Benedict) #2

Hi,
is it the only file in the folder? is it the only file with log extension? is it the only one that starts with buffer?
and most importantly is this question about reading a file with random name or about extracting only certain “parameters” from the file?


(Bidhan Bastola) #3

It is second file that starts with buffer
Other file are buffer.b59b721b18b8629039d51942cf37460ed.log.meta and date.log
I want to read only buffer.b59b721b18b8629039d51942cf37460ed.log
And the file that starts with buffer changes its name in every 5 minutes

Yes this is the question about reading a file and storing it in temp file to view the extracted contents only.


(Karl Benedict) #4

Is it always the second file? Lexicographically?


(Karl Benedict) #5

Also based on the files you listed, the one you are looking for is the only that starts with ‘buffer’ and ends with ‘log’. Is that what you are after, can we assume it is always the case?

If it is:

package main

import (

"fmt"

    "path/filepath"

)

func main() {
    f, err := filepath.Glob("buffer.*log")
    if err != nil {
        fmt.Println(err)
    }

    if len(f) > 0 {
        fmt.Println(f[0])
    }
}

files in the folder:

 ~/Projects/go/golangbridge $ ls -l | egrep "(log|buffer|date)"
-rw-r--r--   1 jabbson  staff     0  6 Jan 08:50 buffer.b59b721b18b8629039d51942cf37460ed.log
-rw-r--r--   1 jabbson  staff     0  6 Jan 08:49 buffer.b59b721b18b8629039d51942cf37460ed.log.meta
-rw-r--r--   1 jabbson  staff     0  6 Jan 08:50 date.log

result:

 ~/Projects/go/golangbridge $ go run .
buffer.b59b721b18b8629039d51942cf37460ed.log

(Bidhan Bastola) #6

Thanks Karl
This worked for me to get file name.
Can you explain how can i get the filtered content from the file and view it by running its executable file ?


(Karl Benedict) #7

I am not entirely sure what you mean, what is filtered content? running what? Please explain.