Home » Questions » Computers [ Ask a new question ]

Program to group files from one large folder into several smaller ones?

Program to group files from one large folder into several smaller ones?

We have a system here that recently had a hard drive failure. I was able to use recovery software to get most of the files off of it, but it was not able to retrieve folder names for the most part. Instead, it dumped most everything (some 200,000 files!) into one folder. This is death for the Windows file system.

Asked by: Guest | Views: 413
Total answers/comments: 1
Guest [Entry]

"Here's a completely untested :) perl script to sort based on first char in the filename

use File::Copy;

# base directory of where we want to copy files
my $destdir = ""destdir"";

opendir(""dir_to_sort"", DH);
my @files = readdir(DH);
closedir(DH);
foreach my $file (@files)
{
# skip . and ..
next if $file =~ /^\.$/;
next if $file =~ /^\.\.$/;

# This is where you'd figure out where you want to put the file
# in this example we're just looking at the first char.
# so a file named ""HelloWorld"" would be copied to $destdir/H/HelloWorld
# pull the first char
$file =~ /^(.).*/;
my $target_dir = $1;

mkdir(""$destdir/$target_dir"") unless -d ""$destdir/$target_dir""

# you could use move instead of copy here
copy($file, ""$destdir/$target_dir/$file"");
}

Really, this is totally untested, if you lose everything by running this, don't complain I didn't warn you. :)"