Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li><p><a href="https://golang.org/pkg/os/#Rename" rel="nofollow noreferrer"><code>os.Rename</code></a> will work fine on directories, including windows. The only bit to question here is whether the operation is atomic, which on all major platforms the answer is yes.</p></li> <li><p>If you want to locate files and directories that match a pattern, you'll want to look at the filepath package. This will let you traverse a directory at which point you can inspect each file and match the name against your requirements. Look at <code>func Walk</code> and <code>type WalkFn</code> of the documentation. <a href="http://golang.org/pkg/path/filepath/#Walk" rel="nofollow noreferrer">http://golang.org/pkg/path/filepath/#Walk</a></p></li> </ol> <p>Here's an example of using <code>Walk</code> to locate png files in a given directory or any of its subdirectories:</p> <pre><code>package main import ( "flag" "fmt" "os" "path/filepath" ) var flagPath = flag.String("path", "", "recursively traverse path and print png filepaths to stdout.") func visit(path string, f os.FileInfo, err error) error { if filepath.Ext(path) == ".png" { fmt.Println(path) } return nil } func main() { flag.Parse() if *flagPath == "" { flag.Usage() return } filepath.Walk(*flagPath, visit) } </code></pre> <p>Here's a <code>func visit</code> that does what your asking about in the comment, be sure to import "strings":</p> <pre><code>func visit(path string, f os.FileInfo, err error) error { if name := f.Name(); strings.HasPrefix(name, "name_") { dir := filepath.Dir(path) newname := strings.Replace(name, "name_", "name1_", 1) newpath := filepath.Join(dir, newname) fmt.Printf("mv %q %q\n", path, newpath) os.Rename(path, newpath) } return nil } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload