Renaming files is quite an easy task but if the files to be
renamed are about 100 files or more then you have to think twice and is not fun
doing repetitive simple task.
* = in regex match any character or basically don't care what character
Rather than specifying one by one files with "2019" regex will automatically do the job. Saves a lot of time.
Search the web for "regex operator" to know more.
Anyway, PowerShell is handy for this kind of task.
Regex in PowerShell is helpful in order to filter only
matching files that needs to be renamed and leave any other files that doesn’t
need to be changed.
Example filename is like this: SampleTestData1_2019Result.csv
New desired filename would be: SampleTestData1_2020.csv
PowerShell code with regex to rename matching files.
How to use basic regex in PowerShell? The code below is a simple one.
How to use basic regex in PowerShell? The code below is a simple one.
Get-ChildItem -Path "c:\dev"| Where-Object {$_.BaseName -Match '2019.*'} |
Rename-Item -NewName {($_.BaseName -Replace '2019.*', '2020')+$_.Extension}
#'2019.*'
– is the regex that matches 2019 until the end of the string
#'2019.*', '2020' – replace
the match string with 2020
* = in regex match any character or basically don't care what character
Rather than specifying one by one files with "2019" regex will automatically do the job. Saves a lot of time.
Search the web for "regex operator" to know more.
That’s it a one liner PowerShell code can save the day.
Cheers. Till next time.
================================
Free Android Apps:
Click links below to find out more:
Excel Keyboard guide:
Heaven's Dew Fall Prayer app for Android :
Catholic Rosary Guide for Android:
Comments
Post a Comment