2017-07-13 22 views
1

私の目標は、特定の行の下に文字列を挿入できるスクリプトを作成することです。これは私がやっていることです:テキストファイルに行を挿入するバッチ

SETLOCAL ENABLEDELAYEDEXPANSION 


set outputFile=C:\Utilisateurs\a669884\Documents\script22.txt 
set "_strInsert=import java.interceptor.ReportInterceptor;" 
set "_strFind=import javax.interceptor.Interceptors;" 


:Ask 
echo Are you in the good folder (wlp-cas-provider)?(Y/N) 
set INPUT= 
set /P INPUT=Type input: %=% 
If /I "%INPUT%"=="y" goto yes 
If /I "%INPUT%"=="n" goto no 
echo Incorrect input & goto Ask 

:yes 

for %%i in (*.java) DO (
FOR /F "usebackq delims=" %%A IN ("%%i") DO (
    Echo %%A | Find "%_strFind%" && ECHO %%A>>"%outputFile%" && ECHO %_strInsert%>>"%outputFile%" 
    IF [!errorlevel!] == [1] ECHO %%A>>"%outputFile%" 
) 
MOVE /Y "%outputFile%" "%%i" && DEL /F /Q "%outputFile%" 
) 

:no 
echo Oh no! Go to the right folder and comeback 
cls 
pause 

主なアイデアは、一時ファイルに変更されたテキストをコピーして、元のファイルに貼り付けすることです。その後、一時ファイルは削除されます。

コードは1つのファイルでのみ完了していれば完全に動作しています。 forを使用すると、フォルダ内のファイル.javaすべてでそれを行うことができます。これはもう機能しません。テンポラリファイルscript22.txtはもう削除されず、その中にテキストが重なり合っています。

+0

あなたのコードは少し複雑ですが、ループが失敗する論理エラーは見つかりませんが、パイプが予期しない動作を引き起こしている可能性があります。とにかく、追加する前に一時ファイルを削除しておくべきですが、 'move'コマンドの後に削除する必要はありません。なぜなら、もはやそこには存在しないはずです... – aschipfl

答えて

0

どのようにこれらの線に沿って何かについて:ループが失敗すべき理由(未テスト)

@ECHO OFF 
FOR %%A IN ("%CD%") DO IF /I NOT "%%~nxA"=="wlp-cas-provider" (
    ECHO=Please change directory to wlp-cas-provider 
    ECHO= before running this script again. 
    ECHO= 
    ECHO=Press any key to exit ... 
    PAUSE>NUL 
    GOTO :EOF) 

SET "_strFind=import javax.interceptor.Interceptors;" 
SET "_strInsert=import java.interceptor.ReportInterceptor;" 

SETLOCAL ENABLEDELAYEDEXPANSION 
SET NL=^ 


SET "str2Add=%_strFind%!NL!%_strInsert%" 

FOR /F "TOKENS=1-2* DELIMS=:" %%A IN ('FINDSTR/BLINC:"%_strFind%" *.java 2^>NUL' 
) DO (SET/A "i=1+%%B" 
    FINDSTR/BLINC:"%_strInsert%" "%%A"|FINDSTR/B "!i!:">NUL 
    IF ERRORLEVEL 1 (( FOR /F "TOKENS=1* DELIMS=:" %%D IN ('FINDSTR/N $ "%%A"' 
      ) DO IF %%D EQU %%B (ECHO=!str2Add!) ELSE ECHO=%%E)>>"%%A.tmp")) 

FOR %%A IN (*.java.tmp) DO IF EXIST "%%~nA" (DEL "%%~nA" && REN "%%A" "%%~nA") 
+0

あなたの助けをありがとう、私はバッチスクリプトの初心者ですので、少し失われています。私はちょうどこれをテストし、それは正常に動作するようです。神のお恵みがありますように –

0

