|
Boost Users : |
Subject: [Boost-users] boost::filesystem directory_iterator: how to get the last file of a directory ?
From: Vial, Florent (fvial_at_[hidden])
Date: 2010-03-12 06:54:33
Hi boost-users,
I am using a recursvive function for iterating over a starting path.
When i finally arrive in a directory that contains no subdirectory
(what i call a "final" directory), i want to retrieve
the first and the last file of this directory.
I succeed in retrieving the first file of the final directory (see code
snippet below),
But i do not know how to retireve the last one using the same directory
iterator.
Can someone think of an elegant solution ?
------------------ CODE BEGIN -------
namespace fs = boost::filesystem;
void my_function (const fs::path &ph)
{
bool firstFileOfFinalDir = false;
if (fs::is_directory(ph))
{
fs::directory_iterator end_iter;
for (fs::directory_iterator dir_itr(ph); dir_itr !=
end_iter; ++dir_itr)
{
if (fs::is_directory(dir_itr->status()))
{
// Recursive call
my_function(fs::path(dir_itr->path()));
}
else
{
// We are in a "final" directory
if (fs::is_regular_file( dir_itr->status()))
{
if (firstFileOfFinalDir)
{
// Do something with the first
file of the final directory
firstFileOfFinalDir = false;
}
else
{
// My problem : how to check if
// dir_itr == (end_iter - 1)
// i.e. if it is the last valid value ?
}
}
}
}
}
}
------------------ CODE END -------
Thank you in advance for your help,
Florent
Be a part of filmmaking's exciting future today by registering
your intent to purchase on: www.arridigital.com
Arnold & Richter Cine Technik GmbH & Co. Betriebs KG
Sitz: Munchen - Registergericht: Amtsgericht Munchen - Handelsregisternummer: HRA 57918
Personlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH
Sitz: Munchen - Registergericht: Amtsgericht Munchen - Handelsregisternummer: HRB 54477
Geschaftsfuhrer: Franz Kraus; Dr. Martin Prillmann
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net