Bulk file name encoder using python

Python is really an interesting and user friendly scripting language. It is easy to write codes in python compared to C and some other high level languages..
     It will be much difficult to rename or encode all the file names manually to a particular pattern if the number of files in a folder are too large... For example, If we want to rename all files (for example 1000 files) in a folder to just numbers ,say, 1,2,3 etc with in seconds, this could be easily done using python.
     Below code could be used for the purpose. Also, we could easily rename it back to the original file names using the same script (just double on the rename.py).  A backup file containing the original file names is created in the same folder so that we could rename the numbered files back to its original file names at any time... The renaming to numbers and the reverse could be done just by clicking the same  "rename.py" script copied in the folder containing the files to be bulk renamed and this is very simple and easy process...


python script:
import os
def read_backup():
    f = open("backup_never_delete","rU")
    k = f.read()
    f.close()
    return k.split("\n")
    
def main():
    fname = os.listdir(".")
    if "backup_never_delete" in fname:
        names = read_backup()
        count = 1
        for i in names:
            if i != '':
                if str(count) in fname:
                    os.rename(str(count),i)
                count = count + 1
        os.remove("backup_never_delete")
    else:
        f = open("backup_never_delete","w")
        count = 1
        for i in fname:
            if i != "rename.py":
                os.rename(i,str(count))
                f.write(i+"\n")
                count = count + 1
        f.close()
        os.popen("attrib +h +s " + "backup_never_delete") 
if __name__ == '__main__': 
    main() 
 

Procedure:

1> Just copy the above python code to a file "rename.py" and copy the "rename.py" file to any folder in which you wish to rename all the file names to numbers...
caution: Don't give any other name other that "rename.py"...Otherwise the python script itself will be renamed and we could not rename the files back to their original names!

2>Now, just double click on the "rename.py" and see all the files in the folder containing the "rename.py" is renamed to numbers...
caution: Dont ever try to delete the backup file "backup_never_delete" generated in the same folder, if it is deleted, the files could not be renamed back to the original file names...But any way the backup file could not be visible normally because it have a hidden system file attribute.. (It could be visible by editing the folder option, but not recommended )

3> Now, double click on the same "rename.py" to rename the files back to their corresponding original file name... The script could be copied to any folder in which the file names are to be encrypted with numbers.......:)

screen shoots:


          (before double clicking on the rename.py)





           (after double click, all the files inside the folder is renamed with numbers, also a backup file is generated which contain the information about the real file names (it is a hidden system file so that it is not visible with default folder option settings in windows)...On another double click on the rename.py, the numbers are replaced with the original file names with in fraction of seconds as in the first screen shoot)

Now if we want to encode the file name to a any specific string followed by number, for example,
instead of {1,2,3, etc }, if I want to encode it as {abc1, abc2, abc3, ....}, for that,
just a small modification in code is enough...ie, we need to replace all str(count) with  "abc" + str(count).

Also, if we want a particular extension to encoded file names, for example .mp3, then again modify the code by replacing all str(count) with str(count) + ".mp3"
Also, the numbering could be replaced by alphabets or any string by slight modifications on the above script ... 

Python is really a great tool..........

2 comments :