1

Question is quite self-explanatory.

Please, could anybody show me how can I check existence of the file by name (without extension) by short and efficient way. It would be great if code returns several occurrence if folder have several files with the same name.

Example:

folder/
  file.html
  file.md

UPDATE:

It is not obviously how to use one of filepath.Match() or filepath.Glob() functions by official documentation. So here is some examples:

matches, _          := filepath.Glob("./folder/file*") //returns paths to real files [folder/file.html, folder/file.md]
matchesToPattern, _ := filepath.Match("./folder/file*", "./folder/file.html") //returns true, but it is just compare strings and doesn't check real content
Timur Fayzrakhmanov
  • 17,967
  • 20
  • 64
  • 95

2 Answers2

3

You need to use the path/filepath package.

The functions to check are: Glob(), Match() and Walk() — pick whatever suits your taste better.

kostix
  • 51,517
  • 14
  • 93
  • 176
  • 3
    No, you're a programmer and are hence supposed to program. This implies reading the docs and trying things out. Please read [this](http://whathaveyoutried.com) – kostix Nov 23 '14 at 21:55
  • After trying Glob() and Match() I realized - it is what I was looking for! Thank you! – Timur Fayzrakhmanov Nov 24 '14 at 08:22
1

Here is the updated code :

package main

import (
    "fmt"
    "os"
    "path/filepath"
    "regexp"
)

func main() {
    dirname := "." + string(filepath.Separator)
    d, err := os.Open(dirname)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    defer d.Close()
    fi, err := d.Readdir(-1)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    r, _ := regexp.Compile("f([a-z]+)le") // the string to match
    for _, fi := range fi {
        if fi.Mode().IsRegular() { // is file
            if r.Match([]byte(fi.Name())) { // if it match
                fmt.Println(fi.Name(), fi.Size(), "bytes")
            }
        }
    }
}

With this one you can also search for date, size, include subfolders or file properties.

Dippo
  • 184
  • 2
  • 11
  • It is not that I actually find - I thought somebody can offer something that differentiate from using just file Walker - but still...thanks) – Timur Fayzrakhmanov Nov 24 '14 at 07:19
  • 1
    @Timur Fayzrakhmanov If you look at this link [link] (http://stackoverflow.com/questions/14668850/list-directory-in-go) you can make my code a little bit shorter. Also, Readdir sorts the outcome, while Glob doesn't. – Dippo Nov 24 '14 at 16:34
  • Thanks for link. I find your code a little bit "low-level", so I'll revisit your code when the time comes. Regarding the link I've already use ReadDir function to take the list of folder's files. I think that Glob should be used as intended instead of listing folder's content, don't you think so? – Timur Fayzrakhmanov Nov 24 '14 at 19:57
  • 1
    @Timur Fayzrakhmanov Glob only returns the filenames and nothing more, nevertheless, with Glob you can tell from the start what files needs to be in the list. While Readdir doesn't give this option, but it gives more control to what have been shown. But the differences are minor between Glob and Readdir. Although, i think that Glob is faster. – Dippo Nov 24 '14 at 20:15
  • hm..it's food for thought. It feels like Glob should be used if we just need to see a content of the folder and not to do anything else. Presumably, Glob may be faster and take less memory. – Timur Fayzrakhmanov Nov 24 '14 at 20:40