0

バッチスクリプトに次の行があります

for %%a in (*.rmt) do (findstr /C:"        model='" %%a)>tmp.par

空のフォルダーでこれを実行すると、エラーレベルはまだ 0 です。

ただし、*.rmt をフォルダーに存在しないファイル名、たとえば x.rmt に置き換えると、エラーレベルは 1 になります。

理想的には、フォルダーに RMT ファイルがない場合、errorlevel!=0 にするべきではありませんか?

フォルダーには 0 から複数の RMT ファイルが存在する可能性があるため、この For ループが *.rmt で機能する必要があります。助けてください。

ありがとう。

注: 文字列 " model='" が 1 つの RMT ファイルに存在する場合、そのフォルダー内の他のすべての RMT ファイル (存在する場合) に強制的に存在します。

4

3 に答える 3

0

これを試して:

@echo off
for /F "delims=" %%i in ('dir /b "path_to_dir\*.rmt"') do (
   :: echo Folder is NON empty
   findstr /C:"model='" %%i >> C:\testlog.log
   goto :EOF
)
于 2014-03-12T11:05:01.187 に答える
0

No, the FOR command never sets the ERRORLEVEL <> 0 if there are no iterations.

Yes, the following command reports ERRORLEVEL=1:

for %%a in (notExists.rmt) do (findstr /C:"        model='" %%a)>tmp.par

But that is because the simple FOR simply lists the string(s) within the IN() clause if they do not contain wildcards. It doesn't bother checking to see if the file exists. So your FINDSTR command is actually raising the error because it cannot find the file, not the FOR statement.

Your command is flawed in that each iteration overwrites the previous tmp.par. That can be easily fixed by adding an extra level of parentheses. This also will create an empty tmp.par if no files were found or if none of the files contained the search string. The ERRORLEVEL cannot be relied upon because its value will not have been set if no files were found, or it may be 0 or 1 depending on if the last file contained the search string.

(for %%a in (*.rmt) do (findstr /C:"        model='" %%a))>tmp.par

If you don't mind having a filename: prefix on each line of output, then you can simplify your code to:

findstr /C:"        model='" *.rmt >tmp.par 2>nul

This also will create an empty tmp.par file if no files were found, or if none of the files contain the search string. But now the ERRORLEVEL will be reliable. The ERRORLEVEL is 1 if no files are found or if no files contain the search string. Otherwise the ERRORLEVEL will be 0.

于 2014-03-12T12:09:12.037 に答える
0

findstrに一致するものがない場合、 は実行されないため*.rmterrorlevelは変更されません。

を使用するx.rmtと、FOR動作が変わります。一致するファイル名を探すのではなく、特定の文字列を調べます。これは、ファイル名である場合とそうでない場合があり、存在する場合と存在しない場合があります。

errorlevelの前に意図的に設定できます。for

@ECHO OFF
SETLOCAL
ECHO y|FIND "x">nul
for %%a in (*.rmt) do (findstr /C:"        model='" %%a)
ECHO errorlevel=%errorlevel%
GOTO :EOF

errorlevel一致が見つからない限り、1を返します。

于 2014-03-12T12:16:41.717 に答える