Thread: System Command: DIR

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    116

    Question System Command: DIR

    I'm using system command DIR: this command list all folder in the present directory. But I don't know how to touch folder in this list.
    Example:
    Code:
    #include<windows.h>
    int main()
    {
    System("DIR"); //list all folder
    //Example: folder A, folder B,...
    }
    And, I want to check if there is folder X in this directory, how can ?
    Anyones who know this, please teach me.

    thanks

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Since you're asking about "DIR", I assume this is a Windows machine. Look into the FindFirstFile function.

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Try chdir, or opendir if chdir isn't available. chdir will return -1 if the specified directory doesn't exist. opendir will return NULL if the specified directory doesn't exist.

    Also, you can use DOS' FIND command to search the output of DIR for folder X.

    Code:
    C:\>DIR | FIND "folder X"
    That command should output "folder X", if folder X exists, or nothing, if it doesn't.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This shows another way to do it, without using a system call. Change it around to your own directory and file name, of course.

    If you use Pelles C, you need to first click on "Project" tab, project options, and compiler. Check the box for "Define Compatibility Names

    Code:
    /*Remember to go into project options and check the "Define compatibility names box. 
    That gets rid of the "Target Architecture not defined", error.
    */
    #include <dirent.h>    
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
       int i=0;
       DIR *dir;
       struct dirent *direntry; //could be a file, or a directory
    
       dir = opendir("C:/Users/Adak");
       if(!dir) {
          printf("Error: directory did not open!\n");
          return 1;
       }
    
       while((direntry=readdir(dir))!=NULL) {
          if(++i < 20) 
             printf("%s\n",direntry->d_name);
          if((strcmp(direntry->d_name, "test.txt"))==0) {
             printf("\nThe %s file has been found\n",direntry->d_name);
             i=-99;  //just a flag value to show the file was found
             break;
          }
    
       }
       if(i!=-99)
          printf("\nThe test.txt file was not found\n");
    
       closedir(dir);
    
       printf("\n");
       return 0;
    }

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    G'day Adak... dirent.h is not a standard C header, it may not exist on the OP's machine.

    Since the DIR command is shelled, it's very likely he's on Windows so he needs to investigate FindFirstFile(), FindNextFile() and FindClose()

    A nice trick is that if you already know the name of the file or folder, you can use FindFirstFile() as a search tool... it will return an error if the specified path is not present...
    Last edited by CommonTater; 01-04-2012 at 03:01 AM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    G'day Tater.

    Anduril had a hyperlink to the findfirstfile() way to do it - I was looking for something that used C, instead of the Windows API. That's what it sounded like the OP was looking for (and I was curious myself).

    Is there a standard C way to do this, without using either the Windows API functions, or non-standard C?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adak
    Is there a standard C way to do this, without using either the Windows API functions, or non-standard C?
    No. <dirent.h> is POSIX standard though.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Every directory in DOS/Windows has a special "file" called NUL (similar to CON and PRN devices)
    So if you try and open it, and succeed, then the directory exists.

    Code:
    fp = fopen("C:/path/to/somewhere/NUL", "r" );
    if ( fp != NULL ) {
      fclose(fp);
      // directory exists
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    G'day Tater.

    Anduril had a hyperlink to the findfirstfile() way to do it - I was looking for something that used C, instead of the Windows API. That's what it sounded like the OP was looking for (and I was curious myself).

    Is there a standard C way to do this, without using either the Windows API functions, or non-standard C?
    Quick way to check with Pelles...
    If it's listed under "Private #includes" in the help file, it's not standard C.
    Also if the function name is prefixed with an underscore it's not standard C.
    Finally.. Hit F1 on the highlighted function... each is clearly marked in the help file.

    Standard C is completely unaware of directories and folders, since each OS has it's own bright ideas about how to handle them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. System command
    By hello in forum C Programming
    Replies: 3
    Last Post: 01-09-2009, 05:28 AM
  2. the system command
    By happyclown in forum C Programming
    Replies: 7
    Last Post: 01-02-2009, 07:14 AM
  3. Help on System Command
    By Smoki in forum C++ Programming
    Replies: 5
    Last Post: 12-14-2005, 06:47 PM
  4. using system command
    By warney_out in forum C Programming
    Replies: 1
    Last Post: 09-03-2004, 05:24 AM
  5. system command
    By vk in forum C Programming
    Replies: 13
    Last Post: 04-03-2002, 07:44 PM