How to join files into folders, based on their prefixes?

Asked

Viewed 221 times

-2

I have in hand thousands of files that have common prefixes in their names, something like:

[Example of prefix 1]

  • ABC-1.mp4
  • ABC-2.mp4
  • ABC-3.mp4
  • [...]
  • ABC-999.mp4

[Example of prefix 2]

  • AAA-1.mp4
  • AAA-2.mp4
  • AAA-3.mp4
  • [...]
  • AAA-999.mp4

However, there are thousands of files and thousands of prefixes in common. I would like to join the files that have the same prefix in the same folder, and the folder would have the prefix name, for example:

  • ABC (would be the name of the folder)
    • ABC-1.mp4
    • ABC-2.mp4
    • ABC-3.mp4
    • [...]
    • ABC-999.mp4
  • AAA (would be the name of the folder)
    • AAA-1.mp4
    • AAA-2.mp4
    • AAA-3.mp4
    • [...]
    • AAA-999.mp4
  • Does this need to be done in C or does the language not matter? Another question, Linux or Windows?

  • Any language, it is important to resolve this problem.. Windows

1 answer

1


Solution in Groovy language 2.4.4

Download Groovy: http://www.groovy-lang.org/download.html

Java must be installed for Groovy to work.

def folders = new File("/home/cantoni/Documents/Teste")
def destFolder = "/home/cantoni/Documents/Teste2/"

folders.listFiles().groupBy {it.name.substring(0,3)}.each {group ->
    def folder = new File(destFolder + group.key)
    if (!folder.exists()) new File(destFolder + group.key).mkdir()

    def files = group.value

    files.each {
        it.renameTo(folder.absolutePath + "/" + it.name)
    }
}

Code explained

The above code goes through all the files that are inside the folder specified by the variable folders, then group them by the first 3 characters that make up the name.

folders.listFiles().groupBy {it.name.substring(0,3)}

The result of grouping is a map, where the key is the first 3 characters and the value is the files that have these first 3 characters in the name. Example:

AAA-123.TXT
AAC-234.TXT
AAA-553.TXT
AAC-001.TXT
CCC-987.TXT

The map would look like this:

AAA: [AAA-123.TXT,AAA-553.TXT], AAC: [AAC-234.TXT,AAC-001.TXT], CCC: [CCC-987.TXT]

From this, create the folder (if it does not exist), inside the folder specified by the variable destFolder. The name of each folder is the key of the map, that is, the first three characters.

    def folder = new File(destFolder + group.key)
    if (!folder.exists()) new File(destFolder + group.key).mkdir()

Created the folder, it’s time to move the files. The value map are the files that share the same key. Thus, just retrieve them, scroll through them and, for each, perform the procedure of moving them from the current location to the destination folder, destFolder.

    def files = group.value

    files.each {
        it.renameTo(folder.absolutePath + "/" + it.name)
    }

Heed

This code has been tested on Linux and works. Test it with other files before you run it with your production files. Can’t be too careful.

  • And if I want the key to the grouping to be the characters before a hyphen (-), including spaces, rather than the first 3 characters?

  • folders.listFiles(). groupBy {it.name.substring(0,it.name.indexof("-"))}. Note, however, that this will only work if the "-" character exists in the file name. If it does not exist, it will not run. If you have a very standardized scenario, this code, the way it is, will help. If there is a heterogeneous scenario, you need to modify it. Certainly, nothing complicated, mainly because it comes to Groovy.

  • Could you explain this line of code? def folders = new File("/home/Cantoni/Documents/Test")

  • In this line, I am instantiating an object of type File, passing as parameter a folder of my computer, the object File (its reference in reality), is being assigned to the variable folders. In your case (Windows), it would be something like: def folders = new File("C: Users Thiago Pastax").

  • What a difference for destFolder?

  • Folder is the source folder, where the files you want to separate are. destFolder is the variable that stores the destination folder.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.