Solved

move sequential files into folders

Posted on 2014-04-12
1
494 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
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 …
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…

839 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