OnSwipe redirect code

Friday, April 24, 2009

Recursively rename files in windows

My friend Prasanna B P asked me to solve a bunch of problems that he was facing with his computer. One of them was fairly common and there was a very easy and straight forward brute force solution. But coming up with a "smart" was really interesting.

Essentially some malware had renamed all the movie files that he had to carry a .jpg extension. His initial solution was to associate those jpg files with a movie player like Mplayer123 or VLC and the file would be invariably played without any regard to the extension. But this made the genuined jpg pictures to be opened with the player.

The obvious solution was to rename all the files and change the extension. Manually doing it from the GUI is the brute force idea that I earlier mentioned. And it is a totally crappy one. Next I can rename all files in a directory from the command line. But here the movie files were in directories of their own and hence I would have to move to each directory manually and run the rename command. This makes it as good as the GUI approach. In fact the extra effort of typing the commands might make it worse.

The I searched the internet a little and got to know that the windows command shell supports a "FOR" statement which can be used to recursively traverse directories, amongst many other things it provides. Using that I found this command:
FOR /R %x IN (*.jpg) DO ren "%x" *.avi
from this website : http://stackoverflow.com/questions/210413/command-line-recursive-renamemove-in-windows

People used to Linux Shell scripting might think of this as a wierd syntax, but yeah thats the windows choice.

It was good to do some Windows Shell scripting too. :-)