script icon indicating copy to clipboard operation
script copied to clipboard

Fix problem with ListFiles producing an unwanted Newline character when zero files are matched

Open MrTbag opened this issue 2 years ago • 1 comments

In this PR I tried to fix #110

Here I take into consideration whether any files are matched at all or not and then ListFiles() only produces an output if files are matched.

To fix the issue with the ListFiles function, you would need to handle the case where the provided path argument refers to a single file instead of a directory. Currently, the function assumes that if an error occurs while reading the directory, it's because the path matches exactly one file. However, this assumption is incorrect in some cases.

In the updated code:

  • We use os.Stat to check whether the path argument refers to a directory or a file.
  • If it's a file, we return it as the output by creating a new Pipe and using Echo to set its content to the file path.
  • If it's a directory, we proceed with the original logic of walking through the directory and appending file paths to the matches slice.

By making these changes, the ListFiles function will correctly handle both directory and file inputs.

#web_1402_issue

MrTbag avatar Jul 13 '23 15:07 MrTbag

Hi, @MrTbag! Thanks for the contribution—it's most welcome!

I'm not sure if I'm completely clear on the problem that this is fixing—could you help me out by supplying a couple of test cases that fail without this change?

bitfield avatar Jul 13 '23 16:07 bitfield