Thread: Checking if Folder/Directory exits...

  1. #1
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308

    Checking if Folder/Directory exits...

    I'm working on a project and I need to check if a folder exists or not using Turbo C++ (yeah yeah, I get it, it's an ancient IDE that is no longer supported and there are a tonne of other free compilers for Windows but I need to use Turbo for the project as that's what my school wants).

    I know how to create a folder if it doesn't exist using mkdir() function which would return 0 on success.

    Code:
    enum bool { FALSE = 0 , TRUE = 1 };
    
    
    typedef bool bool;
    
    void main (void)
    {
            int ERROR_IN_CREATING_FOLDER = mkdir( "C:\\TURBOC3\\BIN\\FOLDER" );
    
        if ( ERROR_IN_CREATING_FOLDER == TRUE )
        {
            cout<<endl<<" ERROR! FOLDER COULD NOT BE CREATED"<<endl
                <<endl<<" (Press any key to exit)";
    
    
            getch();
    
    
            exit(0);
        }
    
    
        else
        {
                    // Do the stuff I need to do
        }
    }
    As one can notice, the code works perfectly fine on the first run and creates a folder called FOLDER. But let's say I run the code again, it shows the ERROR message which in this case is not what I want. Therefore, I need something to check that if FOLDER doesn't exist already, then create it or else if it does exist than run the 'else' part of my code.

    Something of this sort:

    Code:
    if ( ERROR_IN_CREATING_FOLDER == TRUE && Does_Folder_Exist == FALSE )
    {
            // ERROR
    }
    else
    {
            // Do the stuff I need to do
    }
    I looked up for solutions on Google but none of them are viable on TURBO as they require windows.h.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps it's time for a change of school.

    Seriously, whatever hackery you learn for TurboCrap to make this work, it will be of ZERO relevance in the real world if you want to actually write programs after finishing the course.

    Does TC have an access() function by any chance?

    There's no point searching google for TC help.
    The ENTIRE API that it supports is all in a handful of header files, which you can just load into an editor and read.
    If it isn't there, you're pretty much stuck.
    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.

  3. #3
    Guest
    Guest
    I know it's not helpful but I really feel sorry that your school is putting you through this. If you practiced programming logic, it wouldn't matter so much that your toolset is outdated, but what do you gain by learning the toolset itself? You're never gonna use it in your career. It's absurd.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    opendir

    Try opendir it has been around a long time.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    Perhaps it's time for a change of school.

    Seriously, whatever hackery you learn for TurboCrap to make this work, it will be of ZERO relevance in the real world if you want to actually write programs after finishing the course.
    Trust me, I know. But I can't help it....

    Does TC have an access() function by any chance?
    Yes, it does. But isn't it for checking if a file exists? I guess I know what you're thinking but I may be wrong. Do you mean to say "when you create a folder, create a file in it too and every time you run the code, it checks if the file exists and if it does, the code continues to do what it has to but if it doesn't, show the error message"?

    There's no point searching google for TC help.
    Realised.
    Last edited by Zeus_; 08-17-2019 at 02:57 AM.

  6. #6
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    opendir

    Try opendir it has been around a long time.
    Thanks a lot @stahta01! The return value of opendir helped me do what I wanted to

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 20
    Last Post: 02-23-2010, 01:49 AM
  2. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  3. Not able to access directory of home folder
    By Bargi in forum Linux Programming
    Replies: 1
    Last Post: 02-06-2008, 02:45 AM
  4. open/search a folder when searching a directory
    By Ideswa in forum C++ Programming
    Replies: 1
    Last Post: 02-24-2006, 02:31 PM
  5. checking whether a folder is empty
    By Dag_ in forum C++ Programming
    Replies: 6
    Last Post: 03-07-2005, 07:02 PM

Tags for this Thread