ftp icon indicating copy to clipboard operation
ftp copied to clipboard

Walk example

Open markpendlebury opened this issue 3 years ago • 3 comments

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

markpendlebury avatar Oct 23 '22 11:10 markpendlebury

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[bot] avatar Mar 25 '23 12:03 stale[bot]

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)
  }

}

LaughingBubba avatar Feb 17 '24 11:02 LaughingBubba

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)
	}
}

wolveix avatar Apr 26 '24 19:04 wolveix