douyinzha5820 2017-03-30 20:50
浏览 37

用Go编写的Directory Walker停止运行,并出现文件正在使用(锁定)错误

I have this directory walker:

package main

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

var exts = [...]string{"*.psd", "*.cdr", "*.tiff", "*.svg", "*.png", "*.jpeg", "*.jpg", "*.pdf", "*.txt", "*.rtf", "*.docx", "*.doc", "*.xlsx", "*.xls", "*.ppt", "*.pptx", "*.accdb", "*.csv", "*.dwg", "*.dxf", "*.dng", "*.arw", "*.srf", "*.sr2", "*.bay", "*.crw", "*.cr2", "*.dcr", "*.kdc", "*.erf", "*.mef", "*.mrw", "*.nef", "*.nrw", "*.orf", "*.raf", "*.raw", "*.rwl", "*.rw2", "*.r3d", "*.ptx", "*.pef", "*.srw"}
var skipdir = [...]string{"WINDOWS", "Program Files"}

func VisitFile(fp string, fi os.FileInfo, err error) error {
    proceed := true
    if err != nil {
        fmt.Println(err) // can't walk here,
        return nil       // but continue walking elsewhere
    }
    if fi.IsDir() {
        return nil // not a file.  ignore.
    }
    for _, value := range skipdir {
        if strings.Contains(filepath.Dir(fp), value) {
            proceed = false
            break
        }
    }
    if proceed == true {
        for _, value := range exts {
            matched, err := filepath.Match(value, fi.Name())
            if err != nil {
                fmt.Println(err) // malformed pattern
                return err       // this is fatal.
            }
            if matched {
                fmt.Println(fp)
            }
        }
    } else {
        //fmt.Println(proceed)
        return nil
    }
    return nil
}

func getdrives() (r []string) {
    for _, drive := range "ABCDEFGHIJKLMNOPQRSTUVWXYZ" {
        _, err := os.Open(string(drive) + ":\\")
        if err == nil {
            r = append(r, string(drive))
        }
    }
    return
}

func main() {
    drives := getdrives()
    for _, value := range drives {
        filepath.Walk(value+":/", VisitFile)
    }
}

However, when it gets to pagefile.sys, I get this message:

GetFileAttributesEx C:\pagefile.sys: The process cannot access the file because it is being used by another process.

because the file is in use, and program stops. How do I suppress this message to continue walking?

  • 写回答

1条回答 默认 最新

  • duankuang1046 2017-03-31 08:16
    关注

    If you want your walk to finish, you should not return the error inside your function. You should collect the errors inside a slice. Change your function to:

    // Put this var outside your function
    var myErrors []error
    func VisitFile(fp string, fi os.FileInfo, err error) []error {
    

    Just make a variable as slice of errors. And then change the return err

       if proceed == true {
            for _, value := range exts {
                matched, err := filepath.Match(value, fi.Name())
                if err != nil {
                    fmt.Println(err) // malformed pattern
                    // collect the error
                    myErrors = append(myErrors,err)
                    // you could jump out of this by 
                    return nil
                }
                if matched {
                    fmt.Println(fp)
                }
            }
        }
    

    At the end you can check the myErrors slice.

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效