スクリプトには重い論理エラーはありません。私は、新しいコマンドプロンプト(cmd)インスタンスを開始するので、予期しない動作を引き起こすのがパイプ|であると思われます。左端が特別な意味を持つ文字を含む既に展開された文字列(echo %%A)を受け取ります。 cmd。ここで

は、スクリプトの可能な改善である - すべての説明の発言(rem)を参照してください。

@echo off 
setlocal DisableDelayedExpansion 

rem // Define constants here: 
set "targetFolder=C:\Utilisateurs\wlp-cas-provider" 
set "fileMask=*.java" 
set "outputFile=C:\Utilisateurs\a669884\Documents\script22.txt" 
set "_strInsert=import java.interceptor.ReportInterceptor;" 
set "_strFind=import javax.interceptor.Interceptors;" 

rem // Enumerate all matching files: 
for %%I in ("%targetFolder%\%fileMask%") do (
    rem /* Redirect the output of the whole `for /F` loop at once; 
    rem this improves the performance, and there is no appending `>>`, 
    rem so it does not matter whether the file already exists before: */ 
    > "%outputFile%" (
     rem /* Use `findstr /N` to precede every line by its line number 
     rem and a colon, so no line appears empty to `for /F` any more; 
     rem remember that `for /F` ignores empty lines otherwise: */ 
     for /F "delims=" %%A in ('findstr /N "^" "%%~I"') do (
      rem // Store the current line string with line number prefix: 
      set "line=%%A" 
      rem // Toggle delayed expansion to maintain exclamation marks: 
      setlocal EnableDelayedExpansion 
      rem // Remove the line number prefix and the colon: 
      set "line=!line:*:=!" 
      rem // Return the original line unconditionally: 
      echo(!line! 
      rem /* Explicitly initiate another child `cmd` instance on the 
      rem left side of the pipe with delayed expansion enabled; 
      rem escape the delayed expansion like `^^!` not to be 
      rem processed by the parent `cmd` instance, so the child 
      rem `cmd` instance receives the variable `!line!` only; 
      rem therefore strings potentially containing special 
      rem characters are expanded at the very latest point: */ 
      (cmd /V /C echo(^^!line^^!| find "!_strFind!" > nul) && (
       rem /* The string to search has been found, so return the 
       rem string to insert after it at this point: */ 
       echo(!_strInsert! 
      ) 
      endlocal 
     ) 
    ) 
    rem // Move the result file over the currently iterated one: 
    move /Y "%outputFile%" "%%~I" > nul 
) 

endlocal 
exit /B 

これは、文字通りすべての文字が検索文字列に表示されることを可能にします。 findで正しく処理するには、引用符"を倍にする必要があることに注意してください。行全体が検索文字列と一致するようにするには、findコマンド行をfindstr /X /C:"!_strFind!"に置き換えます。この場合、検索文字列に引用符が含まれている場合は、\"のようにエスケープします。リテラルバックスラッシュは\\のように2倍にする必要があります。大文字小文字を区別しない検索を行う場合は、/Iオプションをfindまたはfindstrに追加します。


あなたは...パイプを含むブロックを回避できます。

  (cmd /V /C echo(^^!line^^!| find "!_strFind!" > nul) && (
       echo(!_strInsert! 
      ) 

を...あなたは文字列を比較するifステートメントを使用する場合。例えば

ラインは、検索文字列を含むと予想される場合、代わりにこれを使用する:

  if not "!line!"=="!line:*%_strFind%=!" echo(!_strInsert! 

これは常にために使用sub-string replacementアプローチの大文字と小文字を区別しない検索を行います。検索文字列に次のいずれかの文字が含まれていると、このメソッドが失敗することがあります。^,!,%,=,。

行全体が代わりにこれを使用すると、検索文字列に一致することが期待されている場合:

  if "!line!"=="!_strFind!" echo(!_strInsert! 

あなたはifステートメントに/Iスイッチを追加しない限り、これは、大文字と小文字を区別した検索を行います。

関連する問題