-
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
-
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
-
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...
-
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
-
{
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);
-
-
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)
-
Well, a command line box flashed open and closed and the call returned true, but what did it do?
:confused:
-
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);
-
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.
-
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
-
I think that the delete function might be delt not del, not sure why though.