重命名目录和部分文件重命名

huangapple go评论76阅读模式
英文:

Rename the directory and partial file renaming

问题

你可以使用Go语言的os包来进行文件和目录的重命名操作。以下是你提供的代码的翻译:

package main

import "os"

func main() {
  os.Rename("LICENSE", "e")
}

这段代码将文件名LICENSE更改为e

对于你想要做的两个需求,你可以参考以下方法:

  1. 更改目录名:你可以使用os.Rename函数来更改目录名,与更改文件名的方式相同。

  2. 仅更改名称的一部分:如果你想要仅更改名称的一部分,你可以使用字符串操作来实现。例如,如果你有一个文件或目录名为Example,你想要将Exa更改为Stq,那么你可以使用字符串替换函数来实现这个目标。以下是一个示例代码:

package main

import (
	"fmt"
	"strings"
)

func main() {
	name := "Example"
	newName := strings.Replace(name, "Exa", "Stq", 1)
	fmt.Println(newName)
}

这段代码将输出Stqmple,即将Exa替换为Stq后的结果。

希望对你有帮助!

英文:

Go has this package and function

package main

import "os"

func main() {
  os.Rename("LICENSE", "e")
}

This changes the filename LICENSE to e.

But what I want to do is the following and can't find how to do it. Ruby does this so should Go. Where should I look up?

  1. Change the name of directory not files....

  2. Rename just the part of the name. For example, if I have a file or directory "Example", I would want to change "Exa" to "Stq," then the word would be "Stqmple"

What package should I use?

Thanks in advance.

答案1

得分: 12

  1. os.Rename 可以在包括 Windows 在内的系统上正常工作,也可以用于重命名目录。唯一需要考虑的是该操作是否是原子操作,在所有主要平台上的答案都是肯定的。

  2. 如果你想要查找与某个模式匹配的文件和目录,你可以使用 filepath 包。这个包可以让你遍历一个目录,然后你可以检查每个文件并将其名称与你的要求进行匹配。请查看文档中的 func Walktype WalkFn。http://golang.org/pkg/path/filepath/#Walk

下面是一个使用 Walk 在给定目录或其子目录中查找 png 文件的示例:

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)
}

下面是一个 func visit 的示例,它可以实现你在评论中提到的功能,请确保导入了 "strings":

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
}
英文:
  1. os.Rename 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.

  2. 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 func Walk and type WalkFn of the documentation. http://golang.org/pkg/path/filepath/#Walk

Here's an example of using Walk to locate png files in a given directory or any of its subdirectories:

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)
}

Here's a func visit that does what your asking about in the comment, be sure to import "strings":

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
}

答案2

得分: 0

首先,我认为os.Rename也可以重命名目录(至少在Unix下,目录也是文件)。

其次,也许你可以使用strings.Replace来处理这样的情况,或者在这里进行一些编程操作?

英文:

First I think that os.Rename also renames directories (which - at least under Unix - are files too).

Second: Maybe you could use strings.Replace for such things or do some programming here?

huangapple
  • 本文由 发表于 2013年11月21日 15:56:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/20115327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定