2015-05-08 49 views
-1

假設一個變量的值爲'hello world'根據變量的內容執行不同的命令

我想使它變得無關緊要,如果它有一個特定的詞,例如hello,它會執行某個命令。

如果它不包含單詞hello它執行一個不同的命令。

+1

在http://ss64.com/nt/syntax-replace.html – JosefZ

回答

1
echo %path% | findstr /i /c:"hello" && Echo Success || Echo Failure 

從MSDos 6.22幫助文件。

FIND退出代碼

以下列表顯示了每個退出代碼的其 含義簡要說明:

0搜索已成功完成,並且至少一個匹配的結果。

1搜索已成功完成,但未找到匹配項。

2搜索未成功完成。在這種情況下,在搜索過程中發生了錯誤 ,FIND無法報告是否找到任何匹配的 。

您可以在批處理 程序的命令行上使用ERRORLEVEL參數來處理由FIND返回的退出代碼。

關於CMD的特殊字符。

& seperates commands on a line. 

&& executes this command only if previous command's errorlevel is 0. 

|| (not used above) executes this command only if previous command's errorlevel is NOT 0 

> output to a file 

>> append output to a file 

< input from a file 

| output of one command into the input of another command 

^ escapes any of the above, including itself, if needed to be passed to a program 

" parameters with spaces must be enclosed in quotes 

+ used with copy to concatinate files. E.G. copy file1+file2 newfile 

, used with copy to indicate missing parameters. This updates the files modified date. E.G. copy /b file1,, 

%variablename% a inbuilt or user set environmental variable 

!variablename! a user set environmental variable expanded at execution time, turned with SelLocal EnableDelayedExpansion command 

%<number> (%1) the nth command line parameter passed to a batch file. %0 is the batchfile's name. 

%* (%*) the entire command line. 

%<a letter> or %%<a letter> (%A or %%A) the variable in a for loop. Single % sign at command prompt and double % sign in a batch file. 
+0

閱讀爲例_Boolean測試「確實存在串?」 _如果你不想FINDSTR打印它找到的字符串屏幕,當它實際發現的東西,你應該重定向輸出到NUL。例如,「findstr/i/c:」System32「> NUL」 – indiv