2013-02-25 148 views
0
FOR /F "tokens=*" %%A IN ('gpresult /r ^| FIND "string"') DO SET Result=%%A 
if '%Result%'=='this is where the word string shows up' 
echo Success > %homepath%\Desktop\Success.txt 

即使字符串匹配,也不實際將文件寫入桌面。批處理腳本:IF同步沒有按預期工作

回答

0

你需要

​​

在您的批處理文件的頂部,隨後的替代

'%Result%'=='this is where the word string shows up' 

你需要

'!Result!'=='this is where the word string shows up' 

- 注意!代替 %。否則,當批處理文件第一次被解析時,Result Result%被展開,此時Result變量不包含任何內容。這些更改意味着它會延遲解析它,直到它在for循環內,這一點將適當填充。

0

嘗試在代碼中使用setlocal enabledelayedexpansion。然後使用「!變量!」訪問您的變量而不是「%variable%」。 還要確保%% A是否獲取所需的令牌。

0

echo應在同一行if

if '%Result%'=='this is where the word string shows up' echo Success > %homepath%\Desktop\Success.txt 

或放在括號周圍:

if '%Result%'=='this is where the word string shows up' (
    echo Success > %homepath%\Desktop\Success.txt 
)