News for hackers. Stuff that matters.
Re:New(ish) command line tools by justsomehnguy on Wednesday April 13, 2022 @12:52PM (#31017016)
> </sarcasm>?

Nope.

> to remeber

That's the point, you don't need to remember anything, it would come naturally after a day of tinkering with PS/pwsh.

You just write it for your current needs.

This example was just a quick one-liner, it's very simple:

    ls | % {            # get the file list, cycle for each item
    
    rename-item         # obvious usage: rename-item oldname newname
    
        $_              # pretty obvious, the source - place the loop variable here ie file name
      
        -newname        # explicit calling for a parameter name, isn't really needed 
        
          $(            # subroutine start or eval if you prefer, to construct the new file name:
                        # 'text' + (take property basename from the file and regex it) + 'text'
                        
            'myfile_' + ($_.BaseName -replace '\D') + '.md'
            
          )             # subroutine end
          
      }                 # cycle end.
It could be simplified (in this example) to this:

    ls | % {
        $a = 'myfile_' + ($_ -replace '\D') + '.md'
        rename-item $_ $a
        }
which could be even written as a one-liner if you prefer it right now, just add a semicolon after `$a = ...`

    ls | % {$a = 'myfile_' + ($_ -replace '\D') + '.md'; rename-item $_ $a }

There are no comments on this comment yet.