0

Question about running a custom function in Powershell.

I'm on Windows 10 and I'd like to somehow print my monorepository's directory tree structure excluding node_modules. This is not supported out of the box but requires a custom function to be defined. I found one solution on StackOverflow (https://stackoverflow.com/a/43810460/9654273), which would enable using a command like:

tree -Exclude node_modules -Ascii > tree.txt

The problem is I don't know what to do with the provided source code :D The answer says "add to your $PROFILE, for instance", so I ran notepad $PROFILE in PowerShell, pasted the code snippet there, saved it and tried running the command. It didn't work because I did something wrong. According to the StackOverflow post's comments from anand_v.singh and mklement0 I was still running some other tree command, not the one I just attempted to define.

So how do I use a custom function in PowerShell? Starting point is that source code is on StackOverflow and I don't know where to paste it. Or do you know some other, easier way to print a directory tree on Windows 10 excluding node_modules?

jka
  • 337
  • 5
  • 15
  • Additions to `$PROFILE` only take effect in _future_ sessions. Have you started a new session before trying to invoke the custom `tree` function? Alternatively, run `. $PROFILE` – mklement0 Jan 09 '21 at 13:47
  • 1
    You can also either 1) paste the whole `function tree { ... }` statement into the prompt, or 2) save it in a file with the extension `.ps1` and ["dot-source" the file](https://stackoverflow.com/questions/30504140/what-does-script-do/30504952#30504952) – Mathias R. Jessen Jan 09 '21 at 14:14
  • @mklement0 yes, I restarted PowerShell and even rebooted the whole computer. I fact, now it seems I get this error message any time I open PowerShell: https://imgur.com/a/IWhwYxk i. e. various syntax errors. The source code is copied from the aforementioned reply though. – jka Jan 09 '21 at 14:28
  • @MathiasR.Jessen okay, but how would I provide parameters in those cases? I need to define which subtrees I want to exclude (node_modules). – jka Jan 09 '21 at 14:35
  • 1
    Once you've dot-sourced the file with the `function` definition, it'll be available in your shell, so invoke using the same syntax as any other PowerShell command: `Command-Name -ParamName argument`, or in your case: `tree -Exclude node_modules` – Mathias R. Jessen Jan 09 '21 at 15:21

1 Answers1

0

I had the same problem with that function. The issue is the special characters in the hashtable at line 106:

$chars = @{
      interior = ('├', '+')[$ndx]
      last = ('└', '\')[$ndx]                                                                #'
      hline = ('─', '-')[$ndx]
      vline = ('│', '|')[$ndx]
      space = ' '
    }

I changed the special characters to ascii as follows:

 $chars = @{
      interior = ('+', '+')[$ndx]
      last = ('\', '\')[$ndx]                                                                #'
      hline = ('-', '-')[$ndx]
      vline = ('|', '|')[$ndx]
      space = ' '
    }

The only downside is that you do not now have the option of using special graphics characters (the Ascii switch is still there, but does nothing). Maybe someone could tell us how to embed them properly.

rcriii
  • 687
  • 6
  • 9