Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

move sequential files into folders

Posted on 2014-04-12
1
Medium Priority
?
512 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 2000 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

Technology Partners: 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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

670 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