1

I'm looking for a program that will let me group folders together in a folder depending on the prefix.

So if I have folders, A-B-C-1 and A-B-C-2, since they both have prefix A then they will be placed in a folder.

Is there a program for Windows or OS X that will allow this?

2 Answers 2

1

You could use the built-in PowerShell 3.0 tool in Windows (8+?). Open PowerShell 3.0 and choose a prefix filter, search path, and a destination directory. Example of a command:

Get-ChildItem -Directory -Recurse -Path "C:\Users\John\Desktop" -Filter "A-*" | foreach {Move-Item -Path $_ -Destination "C:\Users\John\Documents\Destination-folder" }

Analysis of the command

Get-ChildItem -Directory

Gets the directories in the specified location

-Recurse

Also checks sub-directories' sub-directories'... You can leave this out if you only need to check directories in one folder.

-Path "C:\Users\John\Desktop"

Specifies the path you want to search for directories in

-Filter "A-*"

Only selects directories starting with "A-" (This is your prefix). The * is a wildcard which tells PowerShell not to care about what the rest of the directory name.

|

The "pipe" character sends the output of the first command (Get-ChildItem) to the next command (for further processing)

 foreach { }

Classic for-loop that does a specified action on each item (aka each directory from Get-ChildItem)

 foreach {Move-Item -Path $_ -Destination "C:\Users\John\Documents\Destination-directory" }

Move the item "$_" (a variable that represents each item (directory) Get-ChildItem sends) to the specified destination directory.

0

You could do this quite quickly with a simple python script - assuming that you have all of the folders in a single location to start with, something like, (untested):

import os
import collections

folder = os.path.curdir  # Or somewhere else?
dirlist = [d for d in os.listdir(folder) if os.path.isdir(os.path.join(folder, d))]
outmap = collections.defaultdict(list)
for name in dirlist:
    pre = name[:1].upper() # 1 is prefix length & make case independent
    if pre.isalnum() and len(name) > len(pre):  # Skip existing and specials
        outmap[pre].append(name)  # Add to the list for that prefix

for k, dirlist in outmap.iteritems(): 
    newdir = os.path.join(folder, k)  # This could be elsewhere
    for d in dirlist:
        os.renames(os.join(folder, d), os.join(newdir, d))

The above should work nicely on OS-X, Linux & Windows but you will need to install python on Windows. Free, Gratis & Open Source.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.