Solved

move sequential files into folders

Posted on 2014-04-12
1
502 Views
Last Modified: 2014-04-13
Is there a quick way to move a large number of files into sub-folders? I have a directory filled with a larger number of files (tens of thousands). I would like to move them into sub-directories, evenly, and in order. I could use a round robin sort of shuffle, but that would not keep them in order.

For example, if there were only 100 files total folder "1" would have files 1-10, folder "2" would have 11-20, etc. It is important that the folders have as close to even counts as possible. And, I don't always know how many files I will be dealing with.

Is this a fairly easy one liner type of fix, or is this a more detailed script type problem. I am hoping someone knows how to dole them out, assuming that the directories (1-10) are already created.

Any thoughts?
0
Comment
Question by:stakor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 1

Accepted Solution

by:
A-p-u earned 500 total points
ID: 39996926
Puts an equal number of files in each destination directory. If the number of files doesn't go into the number of destination directories evenly, any extra files are left in the source directory.

#!/usr/bin/perl

###########################################################################

$sourcedirectory = '/path/to/source/files';
$destinationdirectory = '/path/to/destination/directories';
$numdestdirectories = 10;  # number of destination directories, named 1 to $numdestdirectories

###########################################################################

my $numfiles = files_in_dir($sourcedirectory);
my @listfiles = sort(files_in_dir($sourcedirectory));

my $filesperdir = int($numfiles / $numdestdirectories);
print "Number of files in $sourcedirectory: $numfiles\n";
print "Number of files in each destination: $filesperdir\n";

my $nextdir = 1;
while ($nextdir <= $numdestdirectories)
{
  my $count = 0;
  while ($count < $filesperdir)
  {
    my $filenum =
	$count + ($nextdir * $filesperdir) - $filesperdir;
    rename "$sourcedirectory/$listfiles[$filenum]",
	"$destinationdirectory/$nextdir/$listfiles[$filenum]";
    $count++;
    if ($count == $filesperdir) { $nextdir++; }
  }
}

###########################################################################

sub files_in_dir
{
  # Source: http://www.perlmonks.org/?node_id=606862
  my $dir = $_[0];
  opendir my $dh, $dir or die "opendir '$dir' - $!";
  grep { -f } readdir $dh;
}

###########################################################################

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question