Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Walk example #293

Open
markpendlebury opened this issue Oct 23, 2022 · 3 comments
Open

Walk example #293

markpendlebury opened this issue Oct 23, 2022 · 3 comments

Comments

@markpendlebury
Copy link

Would someone be kind enough to share an example of how to use walk? Honestly i'm unable to work it out myself from the documentation (i'm still leaning go)

Thanks

@stale
Copy link

stale bot commented Mar 25, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Will auto close after 14 days label Mar 25, 2023
@LaughingBubba
Copy link

Would someone be kind enough to share an example of how to use walk? Honestly i'm unable to work it out myself from the documentation (i'm still leaning go)

Thanks

Yeah, I'm in the same boat. I looked at the tests for the Walker and then had a second look at the doco.

func main() {

  host   := "ftp.something.host:21"
  folder := "/some_folder"
  user   := "gonzo"
  passwd := "secret"

  c, err := ftp.Dial(host, ftp.DialWithTimeout(5*time.Second))
  if err != nil {
      log.Fatal(err)
  }

  err = c.Login(user, passwd)
  if err != nil {
      log.Fatal(err)
  }  

  w := c.Walk(folder)

  ok := w.Next() // Get next (first) entry in the tree
  for ok == true {
    e := w.Stat() // Get the current entry
    fmt.Println(e.Type, e.Name, w.Path())
    ok = w.Next() // Get next
  }

  if err := c.Quit(); err != nil {
      log.Fatal(err)
  }

}

@stale stale bot removed the stale Will auto close after 14 days label Feb 17, 2024
@wolveix
Copy link

wolveix commented Apr 26, 2024

It's pretty similar to other libraries :)

package main

import (
	"fmt"
	"io"
	"log"
	"time"

	"github.com/jlaffaye/ftp"
)

func main() {
	conn, err := ftp.Dial("your.host:21", ftp.DialWithTimeout(5*time.Second))
	if err != nil {
		log.Fatal(err)
	}

	if err = conn.Login("username", "password"); err != nil {
		log.Fatal(err)
	}

	walker := conn.Walk("/your/path")

	for walker.Next() {
		// access current full path
		fmt.Println(walker.Path())

		// access entry (file or folder)
		fmt.Println(walker.Stat().Name)
	}

	if walker.Err() != nil && walker.Err() != io.EOF {
		log.Fatal(err)
	}

	if err = conn.Quit(); err != nil {
		log.Fatal(err)
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants