1
#include<stdio.h>

main()
{
    char path[]="\"set path=C:\\Program Files\\WinRAR\""; //the extension I want to path
    system(path);                                         //the command
    system("UnRAR x filename.rar");                       
}

OUTPUT

'UnRAR' is not recognized as an internal or external command,
operable program or batch file.

it works when i try it myself in cmd

bayover
  • 21
  • 4
  • 1
    Does this answer your question? [run 2 or more cmd command using system() in C++](https://stackoverflow.com/questions/26401051/run-2-or-more-cmd-command-using-system-in-c) – Davis Herring May 31 '21 at 23:41
  • *at least* specify the operating system you are working on (probaly DOS-based) . The OS is not part of the C standard. – wildplasser May 31 '21 at 23:55
  • why don't use `system("\"C:\\Program Files\\WinRAR\\UnRAR\" x filename.rar");` to avoid setting PATH? – phuclv Jun 01 '21 at 02:50

1 Answers1

1

Every call to system() starts a fresh shell, and so things like environment variables don't carry over from one to the next.

You need to include both commands in one system() call. According to How do I run two commands in one line in Windows CMD? it looks like you can separate them by &, but I haven't tested this.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • I tried to solve the problem with the solution that you give in the first sentence, but it couldn't solve but ı could figure out and solve thanks to the link that you gave thanks mate! – bayover Jun 01 '21 at 17:25