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.

currently i am rename my file with below command line i want to add alphabetic character to each file name like file-a.jpg file-b.jpg

dir im* | ForEach-Object -begin { $count= 1 } -Process { Rename-Item $_ -newname
"img$count.jpg"; $count++} 
share|improve this question
    
What would happen if there's more than 26 files? –  Ƭᴇcʜιᴇ007 Jun 1 '12 at 6:16
    
I think i can use below command but if you have any better solution then show me @techie007 dir *.jpg | foreach-object -begin { $count= 1 } -process { rename-item $_ -Newname " image-$([char](64 + $count))$([char](96 + $count)).jpg"; $count++}' –  raj Jun 3 '12 at 12:17

1 Answer 1

up vote 3 down vote accepted

You can use the [char] class to convert an integer to an ASCII character.

ASCII code 97 is character a, so you could use 96 + the current loop count ($count); something like:

dir im* | ForEach-Object -begin { $count= 1 } -Process { Rename-Item $_ -newname
"file-$([char](96 + $count)).jpg"; $count++}

I haven't tested your/this code exactly, so you may have to fiddle with it a bit. :)

share|improve this answer
    
Shouldn't it be "file-$([char](96 + $count)).jpg"? –  Bob Jun 1 '12 at 6:29
    
Probably. ;) Let me fix that.. –  Ƭᴇcʜιᴇ007 Jun 1 '12 at 6:31
2  
@raj $() means evaluate the contents and substitute them. It can be used within strings quoted with " quotes. [char] casts the value following it as an ASCII code into a character. The value following it is (96 + $count). ASCII code 97 is a, so when the variable $count is equal to 1, you get 97 which is cast into a. $count++ increments the $count variable (++ increases the value of a variable by one). –  Bob Jun 1 '12 at 11:38
1  
Thanks for helping me and giving your valuable time @Bob techie007 –  raj Jun 1 '12 at 17:36
1  
@raj I should warn you that special characters, possibly invalid in filenames, will start to appear after z, when $count exceeds 26. You never did reply to techie007's comment on the question asking what to do in that case. –  Bob Jun 1 '12 at 17:42

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.