Find all files on any drives


(ali) #1

hi guys…
i want to find the files on any system drives…
what is my code problem ?
Error : cannot use root (type []string) as type string in argument to filepath.Walk

line : err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {

var files []string
  SystemDirvesList := []string{}
  for _, drives := range "ABCDEFGHIJKLMNOPQRSTUVWXYZ" {
    _, err := os.Open(string(drives) + ":\\")
    if err == nil {
      SystemDirvesList = append(SystemDirvesList, string(drives))
    }

  }

  root := SystemDirvesList
  err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
    files = append(files, path)
    return nil
  })
  if err != nil {
    panic(err)
  }
  for _, file := range files {
    fmt.Println(file)
  }```

(Norbert Melzer) #2

The error message tells it already. root needs to be a single string, not a slice thereof.

Looping over the elements in root might help?


(ali) #3

how i can slove this problem?


(Norbert Melzer) #4

Have you tried a loop, as I suggested earlier?


(ali) #5

i writing any programs with python. i tested this loop but not working ( like python )…
maybe this code need type casting.

for _,TestVariable := range DriversList{
bla bla bla
}


(Norbert Melzer) #6

Since there is no DriversList in the code you have originally shown to us, I can’t say what you have done.

Also I have no clue what the body of the loop should be, as there is no bla bla bla in the original code.

Please show the code that is not working and show the error it produces.

Also please use markdown to mark your codeblocks like this:

```go
// your code here
```

(ali) #7

Screen of Errors And Code :

http://s6.uplod.ir/i/00950/gj9cefi384i5.png

http://s6.uplod.ir/i/00950/w05bjynpbq2q.png


(Norbert Melzer) #8

Sorry, but I can not copy and paste from screenshots.

but roughly:

for _, r := range root {
  err := filepath.Walk(r, …)
  // error handling and other code
}

Using a loop is the easiest way to get a T from a []T.


PS: None of the screenshots shows what you wanted to say with your Find all files on any drives post


(ali) #9

i tested this way bot not working.please look this screen :


(Norbert Melzer) #10

The next screenshot will be pretended to not exist. Most of the time I’m browsing the forum via a mobile and screenshots are hard to read here. Properly marked code blocks work much better and do not strain my bandwith quota.

From what I can read out of the documentation of filepath.Walk, the type of the info argument needs to be os.FileInfo, perhaps you missed to qualify.

Anyway, the function you try to pass into filepath.Walk seems to be totally off… The root in the argument list seems to be missplaced.


(ali) #11

Thank you bro…


(Norbert Melzer) #12

So does it work for you now?

I’m unable to test my theories here, as I use operating systems without drive letters.


(ali) #13

no not working for me…
my script have problem.


(Norbert Melzer) #14

What kind of problems? Just try to explain your problem as exact as you are able to, and provide as much context as other might need to help you.


(ali) #15

bro this is my error :

Error : cannot use root (type []string) as type string in argument to filepath.Walk
and i dont know how i can slove this.


(Norbert Melzer) #16

Which I have told you how to fix, use the damn loop and fix the errors that occur then. One by one.


(ali) #17

hey man i tested any ideas but i can’t slove this…
help me if you can


(Norbert Melzer) #18

I did as good as I can. So after you fixed the two errors in the func literal you wanted to pass to filepath.Walk waht did happen next?


(ali) #19


(ali) #20

please go to the this link :

https://play.golang.org/p/yxmrmZbA7sc