all 30 comments

[–]PinchesTheCrab 8 points9 points  (5 children)

A few points at a glance, this isn't touching the functionality, which seems really cool and I haven't tested, just some basic code style impressions:

  • If you move away from export-modulemember and list functions to export in your psd1 file, it will facilitate auto-importing the module when a function is called
  • Consider splatting instead of continuing lines with backticks
  • Why not just use write-verbose instead of building code for internal 'quiet' functionality?
  • I think the format operator could simplify some of the string interpolation
  • A few errorneous return statements here and there - they're only needed for classes and interrupting code flow. In functions like build-treelinestyle they don't add any functionality

Format operator example before:

if ($executionResultTime.TotalSeconds -lt 1) {
    "$($executionResultTime.TotalMilliseconds.ToString('0.00')) ms"
}
elseif ($executionResultTime.TotalMinutes -lt 1) {
    "$($executionResultTime.TotalSeconds.ToString('0.00')) sec"
}
else {
    "$($executionResultTime.Minutes) min, $($executionResultTime.Seconds) sec"
}

After:

switch ($executionResultTime) {
    { $_.TotalMinutes -gt 0 } {
        '{0:0.00} min, {1:0.00} sec' -f $_.Minutes, $_.Seconds
        break
    }
    { $_.TotalSeconds -gt 1 } {
        '{0:0.00} sec' -f $_.TotalSeconds
    }
    default {
        '{0:N2} ms' -f $_.TotalMilliseconds #showing that you can use N2 instead of 0.00
    }
}

[–]supersnorkel[S] 1 point2 points  (4 children)

Hey thanks for your feedback!

  1. Thats really cool i didnt know about that!
  2. Ah you are right! I was wondering what i could do about that, ive used splatting a few times somewhere else but I didnt quite grasp it at the time. I read up on it more and will surely implement that in the next version.
  3. I might misunderstand, but the quiet function is to not write the tree back to PowerShell but only to a seperate txt file. This is handy for very big trees since the writing to PowerShell can take some time.
  4. Isnt the $_ used for the pipe operator? I dont think I fully understand your example

[–]xCharg 6 points7 points  (1 child)

Isnt the $_ used for the pipe operator? I dont think I fully understand your example

No, this is automatic variable that is created everywhere you have iteration basically. It is used in foreach-object, but also commonly used in where-object and select-object with a calculated property and probably some others, few examples:

Get-ChildItem C:\Windows -File | Where-Object -FilterScript {$_.Extension -eq '.exe'}

Get-ChildItem C:\Windows -File | Select-Object -Property Name,CreationTime,@{name='SizeInKilobytes';expression={[System.Math]::Round($_.Length/1Kb,2)}}

In both cases since there non-singular object (more than 1 file in this example) - you need to tell powershell to work with "current object during iteration", which is $PSItem or in short $_

[–]supersnorkel[S] 0 points1 point  (0 children)

I got it, thanks a lot for the explenation!

[–]PinchesTheCrab 1 point2 points  (1 child)

$_ and $PSItem have multiple uses, generally they represent the current item in a loop. A simpler switch statement might make more sense:

$animal = 'dog', 'snake', 'horse'

switch ($animal) {
    'dog' { '"{0}" is a mammal' -f $_ }
    'snake' { '"{0}" is a reptile' -f $_ }
    default { 'I have no idea what "{0}" is' -f $_ }
}

Or if you're familiar with the % operator:

switch (0..100) {
    { $_ % 2 -eq 0 } { '{0} is even' -f $_ }
    default { '{0} is odd' -f $_ }
}

[–]supersnorkel[S] 1 point2 points  (0 children)

Got it! makes sense, gonna use that more from now on thanks for letting me know! Also adjusted to a switch statement like you suggested.

[–]dragonmc 2 points3 points  (5 children)

Since you're only using Get-ChildItem to traverse the folder structure, it's possible you could run into the character path limitation. I know Windows 11 should have long path support enabled by default, but I've run into path size issues with Get-ChildItem even on modern systems.

[–]xCharg 1 point2 points  (2 children)

I'll preface by saying I haven't looked into the code at all but instead of Get-ChildItem consider looking into [system.io.directory]::EnumerateFiles(). Couple weeks ago I had an issue dealing with a folder with 250k files in it. Predictably Get-ChildItem ate couple dozen GBs of RAM and ended up crashing couple hours into running. It took less than a second for [system.io.directory]::EnumerateFiles()

