CMD commands in compiler and in system()

Jun 20, 2013 at 8:15am
Hello
what I am doing here is trying to make a simple program that fixes the damage caused by hidden files virus if you know it, it simply change the attributes from system & hidden files to normal files,
I have managed to do that with CMD, and I have fixed my flash, but I thought it will be better to make it as a program and submit it to the public so that they use it too.

I have only one problem, I need to insert the char drive to system() but I don't know how
1
2
3
    char drive;
    cin>>drive;//lets say that user entered G
        system("drive:")//Need help here <<<<<<<<<<<<<<<<<<<<<<<< 
i want it here to enter G,plus :
How can I use a variable inside system() function?
I have event tried this:
1
2
        system(drive":");//Need help here <<<<<<<<<<<<<<<<<<<<<<<<
        system("attrib -s -h /s /d *.*");

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  #include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    cout<<"\t\tThis program is free ware\n";
    cout<<"\t\tMade by Yousef Al-Hadhrami\nThis prgoram retrives hidden folders in flash drive caused by viruses\n\n";

    cout<<"Enter flash drive(\"Example : F\") :- ";
    char drive;
    cin>>drive;
    cout<<"You have entered drive "<<drive<<":";
    cout<<"\nStart processing? y/n : ";
    char start;
    cin>>start;
    if(start=='y'||start=='Y')
    {
        cout<<"Please wait, This may take long time (according to folders in your flash)... ... ...";
        system("drive:");//Need help here <<<<<<<<<<<<<<<<<<<<<<<<
        system("attrib -s -h /s /d *.*");

    }
    cout<<"Your files should be back now :) \n";
    system("pause");

}


Here is the CMD method source :
http://ezelovesse7en.blogspot.com/2012/01/tips-unhide-hidden-files-caused-by.html

and one more thing, I think using system() is legit here because I need it to do the job done ^_^, although system() is evil
Last edited on Jun 20, 2013 at 8:30am
Jun 20, 2013 at 9:26am
> although system() is evil

Who told you that std::system() is evil?
Don't you realize that it would have been removed (or at least deprecated) if it actually was evil?

It can hit you if you use it carelessly; more or less in the same way that using fork()/exec()/CreateProcess or using a shared library (.so/.dll) could hit you. Or for that matter, trying to compile or run your code from within an IDE could hit you.

If you have very strong feelings about it, say something like: 'std::system() is considered harmful'.
Saying: 'std::system() is evil' just makes you look ignorant.

Re: the question; you could do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <cstdlib>

int main()
{
    std::string cmd = "attrib -s -h /s /d " ;
    std::string path = ":\\*.*" ;
    char drive = 'G' ;

    std::string cmdline = cmd + drive + path ;

    std::cout << "execcuting >" << cmdline << '\n' ;
    int result = std::system( cmdline.c_str() ) ;
    std::cout << "result: " << result << '\n' ;
}

Jun 20, 2013 at 9:34am
I try to make my system() commands clear and limited because the following facts in a thread , here is the system() evil thread
http://www.cplusplus.com/forum/articles/11153/
and its true I am ignorant at the moment, that why I post in beginners forums, thanks for the tip

and thanks for the code, I compiled it it was great and simple although I did not get system( cmdline.c_str() ) part, I will read more about <string> header
Last edited on Jun 20, 2013 at 9:36am
Jun 20, 2013 at 9:50am
Jun 20, 2013 at 9:52am
> here is the system() evil thread

What is said inside that thread is essentially correct; though it has a poor title.

The sensible guideline is: 'Do not call system() if you do not need a command processor'.
https://www.securecoding.cert.org/confluence/display/seccode/ENV04-C.+Do+not+call+system()+if+you+do+not+need+a+command+processor

along with: 'Sanitize the environment when invoking external programs'
https://www.securecoding.cert.org/confluence/display/seccode/ENV03-C.+Sanitize+the+environment+when+invoking+external+programs
Jun 20, 2013 at 12:18pm
I see, thanks.
Topic archived. No new replies allowed.