473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Check whether entity is directory/file

I'm trying to write a script that will look into a directory, and check each entity to see if it is a directory or a file. (Borland CodeGear RAD Studio)
Here's what I have so far.

Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include <iostream.h>
  3. #include <string>
  4. #include <dir.h>
  5. #include <dirent.h>
  6. #include <errno.h>
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9.  
  10. #include <vcl.h>
  11. #pragma hdrstop
  12.  
  13. //---------------------------------------------------------------------------
  14.  
  15. #pragma argsused
  16.  
  17.  
  18. bool is_dir(const char path[])
  19. {
  20.     DIR * dir_pointer;
  21.     dir_pointer = opendir(path);
  22.     dirent * testptr;
  23.  
  24.  
  25.     if (!dir_pointer)
  26.     {
  27.       closedir(dir_pointer);
  28.       return false;
  29.     } else
  30.         {
  31.                 closedir(dir_pointer);
  32.                 return true;
  33.         }
  34.  
  35. }
  36.  
  37.  
  38. int main(int argc, char* argv[])
  39. {
  40.     DIR * directory_pointer;
  41.     dirent * file_pointer;
  42.  
  43.     if (argc > 1 && argc < 3)
  44.     {
  45.         directory_pointer = opendir(argv[1]);
  46.         if (!directory_pointer)
  47.         {
  48.          cout << "Failure[1]...terminating...";
  49.                  exit(1);
  50.         }
  51.  
  52.  
  53.             errno = 0;
  54.         while (file_pointer = readdir(directory_pointer))
  55.             {
  56.              /*    if (errno)
  57.                 {
  58.                  cout << "\nFailure[2]...terminating...";
  59.                  exit(1);
  60.                 }    */
  61.  
  62.                 if (strcmp(file_pointer->d_name,".") != 0
  63.                     && strcmp(file_pointer->d_name,"..") != 0)
  64.                 {
  65.                  cout << "\n" << file_pointer->d_name;
  66.  
  67.                  if (is_dir(file_pointer->d_name))
  68.                     {
  69.                      cout << "\t\t<DIRECTORY>";
  70.                     } else cout << "\t\t<FILE>";
  71.                 }
  72.  
  73.             }
  74.  
  75.         closedir(directory_pointer);
  76.  
  77.     } else cout << "\nCorrect usage:\n\ndirte <folder> | <PATH>\n\n";
  78.     return 0;
And previous to this the is_dir function used stat(), like this:

Expand|Select|Wrap|Line Numbers
  1. /*
  2.     struct stat holder;
  3.     stat(path, &holder);
  4.     cout << "\n" << path;
  5.     if (S_ISDIR(holder.st_mode))
  6.     {
  7.       return true;
  8.     } else return false;
  9.     */
But no matter which way I try, the function will wither return all false or all true, depending on something I am unaware of. Does the problem lie somewhere in how I pass the char array? I note that when I pass argv[1] to opendir, and that arg is not a directory, it will fail and quit on my first error message. By that logic, the is_dir function should work as well. I'm stumped.

Thank you.
Dec 6 '07 #1
0 1601

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Vincent Lefevre | last post by:
I would like to know if the base URI considered to resolve an unparsed entity defined by a relative URI should be the URI before or after its rewriting due to a possible catalog. Let's take an...
2
by: Ed Dennison | last post by:
I'm starting to look at DocBook-XML (not SGML) for producing a large documentation set. The hierarchy of DocBook elements for organizing the content is (more or less); set book part chapter...
6
by: Thomas Sommer | last post by:
Hi, I know doing something like this <!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" > <section><title/>...
1
by: Praveen | last post by:
Hi All, I'm new to DB2, I have installed DB2 UDB V8 on Solaris machine. And i tried to create a stored proc.But, gives the following error.. -- LOG FILE P2093405.log FOR PROCEDURE...
10
by: Raymond | last post by:
Hi All: To find a file exists using the file name, I have tow routings on UNIX system. 1. access(2) 2. lstat(2) This tow function also can do. When the return value is "-1" and errno is...
2
by: Ponnurangam | last post by:
Hi, I need to check whether a file named flower exists with different extensions. I know only file name. I don't the the extension(may be .bmp, .ipg, or ..gif). And if there are files with...
4
by: Stephen | last post by:
Hi all, Suppose I am monitoring a Folder C:\Temp and I drop either a file or directory into this folder How will i programmatically know whether the "item" dropped is a file or a folder ...
5
by: sword | last post by:
How can I check whether a file exists?
14
by: Martin Wells | last post by:
When I have errors in a program, whether they be compiler errors, linker errors, or if the program crashes when running, I have a list of things I check for initially. If I get an error for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.