Saturday, April 2, 2016

massive directory name case change

Some time ago I had a disk failure. One project code was affected. I managed to restore almost all data, but result was often a file with name changed to upper case. As this happened to a windows machine, I could probably live with that, but it turned out package name in java is case sensitive, and Idea was not able to do quick fix as quickly as I would expect. Manual change is not an option for a developer.
So let's automate that. Solution would be to recursively visit each directory and in case its case is wrong - rename. You can have that in java, I would rather be happy to have that in script, however I am not sure whether scripting cmd or bash port on windows will actually handle that rename sanely.
Fortunately there is nice tool in python that accomplishes the same goal - os.walk. Usage is trivial and script is portable to any platform. File system paths can be also abstracted easily with os.path.join. There is even a method in python string that checks case of whole content - so you will not write any extra loop iterating over characters. Python handles nicely filtering in list comprehensions - so it is possible to use the condition in for statement.
from os import walk, rename
from os.path import join

for (dirpath, dirnames, filenames) in walk( join('src', 'main', 'java') ):
  for d in [f for f in dirnames if f.isupper()]:
       rename(join(dirpath, d), join(dirpath, d.lower()))
As you can see some APIs may be even more developer friendly than what is in Java standard. Now came the difficult part - for some reason git was unable to recognize character case change in filenames. It happens that some default on windows is aligned with os approach to case sensitivity. Making git see the difference is changing its configuration:
git config core.ignorecase false

1 comment:

  1. Sands Casino & Resort - Treasure Island, NY
    Play at Sands Casino & Resort 샌즈카지노 - choegocasino Treasure Island, NY. 1xbet Discover Las Vegas casino games, including Blackjack, Roulette, Baccarat, Poker and live

    ReplyDelete