sara sara - 7 months ago 18
PHP Question

how to execute win cmd from php code?

I have x.exe in this path:

C:\inetpub\wwwroot\webclient\db\nucleotide
and I want to execute this command:
blastdbcmd -db nr -entry all -outfmt "%g %T"
.

In Windows command line I do:

cd C:\inetpub\wwwroot\webclient\db\nucleotide
blastdbcmd -db nr -entry all -outfmt "%g %T"


How can I do this from php code. I know
exec($cmd)
or
shell_exec($cmd)
will do it but, how to type the above two statement in
$cmd
?

Edit1:
How to save the output of the command in a text file?
I try 3 statement:

chdir("C:\\inetpub\\wwwroot\\webclient\\db\\nucleotide");
$cmd = "blastdbcmd.exe -db nr -entry all -outfmt "%g %T" -out $text_files_path/NewSeq_$OldDatabaseFile$NewDatabaseFile.txt";
exec($cmd);


,,

exec("C:\inetpub\wwwroot\webclient\db\nucleotide\blastdbcmd.exe -db nr -entry all -outfmt "%g %T" -out $text_files_path/NewSeq_$OldDatabaseFile$NewDatabaseFile.txt");


,,

exec("cd C:\inetpub\wwwroot\webclient\db\nucleotide && blastdbcmd.exe -db nr -entry all -outfmt \"%g %T\" -out $text_files_path/NewSeq_$OldDatabaseFile$NewDatabaseFile.txt");


but, NewSeq_$OldDatabaseFile$NewDatabaseFile.txt not generated. It seems that the command not executed!

What can I do?

Thanks.

Answer

Three options:

You can prepend the .exe with the path:

exec("C:\inetpub\wwwroot\webclient\db\nucleotide\blastdbcmd.exe -db nr -entry all -outfmt \"%g %T\"");
// don't forget to escape the quotes.

If your .exe for some reason really requires you to run it from that directory, change into it with chdir() before you execute it.

chdir("C:\inetpub\wwwroot\webclient\db\nucleotide");
exec("blastdbcmd.exe -db nr -entry all -outfmt \"%g %T\"");

Alternatively you can change into the directory and run the command in a single line:

exec("cd C:\inetpub\wwwroot\webclient\db\nucleotide && blastdbcmd.exe -db nr -entry all -outfmt \"%g %T\"");
Comments