3

ここで問題があります。まず最初に、コード:

test.bat の内容:

@echo off
cls
for /F "delims=" %%a in ('dir /B /A-D ^| findstr /I ".txt$"') do (
set str=%%a
echo %str% >> list.tmp
pause
)

echo ------------------
for /F %%i in (list.tmp) do echo %%i
del list.tmp
echo ------------------

test.bat と同じディレクトリに、1.txt と 2.txt の 2 つのテスト ファイルがあります。

test.bat を実行すると、出力は次のようになります。

------------------
2.txt
2.txt
------------------

ご覧のとおり、1.txt はリストされていません。

3.txt を追加すると、出力は次のようになります。

------------------
3.txt
3.txt
3.txt
------------------

誰でも私を助けてもらえますか??

ありがとう、アンドリュー・ウォン

4

2 に答える 2

5

ループ内では変数を読み取っていて、その変数もそのループで変更されるため、遅延拡張機能を使用する必要があります。FOR

@echo off
setlocal enabledelayedexpansion
cls
for /F "delims=" %%a in ('dir /B /A-D ^| findstr /I ".txt$"') do (
  set str=%%a
  echo !str! >> list.tmp
  pause
)

echo ------------------
for /F %%i in (list.tmp) do echo %%i
del list.tmp
echo ------------------
于 2012-10-28T07:43:45.840 に答える
3

関数(subRoutine)を使用することもできます...それはまた、ループごとに評価するようにCMDを「強制」します...

@echo off
cls
for /F "delims=" %%a in ('dir /B /A-D ^| findstr /I ".txt$"') do (
  call :doOne %%a 
)

echo ------------------
for /F %%i in (list.tmp) do echo %%i
del list.tmp
echo ------------------
goto :EOF

:DoOne
  set str=%1
  echo %str% >> list.tmp
  pause
于 2012-10-28T08:33:36.237 に答える