roger pence

Potentially, each day is crucial in the total development

Search
Advanced Search
Folder to search:
Limit search to one of these properties:

    PowerShell find-files script to find files in a directory

    Show all *.svelte files in the current directory.

    find-files *.svelte 
    

    produces:

    Name                                  LastWriteDate DirectoryName
    ----                                  ------------- -------------
    Anchor.svelte                         2024-08-23    C:\Users\thumb\Documents\...
    AnchorLink.svelte                     2024-12-03    C:\Users\thumb\Documents\...
    ArticleIntro.svelte                   2024-08-23    C:\Users\thumb\Documents\...
    

    The directory name above is truncated for this article. The full directory name shows on the command line.

    Truncate the directory name

    Show all *.svelte files and truncate the directory name started at the src directory.

    find-files *.svelte src
    

    produces:

    Name                                  LastWriteDate DirectoryName
    ----                                  ------------- -------------
    Anchor.svelte                         2024-08-23    src\components\text-decorators
    AnchorLink.svelte                     2024-12-03    src\components\all-locales\all-pages
    ArticleIntro.svelte                   2024-08-23    src\components\text-decorators
    

    If the directory name provided as the second argument doesn't exist, the entire directory name is shown.

    If you want a different sort order:

    find-files *.css css | sort-object -property DirectoryName
    

    Script source

    param (
        [Parameter(Mandatory = $true, Position = 0)]
        [string]$Filter,
    
        [Parameter(Mandatory = $false, Position = 1)]
        [string]$FirstDirectory
    )
    
    Get-ChildItem -Filter $Filter -Recurse -ErrorAction SilentlyContinue | 
        Sort-Object Name | 
        Select-Object Name, 
        @{Name = "LastWriteDate"; Expression = { $_.LastWriteTime.ToString("yyyy-MM-dd") } }, 
        @{
            Name = "DirectoryName"
            Expression = {
                $separator = [System.IO.Path]::DirectorySeparatorChar
                $parts = $_.DirectoryName -split [regex]::Escape($separator)
    
                if ($FirstDirectory -and ($parts -contains $FirstDirectory)) {
                    $index = [array]::IndexOf($parts, $FirstDirectory)
                    $parts[$index..($parts.Count - 1)] -join $separator
                }
                else {
                    $_.DirectoryName
                }
            }
        }