Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform file system scanning

Tags:

file

directory

go

  1. I need to write a function which when given the path of a folder scans the files rooted at that folder.
  2. And then I need to display the directory structure at that folder.

I know how to do 2 (I am going to use jstree to display it in the browser).

like image 788
chinmay Avatar asked Jul 07 '11 10:07

chinmay


People also ask

What is system scanning?

System scan is a network-based vulnerability scanner that is able to scan any system with an IP for common vulnerabilities. System scan's vulnerability detection is based on both active and passive vulnerability checks. For example, it will attempt to identify the service (product) and its version number.

What is a SFC scan?

System File Checker (SFC) is a utility in Microsoft Windows that allows users to scan for and restore corrupted Windows system files.

How do I find corrupted files?

Open Windows File Explorer and then right click on the drive and select 'Properties'. From here, select 'Tools' and then click 'Check'. This will scan and attempt to fix glitches or bugs on the hard drive and recover corrupt files. This is the command version of the check disk tool in step 1.


1 Answers

EDIT FOR 1.16: Enough people still hit this answer, that I thought I'd update it for Go 1.16.

The function filepath.WalkDir introduced in Go 1.16 has better performance than filepath.Walk mentioned in the previous edit. Here's a working example:

package main  import (     "flag"     "fmt"     "io/fs"     "path/filepath" )  func visit(path string, di fs.DirEntry, err error) error {     fmt.Printf("Visited: %s\n", path)     return nil }  func main() {     flag.Parse()     root := flag.Arg(0)     err := filepath.WalkDir(root, visit)     fmt.Printf("filepath.WalkDir() returned %v\n", err) } 

EDIT: Enough people still hit this answer, that I thought I'd update it for the Go1 API. This is a working example of filepath.Walk(). The original is below.

package main  import (   "path/filepath"   "os"   "flag"   "fmt" )  func visit(path string, f os.FileInfo, err error) error {   fmt.Printf("Visited: %s\n", path)   return nil }    func main() {   flag.Parse()   root := flag.Arg(0)   err := filepath.Walk(root, visit)   fmt.Printf("filepath.Walk() returned %v\n", err) } 

Please note that filepath.Walk walks the directory tree recursively.

This is an example run:

$ mkdir -p dir1/dir2 $ touch dir1/file1 dir1/dir2/file2 $ go run walk.go dir1 Visited: dir1 Visited: dir1/dir2 Visited: dir1/dir2/file2 Visited: dir1/file1 filepath.Walk() returned <nil> 

ORIGINAL ANSWER FOLLOWS: The interface for walking file paths has changed as of weekly.2011-09-16, see http://groups.google.com/group/golang-nuts/msg/e304dd9cf196a218. The code below will not work for release versions of GO in the near future.

There's actually a function in the standard lib just for this: filepath.Walk.

package main  import (     "path/filepath"     "os"     "flag" )  type visitor int  // THIS CODE NO LONGER WORKS, PLEASE SEE ABOVE func (v visitor) VisitDir(path string, f *os.FileInfo) bool {     println(path)     return true }   func (v visitor) VisitFile(path string, f *os.FileInfo) {     println(path) }  func main() {     root := flag.Arg(0)     filepath.Walk(root, visitor(0), nil) } 
like image 54
laslowh Avatar answered Nov 15 '22 18:11

laslowh