![]() ![]() The above could (probably) be re-written as a one-liner just using pipes and without variables, and I leave that as an exercise for you the reader :). Set-Content -Value ($contents -replace "Your_Filter", "My_Filter") -Path $file.PSPath when executed this step will cause the text Your_Filter to be replaced with My_Filter and then saved to the file.if($contents -match "Your_Filter") as above -match invokes a regex comparison on the variable.$contents = Get-Content -Path $file.PSPath -Raw gets the contents of the current file as a single string.Get-ChildItem -Recurse | ? with $_ replacing $file. The last part then is to use Rename-Item to do the rename. For this we can use the Where-Object Cmdlet, which is aliased to ?. What we can do though is pipe the files through to another Cmdlet and filter them there. Rename-Item cmdlet in PowerShell is used to rename part of the filename by passing the file path to be replaced with a new name. (Yes, I’m aware Windows isn’t case sensitive, but NTFS is….) Regexes are case sensitive, and Powershell does regex by default. My issue is that actually I only want to change files named *Your-Filter*and ignore *your-filter*. If you’re here your probably aware of Get-ChildItem -Filter "your-filter". My first step was to apply some file renames, but only to specific files. So now is the time to document how I did it today for posterity. ![]() ![]() Except the problem is, whilst I know I can do these changes in Powershell the exact commands always elude me and I need to work them out each time. This week it has been my turn to do something very similar. A colleague did this last week, using sublime text to make the required changes within files, and then I helped him put together the necessary changes to rename files in Powershell. For reasons I today needed to apply some bulk updates to a lot of code files. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |