Search code examples
notepad++case-sensitive

How do I swap cases in Notepad++?


Is there a way to swap cases in Notepad++, so in a set of text which I select, the uppercase letters are converted to lowercase, and the lower case letters are converted to uppercase.

Why?

I have hundreds of subtitle files, which I spent a few hours downloading for somebody who is deaf, and needs them. However, all the subtitles have the cases the wrong way round.

This is what I am talking about:

enter image description here

It is really annoying. This carries on through the entire thing, and it would take years to rewrite, or right click and swap the case.


Solution

  • Okay, here's what you need to do. First of all make a folder containing all of these subtitle files. For example let's call it "subtitle", also make a copy "subtitle_backup" (just in case).

    Now let's say your subtitle folder is inside My Documents, create a file called correct.py in My Documents, or whichever directory also contains your subtitle directory. Do not put correct.py inside "subtitle", it should go in the directory directly above.

    Copy and paste this code into correct.py

    from os import listdir
    from os.path import isfile, join
    import sys
    
    dirpath = sys.argv[1]
    onlyfiles = [ join(dirpath,f) for f in listdir(dirpath) if isfile(join(dirpath,f)) ]
    
    def correctFile( filePath ):
            f = open(filePath, "r")
            data = f.read()
            f.close()
            fixedF = ""
            for i in data:
                    if i.lower() == i:
                            fixedF = fixedF + i.upper()
                    else:
                            fixedF = fixedF + i.lower()
            return fixedF
    
    for fi in onlyfiles:
            corrected = correctFile(fi)
            f = open(fi, "w")
            f.write(corrected)
            f.close()
            print "Corrected file %s" % fi
    

    Now open the command prompt, go to the directory where your correct.py file and your subtitle folder are. Run:

    python correct.py subtitle

    Hope this helps, comment if you need clarification on any of the steps!