0

Looking to build a tree of directories and files, with output quite similar to that of the tree command. With the additional functionality that the actual files can be piped to powershell in order to, for example, here, search with regex through the files.


The Calibre library holds a number of files.

Attempting to loop through its directories and sub-directories:

$files = Get-ChildItem "/home/nicholas/Calibre Library/"

foreach ($f in $files){
    $outfile = $f.FullName + "out" 
    Get-Content $f.FullName | Where-Object { ($_ -match 'step4' -or $_ -match 'step9') } | Set-Content $outfile
}

But, that just generates pages of errors:

  11 |      Get-Content $f.FullName | Where-Object { ($_ -match 'step4' -or $ …
     |      ~~~~~~~~~~~~~~~~~~~~~~~
     | Unable to get content because it is a directory: '/home/nicholas/Calibre Library/Unknown'. Please use
     | 'Get-ChildItem' instead.

The notion is to search ".txt" files using regex. First, need to loop through the ".txt" files in the Calibre library.

(Or, perhaps, just pass the output of "tree" to powershell? Might be far more pragmatic? The 'tree' command certainly works from the REPL console. At least on Linux. I'll perhaps make that a seperate question.)

  • your post makes very little sense to me. why do you expect to get the content of a DIRECTORY? what on earth are you actually trying to do? – Lee_Dailey Feb 22 '21 at 00:54
  • Hopefully the new paragraph at the top gives some context @Lee_Dailey – Nicholas Saunders Feb 22 '21 at 01:08
  • 2
    You may limit the result of your `Get-ChildItem` command to only files with the parameter `-File`. ;-) – Olaf Feb 22 '21 at 01:13
  • 1
    And you may search for patterns in files a little more efficiantly with the cmdlet `Select-String` – Olaf Feb 22 '21 at 01:15
  • @NicholasSaunders - thank you for the clarification. [*grin*] ///// as Olaf pointed out, the faster/safer methods would be to grab ONLY files with the `G-CI` call AND to use a `-Filter` to get only txt files. then feed the `.FullFileName` value into `Select-String` using it's `-Path` parameter. finally, use the gathered info to build a `[PSCustomObject]` that has only the properties you want. – Lee_Dailey Feb 22 '21 at 02:29

0 Answers0