Solved

move sequential files into folders

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MySQL Web Database 5 107
perl script 4 94
compress files in RAR using perl 13 75
Vb script to unzip a files and rename the files 5 54
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now