Hacker News new | past | comments | ask | show | jobs | submit login

    ls | % {rename-item $_ -newname $('myfile_' + ($_.BaseName -replace '\D') + '.md' )}
No need to remember anything.



</sarcasm>? I would be much more likely to remeber ls | renamer than figure out how to write such a command.


> </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 }


This is a lot to remember, and a typo could mess things up badly.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: