Find all files on any drives


(Norbert Melzer) #21

Using the loop seems to work, the program runs then on the play ground without any errors or any output.

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


(ali) #22

your code just returned drives list…
i can give system drives list…
i want find any files on the drives…


(Norbert Melzer) #23

As I said already, I’m not using windows, nor would I have access to one (where I could install go).

Also I have to say, that https://play.golang.org/p/gmk3lhGU070 seems to work, as it prints a list of files on the system.

So it is probably some windows thing. Have you tried inspecting err in the WalkerFn or the path or anything?


(ali) #24

yes bro… but not working!

your code is true… But I want to show me all drives files… you entered directly path.
this is my idea :
1- find windows drives list
2- find files on the drives
3- show me files path


(Norbert Melzer) #25

If you use my code, but provide "C:\\" directly, does it print you something? If yes, then you are probably detecting your drives wrong.


(ali) #26

https://play.golang.org/p/gmk3lhGU070
this script working Properly … but i want this script using windows drives.


(Yamil Bracho) #27

Check this

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

}


(Norbert Melzer) #28

Does that script run on your computer when you hardcode a single drive you know to exist?

Its not the first time I’m asking exactly this…


(ali) #29

yes …


(ali) #30

i can get drives list. i need find any files on founded drives


(Norbert Melzer) #31

Okay, you know it works for a single drive letter, and you said you have a way to get the drive letters, now just loop over the drive letters with the body in my example, only the hardcoded drive letter swapped by the looping variable. Does that work or do you see any error?

If it does neither work, nor it prints any error, please show the code you used.


(ali) #32

with my script i just can give drives list
check below link please :
https://play.golang.org/p/ddxHzaBkWjv


(Norbert Melzer) #33

Again.

You said, can get a slice of available drives.
You said you can get the list of files printed for a single hard coded drive.

Whats the hard part to loop over the list of drives and just print the list for each of those?

Or instead of printing, append them to a slice which you initialise empty before the loop?


(ali) #34

I sloved this problem. ty guys…

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


(Norbert Melzer) #35

Nice you managed it!

I did some cleanup which massively removed indentation.

  1. Happy path is left aligned, instead of doing something if err == nil, you skip it if err != nil.
  2. extracted the walker function.

I also built the “root” once, instead of doing two string concatenations, which even do different things.


(Norbert Melzer) #36

And now I realize, that I even forgot to link to my changes on the playground :wink:

I’ll redo and link when I have a computer available.


(Norbert Melzer) #37

As promised, my version: https://play.golang.org/p/CnEI3X9CyzJ


(Steve) #38

Your patience and persistence is admirable.