2016-09-06 15 views
1

我試圖編寫一個批處理文件,以將特定字符串「str2 => bbb」附加到文件,如果它尚未存在於文件。 「str2」將在字符串「str1 => aaa」(它始終存在於文件中)之後。例如:通過Windows批處理文件在另一個字符串之後附加字符串

file.txt的

...

STR1 => AAA

... file.txt的的

它將變成:

file.txt的

...

...

...

STR1 => AAA

STR2 => BBB

.. 。

file.txt結尾

並且該批處理文件必須不具有破壞性,即如果該文件中已存在「str2」,該批處理將不執行任何操作。

我知道如何找到一個字符串的文件中:

FINDSTR "str2 => bbb" "file.txt" 

IF %errorlevel%==0 (
    ECHO FOUND 
) 

,但我不知道自己還能做些什麼寫在下一行另一個字符串。在批處理文件

+0

您可能需要考慮通過文本文件工作,檢查您得到的行是否正確('aaa')並將其寫入臨時文件。如果你得到的那一行是正確的,那麼你會在那之後寫出'bbb'。最後刪除舊的並重命名臨時文件。只有當字符串單獨行時纔有效,並且效率不高。 – geisterfurz007

回答

0

使用PowerShell來簡化事情

FINDSTR "str2 => bbb" "file.txt" 

    IF %errorlevel%==0 (
     ECHO FOUND 
     Goto END 
    ) 
    powershell -Command "(get-content File.txt) -replace "str1 => aaa", "$&`n str2 => bbb" | set-content File.txt" 
:end 

的PowerShell命令會得到你的文件的內容和替換搜索字符串($ &)+新線+ STR2您的字符串...)

1

由於我不清楚str2是否必須在str1之後立即出現在文件中或者在任何地方,我寫了下面的腳本,它能夠覆蓋這兩個標準。它會直接修改輸入文件,所以要小心。輸入文件必須speficied作爲命令行參數:

@echo off 
setlocal EnableExtensions DisableDelayedExpansion 

rem // Define constants here: 
set "FILE=%~1" & rem // (input file; `%~1` takes first command line argument) 
set "WORD1=str1" & rem // (word after which `%WORD2%` must be inserted) 
set "WORD2=str2" & rem // (word that must be present in the file) 
set "STRING2=%WORD2% => bbb" & rem // (full string to insert if `%WORD2%` is missing) 
set "SEPARATORS= = " & rem // (characters that separate the words from the rest) 
set "FIXEDPOS=#" & rem // (if not empty, defines that `%WORD2%` must be after `%WORD1%`) 

rem // Create line-break (carriage-return and line-feed): 
(for /F %%# in ('copy /Z "%~f0" nul') do set ^"CR+LF=%%#^ 
%= empty line =% 
^") 

rem // Ensure list of separators contains (ends) with space: 
if defined SEPARATORS (
    if not "%SEPARATORS:~-1%"==" " set "SEPARATORS=%SEPARATORS: =% " 
) else set "SEPARATORS= " 
setlocal EnableDelayedExpansion 
rem // Set up regular expression: 
if defined FIXEDPOS (
    rem /* `%WORD2%` must be in the line following `%WORD1%`, so define a dual-line 
    rem regular expression (both words must be present at the beginnings of lines): */ 
    set "REGEX=^%WORD1%[%SEPARATORS%].*!CR+LF!%WORD2%[%SEPARATORS%]" 
) else (
    rem /* Position of `%WORD2%` does not matter with respect to `%WORD1%`, 
    rem hence it merely must be present at the beginning of a line: */ 
    set "REGEX=^%WORD2%[%SEPARATORS%]" 
) 
rem // Search for regular expression in file: 
> nul findstr /I /R /C:"!REGEX!" "%FILE%" || (
    rem // No match encountered, so read entire file and deplete it afterwards: 
    for /F "delims=" %%L in ('findstr /N /R "^" "%FILE%" ^& ^> "%FILE%" break') do (
     endlocal 
     rem // Read a line, reset flag that defines whether or not to insert a string: 
     set "FLAG=" & set "LINE=%%L" 
     setlocal EnableDelayedExpansion 
     rem // Split off first word and compare with `%WORD1%`: 
     for /F "eol= tokens=1 delims=%SEPARATORS%" %%K in ("!LINE:*:=!") do (
      endlocal 
      rem // First word matches `%WORD1%`, so set flag: 
      if /I "%%K"=="%WORD1%" set "FLAG=#" 
      setlocal EnableDelayedExpansion 
     ) 
     rem // Append to file: 
     >> "%FILE%" (
      rem // Write original line: 
      echo(!LINE:*:=! 
      rem // Write string to insert in case flag is defined: 
      if defined FLAG echo(!STRING2! 
     ) 
    ) 
) 
endlocal 

endlocal 
exit /B 

注意,該腳本不檢查是否str1多次發生。

相關問題