I have some 100s of files having names like this:
MOD15A2H.A2012001.h26v06.001.2015230181014_LAI500m.tif
MOD15A2H.A2012009.h26v06.001.2015236194306_LAI500m.tif
MOD15A2H.A2012017.h26v06.001.2015237120626_LAI500m.tif
MOD15A2H.A2012025.h26v06.001.2015237124147_LAI500m.tif
MOD15A2H.A2012033.h26v06.001.2015237131607_LAI500m.tif
MOD15A2H.A2012041.h26v06.001.2015237232610_LAI500m.tif
MOD15A2H.A2012049.h26v06.001.2015238141341_LAI500m.tif
MOD15A2H.A2012057.h26v06.001.2015238172148_LAI500m.tif
MOD15A2H.A2012065.h26v06.001.2015238191713_LAI500m.tif
MOD15A2H.A2012073.h26v06.001.2015238174737_LAI500m.tif
MOD15A2H.A2012081.h26v06.001.2015239174812_LAI500m.tif
MOD15A2H.A2012089.h26v06.001.2015240024933_LAI500m.tif
MOD15A2H.A2012097.h26v06.001.2015240043222_LAI500m.tif
MOD15A2H.A2012105.h26v06.001.2015240142201_LAI500m.tif
MOD15A2H.A2012113.h26v06.001.2015240161909_LAI500m.tifI need to rename them as
L2012001.tif
L2012009.tif etc.,I am attempting to rename them in bulk through Powershell commands
As far till now, I replaced "MOD15A2H.A" with "L" and ".h26v06.001." with "" using the following in powershell
>get-childitem -recurse | rename-item -newname {$_.name -replace 'MOD15A2H.A','L'}
>get-childitem -recurse | rename-item -newname {$_.name -replace '.h26v06.001.',''}Now please give me any suggestion on removing the remaining part of the name which is different for all the files.
06 Answers
You could use a regular expressions, with . to match any character except newline in your search-for expression.
Possibly useful:
Try this (You will need to escape dots by backslash in Regex):
Get-Childitem -recurse | Foreach { Get-Item $File.Fullname | Rename-Item {$_.name -replace 'MOD15A2H\.A','L'} Get-Item $File.Fullname | Rename-Item {$_.name -replace '\.h26v06\.001\.',','}
} If I have 10 files in a specific directory that are all .txt files, and I want to change the extensions to .xml, I can navigate to the directory, and type:
ren *.txt *.xml 1 One good regular expression should do it:
( Get-ChildItem -File -Recurse ) | Rename-Item -NewName { $_.Name -replace '^MOD15A2H\.A(\w+)\..+(\.tif)$', 'L$1$2' }Breaking it down:
'^MOD15A2H.A(\w+)..+(.tif)$':
^ - Beeginnig of string
MOD15A2H\.A - Literal match for "MOD15A2H.A" (\w+) - Capture group 1: one or more word characters \. - Match first "." after word characters .+ - Match all charaters up to capture group 2 (\.tif) - Captrue group 2: ".tif" $ - End of string'L$1$2':
L - Literal "L"
$1 - Capture Group 1
$2 - Capture Group 2 You can try using Get-Item and Split with foreach:
get-childitem "F:\MOD15A2H_GTiFF\LAI\*.tif" -recurse | % { rename-item $_.fullname -newname ('L'+$_.name.split('A.*/')[3]+$_.Extension) -WhatIf } - Or, using aliases...
gi "F:\MOD15A2H_GTiFF\LAI\*.tif" | % { ren $_.fullname -new ('L'+$_.name.split('.A*/')[-6]+$_.Extension) -WhatIf } - Your outputs using
-WhatIf
What if: Performing the operation "Rename File" on target "Item: F:\MOD15A2H_GTiFF\LAI\MOD15A2H.A2012001.h26v06.001.230181014_LAI500m.tif Destination: F:\MOD15A2H_GTiFF\LAI\L2012001.tif".
What if: Performing the operation "Rename File" on target "Item: F:\MOD15A2H_GTiFF\LAI\MOD15A2H.A2012009.h26v06.001.236194306_LAI500m.tif Destination: F:\MOD15A2H_GTiFF\LAI\L2012009.tif".- The output generated by
-WhatIfallows you to preview the execution, but to effectively rename your files, just remove it:
get-childitem "F:\MOD15A2H_GTiFF\LAI\*.tif" -recurse | % { rename-item $_.fullname -newname ('L'+$_.name.split('A.*/')[3]+$_.Extension) -WhatIf}
ls -r "F:\MOD15A2H_GTiFF\LAI\*.tif" | % { ren $_.fullname -new ('L'+$_.name.split('.A*/')[-6]+$_.Extension) -WhatIf}- Output results
MOD15A2H.A2012001.h26v06.001.230181014_LAI500m.tif ⇿ L2012001.tifMOD15A2H.A2012009.h26v06.001.236194306_LAI500m.tif ⇿ L2012009.tifObs.:An alternative for your case, since the strings in the name are fixed, you can use substring to easily compose the new name:
MOD15A2H.A2012001.h26v06.001.2015230181014_LAI500m.tif'L' +|_________|(10,07)|___________________________________|+ '.tif' MOD15A2H.A2012001.h26v06.001.2015230181014_LAI500m.tif
get-childitem "F:\MOD15A2H_GTiFF\LAI\*.tif" -recurse | % { rename-item $_.fullname -newname ('L'+$_.basename.Substring(10,7)+$_.Extension)}
ls "F:\MOD15A2H_GTiFF\LAI\*.tif" -r|% {ren $_.fullname -new ('L'+$_.basename.Substring(10,7)+'.tif')}- And you have the same output / results:
What if: Performing the operation "Rename File" on target "Item: F:\MOD15A2H_GTiFF\LAI\MOD15A2H.A2012001.h26v06.001.230181014_LAI500m.tif Destination: F:\MOD15A2H_GTiFF\LAI\L2012001.tif".
What if: Performing the operation "Rename File" on target "Item: F:\MOD15A2H_GTiFF\LAI\MOD15A2H.A2012009.h26v06.001.236194306_LAI500m.tif Destination: F:\MOD15A2H_GTiFF\LAI\L2012009.tif".Some further reading:
[√] Split
[√] % | Foreach
[√] ls | Get-Item
If the part of the filename that you want to keep is always between the first and second occurance of . you can also .split('.')[1] and then only replace A with L
Get-ChildItem C:\Install\tesfile -Recurse | ForEach-Object { $NewName = ($_.Name.Split('.')[1] -replace "A","L") + $_.Extension Rename-Item $_.FullName $NewName
}