Saturday 17 February 2018

powershell - How to rename some files according to their timestamp


suppose I have 10 files generated by the system every day under D:\Temp. The names are RANDOMLY when generated.


Assume I need to rename the oldest one to be 'aaa', the second oldest one to be 'bbb'..then 'ccc'...'ddd'.


can someone help to write batch script? Powershell cmdlt will be good as well.


Thanks!



Answer



Here is a solution using a list of names:


$names = @('one.txt', 'two.txt', 'three.txt', 'four.txt', 'five.txt', 'six.txt', 'seven.txt', 'eight.txt', 'nine.txt', 'ten.txt')
$count = 0
foreach($file in (Get-ChildItem i:\temp | Sort-Object LastWriteTime | Select-Object -First 10)){
Rename-Item $file.FullName (Join-Path $file.DirectoryName $names[$count])
$count++
}

No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...