Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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!

share|improve this question
    
When you say 'aaa', 'bbb', 'ccc', etc, do you mean that literally? –  EBGreen May 15 '13 at 15:06
add comment

2 Answers

up vote 2 down vote accepted

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++
}
share|improve this answer
    
Thanks!!!!!!!It comes in handy for someone like me who almost know nothing about powershell / batch scripting –  ella May 15 '13 at 22:54
add comment

It would matter how you want to name them. Using something like 1,2,3,4 is easiest. You would need to declare the variable for the name and an incrementer. Although that is relatively easy and straight forward. For the finding the order of creation:

Get-ChildItem h:\CallCList | Sort-Object CreationTime |`
    ForEach-Object {
    Rename-Item $_.Name -NewName $NameToUse
    }

That would be the work of the it for finding and renaming.

share|improve this answer
    
Thanks a lot! I would rename them to some strings which are already set, I will declare the variable for them:) –  ella May 15 '13 at 22:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.