2016-04-19 165 views
-2

我在這個路徑中有x.exe:C:\inetpub\wwwroot\webclient\db\nucleotide我想執行這個命令:blastdbcmd -db nr -entry all -outfmt "%g %T"如何從php代碼執行win cmd?

在Windows命令行我做的:

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

我怎樣才能做到這一點從PHP代碼。我知道exec($cmd)shell_exec($cmd)會做到這一點,但是,如何在$cmd中輸入以上兩條語句?

編輯1: 如何將命令的輸出保存在文本文件中? 我嘗試3聲明:

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"); 

但是,NewSeq_ $ OldDatabaseFile $ NewDatabaseFile.txt不會產生。看來這個命令沒有執行!

我該怎麼辦?

謝謝。

+0

你嘗試['$ CMD = ...'](http://php.net /manual/en/language.variables.basics.php) –

+0

@Gerald Schneider cmd第一次= cd .....第二次=命令本身,如何執行兩者? – sara

+0

看看'chdir()'[在手冊中](http://php.net/manual/en/function.chdir.php)然後運行第二行 – RiggsFolly

回答

0

三個選項:

你可以在前面加上路徑中的.exe:

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

如果你因爲某些原因.EXE真的需要你從該目錄中運行它,變成它chdir()然後再執行它。

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

或者你可以改變進入目錄,並在單個行運行命令:

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

請看看編輯1 – sara

+0

@sara you編輯你的問題太多,答案變得無效。不要這樣做,而是要針對您的新問題提出一個新問題。 –

+0

我把你的答案標記爲正確的答案,但似乎不起作用!也許我測試它錯了。但真的不起作用。我的編輯與我的問題相同。我需要知道如何從php執行cmd並將輸出放在文本文件中。 – sara