abraxas334
October 19th, 2010, 10:00 PM
I need to check if a directory tree exists.
Essentially i want to check top directory if it doesn't exist create it. Then check if first subdirectory exists if it doesn't create it etc.
I wrote a little test program to try this out. I can verify the existance of the top directory as well as creating it. But if i want to check the subdirectory I fail. I use chdir() how can i get a right answer for my subdirectory.
I created a directory called test, which has a subdirectory subdir using mkdir on the commandline. Here my program trying to check if subdir actually exists:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string n = "test";
if(chdir(n.c_str())==0)
{
cout<<"This directory exists "<<endl;
}
else
{
cout<<"This does not exist"<<endl;
}
string n1 = "test/subdir";
if(chdir(n1.c_str())==0)
{
cout<<"Sub dir also exisits "<<endl;
}
else
{
cout<<"Pustekuchen"<<endl;
}
}
Essentially i want to check top directory if it doesn't exist create it. Then check if first subdirectory exists if it doesn't create it etc.
I wrote a little test program to try this out. I can verify the existance of the top directory as well as creating it. But if i want to check the subdirectory I fail. I use chdir() how can i get a right answer for my subdirectory.
I created a directory called test, which has a subdirectory subdir using mkdir on the commandline. Here my program trying to check if subdir actually exists:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string n = "test";
if(chdir(n.c_str())==0)
{
cout<<"This directory exists "<<endl;
}
else
{
cout<<"This does not exist"<<endl;
}
string n1 = "test/subdir";
if(chdir(n1.c_str())==0)
{
cout<<"Sub dir also exisits "<<endl;
}
else
{
cout<<"Pustekuchen"<<endl;
}
}