无法在Windows上创建文件夹。

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

Unable to create folder on Windows

问题

我正在尝试在Windows上创建一个文件夹,但一直遇到错误,不明白原因。

代码片段如下:

reader := bufio.NewReader(os.Stdin)
fmt.Print("主机名:")
hostname, _ := reader.ReadString('\n')

now := time.Now().Format("20060102150405")
var folderName string = fmt.Sprintf("%s_%s", now, hostname)

err := os.Mkdir(folderName, os.ModePerm)

if err != nil {
	log.Fatal(err)
}

错误信息(从荷兰语翻译过来):

2022/06/10 13:37:13 mkdir 20220610133713_TEST
:文件名、文件夹名或卷名的语法不正确。
退出状态 1

我尝试使用带有\\的完整路径和相对路径.\或.\\。

我通过下面的代码绕过了错误,但这不是正确的方法... 无法在Windows上创建文件夹。

cmd := exec.Command("cmd.exe", "/C", fmt.Sprintf("mkdir %s", folderName))
err := cmd.Run()

问题可能很明显,但我没有找到原因。也许是Windows特定的问题?

无论如何,感谢你的帮助。

英文:

I'm trying to create a folder on Windows but keep getting an error and do not understand why.

The code snippet:

reader := bufio.NewReader(os.Stdin)
fmt.Print("Hostname: ")
hostname, _ := reader.ReadString('\n')

now := time.Now().Format("20060102150405")
var folderName string = fmt.Sprintf("%s_%s", now, hostname)

err := os.Mkdir(folderName, os.ModePerm)

if err != nil {
	log.Fatal(err)
}

The error (translated from Dutch):

2022/06/10 13:37:13 mkdir 20220610133713_TEST
: The syntax of the file name, folder name or volume name is incorrect.
exit status 1

I tried using the full path with \\ and a relative path .\ or .\\

I got around the error with the code below, but that's not the way to go... 无法在Windows上创建文件夹。

cmd := exec.Command("cmd.exe", "/C", fmt.Sprintf("mkdir %s", folderName))
err := cmd.Run()

The issues is probably obvious but I fail to see the reason. Maybe something Windows specific?

Anyway thanks for having a look.

答案1

得分: 3

Reader.ReadString() 的文档说明如下:

> ReadString 会读取输入直到第一个分隔符(delim)的出现,并返回包含数据和分隔符在内的字符串。

Reader.ReadString('\n') 不会去除换行符,返回的字符串将包含一个尾部的\n字符。

你可以像这样打印它来查看:

fmt.Printf("%q\n", folderName)

这将输出(在Go Playground上尝试):

"20091110230000_TEST\n"

而在 Windows 中,文件夹名称不允许包含换行符。

解决方案是使用bufio.Scanner

例如:

scanner := bufio.NewScanner(strings.NewReader("TEST\n"))
if !scanner.Scan() {
	return // 没有输入
}
hostname := scanner.Text()

now := time.Now().Format("20060102150405")
var folderName string = fmt.Sprintf("%s_%s", now, hostname)

fmt.Printf("%q\n", folderName)

这将输出(在Go Playground上尝试):

"20091110230000_TEST"
英文:

Reader.ReadString() documents that:

> ReadString reads until the first occurrence of delim in the input, returning a string containing the data up to and including the delimiter.

Reader.ReadString('\n') does not trim the newline character, the returned string will contain a trailing \n character.

You can see it if you print it like this:

fmt.Printf("%q\n", folderName)

This will output (try it on the Go Playground):

"20091110230000_TEST\n"

And Windows does not allow the newline character in folder names.

Solution: use bufio.Scanner.

For example:

scanner := bufio.NewScanner(strings.NewReader("TEST\n"))
if !scanner.Scan() {
	return // No input
}
hostname := scanner.Text()

now := time.Now().Format("20060102150405")
var folderName string = fmt.Sprintf("%s_%s", now, hostname)

fmt.Printf("%q\n", folderName)

This will output (try it on the Go Playground):

"20091110230000_TEST"

huangapple
  • 本文由 发表于 2022年6月10日 19:48:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/72573798.html
匿名

发表评论

匿名网友

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

确定