doujingjiao0015 2019-05-10 10:30 采纳率: 0%
浏览 800

Golang OS.Rename(<fromDir>,<toDir>)在Windows中不起作用

Using Go - lang, according to the documentation, the os.Rename should be able to rename either a file or directory on any operating system.

On Linux it works as it should, pass either a file or directory into it and the file or directory are moved.

On windows i recieve an 'Access is denied' Error when trying to pass a folder. It works 100% for files.

example: source = c:\sourcefolder destination = c:\destinationfolder

source contains: C:\sourcefolder\file1.xml C:\sourcefolder\file2.xml C:\sourcefolder\foldername1 C:\sourcefolder\foldername1\file3.xml C:\sourcefolder\foldername2 C:\sourcefolder\foldername2\file4.xml

both file1.xml and file2.xml will successfully copy to c:\destination. But the folders and files within the folders crash out with access denied

The script is pretty simple:

source := "C:\\sourcefolder"
destination := "C:\\destinationfolder"
pathSeperator := "\\"

files, err := ioutil.ReadDir(source)
    if err != nil {
    fmt.Println("Move command execution error: ", err)
    }

for _, f := range files {
    fmt.Println(f.Name())
    fmt.Println(f.Mode())
    err := os.Rename(source+pathSeperator+f.Name(), destination+pathSeperator+f.Name())
    if err != nil {
    fmt.Println("Move command execution error: ", err)
    panic(err)
    }
}

Having searched stackoverflow and golang's resources, i found the issue listed in 2016 that reported this fault and according to the issue it was fixed, but i am unable to get this to work. Nowhere else that i can find lists this issue go golang.

checking the f.Mode for access, i get drwxrwxrwx and have complete access to all the files and directories.

Any help with this would be great, racking my mind. Thank you.

  • 写回答

1条回答 默认 最新

  • _还行_ 2022-01-18 13:41
    关注

    解决了吗?同样的问题

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