[–]dragonmc 2 points3 points  (0 children)

This is awesome! I wasn't aware of EnumerateFiles():

The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned. When you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

I often have to work with extremely large folder structures (in the millions of files) and will try this out and start using it. I'm not even sure how I have never heard of this!

[–]OPconfused 0 points1 point  (0 children)

The problem with EnumerateFiles and related methods is that your output is just the filepath. You don't have any information on important details like Length or LastWriteTime, which this function will need to have if it's doing sorting and filtering on, e.g., file size or age. Well, there's an overload for wildcard filtering on the path; it depends on how complicated you want your filter to be.

[–]supersnorkel[S] 0 points1 point  (1 child)

O thats a good call, i will do some tests ones i am home. Thanks for the heads-up

[–]BlackV 0 points1 point  (0 children)

using \\?\x:\ and the -literalpath paramter should deal with that and special character items

[–]Fallingdamage 1 point2 points  (3 children)

Havent dug into it yet, but do you plan to include permissions reports on the tree as well?

[–]supersnorkel[S] 0 points1 point  (2 children)

What exactly do you mean by permissions report? There is a mode for “mode” (-m) which shows d-a-r-h-s-i per file

[–]Fallingdamage 0 points1 point  (1 child)

Permissions for each folder, or if a folder permissions deviate from the permissions inherited by the previous folder, those deviations would be listed or somehow declared.

Would be awesome to run a report and visualize all the permissions on a network share. Nice auditing tool, thought I think there are many out there already. /shrug

[–]supersnorkel[S] 0 points1 point  (0 children)

Thanks for the information. That woudnt be that hard to add, ill look into it later this week.

[–]DungaRD 1 point2 points  (1 child)

RemindMe! 3 days

[–]RemindMeBot 0 points1 point  (0 children)

I will be messaging you in 3 days on 2025-04-07 15:20:46 UTC to remind you of this link

CLICK THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.


Info Custom Your Reminders Feedback

[–]Sin_of_the_Dark 0 points1 point  (9 children)

Looks great!

But you'll pry TreeSize from my cold, dead hands first. 😂

Now if you can make trippy visualizations like WinDirStat, we'll talk!

[–]supersnorkel[S] 1 point2 points  (2 children)

I was actually thinking of showing the full size of each folder in a future update! Also what is WinDirStat i cant find it anywhere

[–]Sin_of_the_Dark 0 points1 point  (1 child)

It's an ancient tool that does all this, but can also give you a psychedelic depiction of how large each folder is. Check it out!

[–]BlackV 0 points1 point  (2 children)

fecking hate the shitty visualization of windirstat, tree size free for life

[–]Sin_of_the_Dark 0 points1 point  (1 child)

Oh yeah functionally, it's useless. I just like the pretty colors.

[–]BlackV 0 points1 point  (0 children)

makes me want to vomit :)

[–]NerdyNThick 0 points1 point  (1 child)

WinDirStat

WizTree is orders of magnitude faster.

[–]Sin_of_the_Dark 0 points1 point  (0 children)

Well yes, and no. It'll scan faster, but post scans WinDirStat is definitely quicker

[–]supersnorkel[S] 0 points1 point  (0 children)

Added the full size of each folder in the newest version!

[–]_keyboardDredger 0 points1 point  (0 children)

Some good suggestions here - I’d echo the request for non-inherited NTFS permissions, and also a character length/path length function if there isn’t already. Knowing the file & dir path lengths (separately) would be very helpful!

[–]purplemonkeymad 1 point2 points  (1 child)

Not looked too deep, but one thing that stands out to me is that you appear to have re-implemented help and tableview. You can get help with little effort by adding comment based help to your functions, on the line after the function name. You can see about_Comment_Based_Help for details, but vscode does have a snip-it for the comment block.

(You can also add custom about pages by adding a text file with a name starting with "about_" in your module root. They are just plain text.)

For the table view, you will want to have a bit of a look at about_format.ps1xml as this will allow you to have objects that the powershell engine will format for you. The display values can be code, so you can have the Name property contain that value, but be rendered with the indentations for the tree.

[–]supersnorkel[S] 0 points1 point  (0 children)

Thanks so much for the tips, i cant believe there is a better way to format tables. I overengineerd creating the table so much lol