Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

move sequential files into folders

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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 …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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

877 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