2016-03-29 20 views
1

これはなぜ機能しないのでしょうか?私は、特定の文字列(123456)のカレントディレクトリにすべてのバッチファイルをスキャンするファイルを作成しようとしています。ファイルが見つからない場合、ファイルはスキャンされたファイルに自身をコピーし、次のファイルのスキャンを続行する必要があります。どんなアイデアやヒントもありがとうございます!乾杯!複数のコマンドを含むループのバッチ

for %%f in (*.bat) do (
     set A=%%f 
     set file=%A% 
     findstr "123456" %file% 
     if %errorlevel%==0 goto end 
     copy %0 %A% 
     ) 
    :end 

私は、次のコードをテストした:

SETLOCAL EnableExtensions EnableDelayedExpansion 
    for %%f in (*.bat) do (
     set A=%%~f 
     set file=%A% 
     findstr "123456" %file% 
     if %errorlevel%==0 goto end 
     copy %0 %A% 
     ) 
    :end 

を、コードのdidntは後藤終了コマンドを実行します。出力は次のようになります。

C:\Users\Epidex98\Desktop\routine>(
    set A=ir.bat 
    set file= 
     findstr "123456" 
    if 0 == 0 goto end 
    copy "C:\Users\Epidex98\Desktop\routine\ir.bat" 
    ) 
+1

保持しているフォルダにこれをテストすることを忘れないでくださいあなたが必要とする他のスクリプトの唯一のコピーです。 – LinuxDisciple

答えて

3
SETLOCAL EnableExtensions EnableDelayedExpansion 
for %%f in (*.bat) do (
    set A=%%~f 
    set file=!A! 
    findstr "123456" !file! 
    if !errorlevel!==0 goto :end 
    copy %0 !A! 
    ) 
:end 

または単純

SETLOCAL EnableExtensions EnableDelayedExpansion 
for %%f in (*.bat) do (
    findstr "123456" "%%~f" 
    if !errorlevel!==0 goto :end 
    copy %0 "%%~f" 
    ) 
:end 

しかし、あなたには、いくつかの理由で遅延拡張を適用できない場合:

SETLOCAL EnableExtensions DisableDelayedExpansion 
for %%f in (*.bat) do (
    set A=%%f 
    call :proc 
    if errorlevel 0 goto :end 
    copy %0 %%f 
    ) 
rem next command skips :proc subroutine 
goto :end 

:proc 
    set file=%A% 
    findstr "123456" %file% 
    set /A myerror=%errorlevel%-1 
exit /B %myerror% 

:end 

リソースを(必要な読書):

関連する問題