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
文字列が一致しても、実際にはファイルをデスクトップに書き込みません。
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
文字列が一致しても、実際にはファイルをデスクトップに書き込みません。
あなたが必要です
setlocal enabledelayedexpansion
バッチファイルの先頭にあり、代わりに
'%Result%'=='this is where the word string shows up'
あなたが必要
'!Result!'=='this is where the word string shows up'
-注意してください!それ以外の %。それ以外の場合、バッチファイルが最初に解析されるときに%Result%が展開され、その時点でResult変数には何も含まれません。これらの変更は、forループ内になるまで解析を遅らせることを意味します。その時点で、適切に入力されます。
は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
)
コードで setlocal enableddelayedexpansion を使用してみてください。次に、「!variable!」を使用して変数にアクセスします。「%変数%」の代わりに。また、%%A が必要なトークンを取得しているかどうかも確認してください。