Walking through a directory execute the same code twice

Hi,
I have a program that walks through a directory and for every pdf file it founds it does some action, but I am not sure why it is executing this action twice.

Might it be because when it finds the file “.”, which in linux is the current directory, it starts over? Is there any way to avoid this?

Hey @jandres,

It’s hard to tell what’s happening without you showing the code that you are having trouble with.

Either way, there should be no problem if you just simply use filepath.Walk, unless you’re already using it.

Here’s an example based on my previous response. Note that info is an os.FileInfo object so you get information such as modtime, size, whether it’s a directory, it’s name, mode and more:

package main

import (
	"fmt"
	"os"
	"path/filepath"
)

func main() {
    // Walk recursively starting at the current directory.
	filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		fmt.Printf("Path: %s, Size: %d, ModTime: %v\n", path, info.Size, info.ModTime())
		return nil
	})
}

Hi,
Thanks a lot for you help. Here is a snippet, I hope do_something omitted doesn’t affect the behavior of the program.

So what have is a directory with only one PDF file, so, from GNU/Linux point of view I have 3 files:

.

some.pdf

So what I get is :

No match
No match
Match (and does the process )

No match
No match
Match (and does the process )

I am not sure if there is enough information here… But I am just starting with GOLANG.

Cheers.

	sftp_destination, err := sftp.NewClient(client_destination)
	// Look for pending pdfs
	w := sftp_destination.Walk(*sourceAlbaransPath)
	for w.Step() {
		if w.Err() != nil {
			continue
		}

		match, _ := regexp.MatchString(".pdf$", w.Path())

		log.Println("*************************************************************")
		fmt.Println("Primer Match:", match)
		fmt.Println("Walker:", w.Step())
		if match {
            do_something()
		} else if match == false {
			fmt.Println("Match false, no files found...")
		}
    }




Hey @jandres,

By the looks of it, whatever sftp package you are using seems to be using the kr/fs package underneath, so can you try replacing your loop with this and let me know what the output is:

for w.Step() {
	if err := w.Err(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		continue
	}
	if filepath.Ext(w.Path()) == ".pdf" {
		fmt.Println(w.Path())
	}
} 

Just by the way, you definitely don’t need to be using regular expressions for something as simple as checking an extension. I would personally just use filepath.Ext like in the above example.

You’ll need to import path/filepath and os for the above example to work. (Not sure how much Go you’ve done, but just specifying incase you are very new :slight_smile: )

Ok, I think I know where the problem is.

I was wrong when I stated that I have 3 files:

.

file.pdf

I actually had:

.

file.pdf
backup

And “backup” is a directory. So, when removing this, everything worked as expected. Not sure how the program was acting.

Regarding the extension, definitely is much simple to use the approach you just showed. I will try with this now.

Thanks a lot.

No worries!

My assumption was that you probably had a second file that you were unaware of so that’s why I thought printing the full paths of the found pdf’s might help :slight_smile:

Alright, that is what it was!

Cheers!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.