Mass file rename matching with regular expression substring replace
Powershell Mass file rename matching with regular expression substring replace
The below is an examples of renaming files in mass based on matching file name string in regular expression form.
As well as replacing matching substring text based on regular expression.
Rename those files where any parts of the file name match regular expressions and only replace those parts:
Source
ForEach ($myfile in (Get-Childitem "C:\Test")){
Rename-Item $myfile.FullName -newname ((((( $myfile.PSChildName ) -creplace "^\d{4}.*-","" ) -creplace "\(.*\).*\(.*\)","") -creplace "\s\.",".") -creplace "^\s","")
}
Rename those files that only match a regular expression and any parts of the file name that match regular expressions and only replace those parts,
Just an example of using the where clause with a regular expression "-match":
Source
ForEach ($myfile in (Get-Childitem "C:\Test")) | Where { $_.PSChildName -match "^\d{4}.*-.*"} ) {
Rename-Item $myfile.FullName -newname ((((( $myfile.PSChildName ) -creplace "^\d{4}.*-","" ) -creplace "\(.*\).*\(.*\)","") -creplace "\s\.",".") -creplace "^\s","")
}