Thread: DOS Commands

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    7

    Question DOS Commands

    I've seen similar quetsions asked and reasonable answers given. Unfortunately, none of the answers really address the problem at hand.

    I need to write some of the dos commands in Borland C++ Builder (v3.0). del, copy, move, attrib, md and deltree. I've done it and they all work very well on individual files. The problem comes in when using wild cards. (I wasn't thinking about wildcards when I wrote them). Of all the solutions I've seen here, none take wildcards into account except for those using system(). Now the system() function is great except that every release of Windows is in flux and who knows what system commands Microsoft is going to eliminate. For instance, had I written system("deltree c:\test") in Windows 95 (I think it was still there in Windows 95) it would have workd fine, but in NT and 2000 the command is system("rd c:\test /s") (at least at the command line it would be rd c:\test /s). I want to write this to work for years to come and not fail when Microsoft decides to eliminate the copy command from the system. Has anyone written some of the dos files in c or c++ as an exercise in a class or done something similar with the same concerns I have?

    Thanks for any help.

    Dana

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Every operating system has its own commands for these sort of operations. If you want portability across all forms of windows then this is accomplishable by using the win32 api.The system function is awkward and slow and as you have seen often not portable. look into the api functions .....
    CopyFile()
    MoveFile()

    etc.

    there is info on these functions at msdn. You can use them in a console if you want.All you need is to include windows.h
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    It is awkward and slow... but you can do what you want with it by taking in desired operation via a null terminated character array, I believe. I would test that, but I am not at home.

    Your program could have an option list for the user to pick what OS they are using (or you could find out yourself if you are that savy) and their selection would determine which type of parameters are used.

    And there could be a seeming million of other ways to do this...
    Blue

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    7
    I just tried it with DeleteFile() (the easiest example) and it is not handling the wildcards.
    {
    bool ret = true;
    char MyFile["c:\\testing\\test*.*"];
    ret = DeleteFile(MyFile);
    }

    returning false and not touching c:\testing\test1.txt.



    Dana

  5. #5
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    {
    bool ret = true;
    char MyFile["c:\\testing\\test*.*"];
    ret = DeleteFile(MyFile);
    }



    try this


    bool ret = true;

    char MyFile[30] = "c:\\testing\\test*.*\0";

    ret = DeleteFile(MyFile);


    and if the wildcards are no go still...

    bool ret = true;

    char MyFile[30] = "c:\\testing\\test*.*\0";

    ret = system(MyFile);
    Last edited by Betazep; 01-04-2002 at 12:08 PM.
    Blue

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    7
    No Dice.

  7. #7
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    sorry I was editing... try the second one...


    I don't have a compiler with me... and it seems that a lot of my programming is trial and error ... esp when doing something somewhat new (i.e. handling wildcards... the rest I know)
    Blue

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    7
    Well, a command line box flashed open and closed and the call returned true, but what did it do?


  9. #9
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    It didn't do anything... now do this...

    bool ret = true;

    char MyFile[30] = "c:\\testing\\test*.*\0";

    char MyCommand[30] = "del";

    ret = system(MyCommand + MyFile);


    That may not work... You might need to strcat(). Like I said... shooting from the hip here.

    Try this too...



    bool ret = true;

    char MyFile[30] = "del c:\\testing\\test*.*\0";

    ret = system(MyFile);
    Blue

  10. #10
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    anyway... I have to split. Someone else may help you better.

    Imagine though if you concatenated the two char arrays and put those into the system() function. Then you could have multiple selections and pick them from menus or you could get it from user input etc etc etc. You can use switches with the system() call. It just has to be in a null terminated char* format. So how you get it there is entirely up to you.
    Blue

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    sorry i should have said that you need to use FindFirstFile() or FindFirstFileEx() and FindNext() to fill an array or linked list etc with the file information you want to use with the other api funcs.
    FindFirstFile and FindNextFile accept wildcards. there should be info on their use at msdn.microsoft.com
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  12. #12
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    I think that the delete function might be delt not del, not sure why though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Executing DOS commands from inside an image
    By Moony in forum C Programming
    Replies: 6
    Last Post: 03-16-2008, 12:40 PM
  2. DOS commands using C++
    By rusty0412 in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2003, 04:56 PM
  3. Dos commands hehe
    By Carp in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-17-2003, 02:51 PM
  4. DOS Commands
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 05-13-2002, 03:40 AM
  5. dos commands in c++
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-18-2001, 11:05 PM