差不多......需要找到并替换。使用for循环

时间:2013-03-21 05:35:49

标签: batch-file cmd batch-processing

我正在努力寻找和替换。我正在使用for循环。我正在学习,但我几乎就在那里。我试图找到答案,但消息来源对我来说有点混乱。

delim是一个空格,你可以告诉我跳过4行并做第二个令牌。

我需要在那个地方找到的被 var5a 取代的地方。我有它向后,因为我需要 %% F 等于 var5a ,而不是相反(因为我现在已经写了)。但不知道如何写它。请解释一下如何做到这一点。我尝试使用<< =但没有运气。

for /f "skip=4 tokens=2 delims= " %%F in (script.vbs) do (
set var5a=!var5a!%%F
)

我正在学习,请善待。

2 个答案:

答案 0 :(得分:0)

如果您在%% F中有Henri,并且您希望将Henri的值设置为当前值var5a,那么

set %%F=!var5a!

!var5a!如果您希望RUN-TIME值为var5a(前提是您已调用ENABLEDELAYEDEXPANSION模式)或%var5a%(如果您需要) PARSE-TIME值)

<小时/> 计划证明答案确实有效:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

::: WARNING! This will overwrite SCRIPT.VBS

DEL script.vbs 2>nul
FOR /l %%i IN (1,1,4) DO >>script.vbs ECHO ignore this line %%i
>>script.vbs ECHO Oh Henri - it does work
>>script.vbs ECHO Now try again

ECHO ===== here is script.vbs

TYPE script.vbs

ECHO ===== script.vbs ends

SET var5a=this is the value

SET henri
SET var5a

ECHO ===== now run the FOR loop...

for /f "skip=4 tokens=2 delims= " %%F in (script.vbs) do (
 set %%F=!var5a!
)

SET henri

GOTO :eof

结果:

===== here is script.vbs
ignore this line 1
ignore this line 2
ignore this line 3
ignore this line 4
Oh Henri - it does work
Now try again
===== script.vbs ends
Environment variable henri not defined
var5a=this is the value
===== now run the FOR loop...
Henri=this is the value

现在 - "it doesn't work"你的意思是什么?当然可以。如果它没有做你想做的事情,那么就说明你为何提出要求,否则我们就会猜测。

例如,上面的内容也会将try设置为该值,因为Henri不是第2个唯一的令牌,在源文件的4行之后以空格分隔。这很容易解决(但我不是一个思想家 - 只是一个谦虚的程序员)

(set notsetyet=Y)
for /f "skip=4 tokens=2 delims= " %%F in (script.vbs) do (
 IF DEFINED NOTSETYET set %%F=!var5a!&(set notsetyet=)
)

但也许"it didn't work"因为Henri被设置为错误的值。或许它崩溃了。我们无法从您的回复中得知。

答案 1 :(得分:0)

@ECHO OFF
SETLOCAL
:: Replace token 2 (space-separated) in line 5 of a file with REPLACEMENT
:: Assumed the file exists, etc. and no line begins ":"
SET replacement=THIS IS THE REPLACEMENT TEXT
DEL newfile.txt 2>nul
FOR /f "tokens=1*delims=:" %%i IN ('findstr /n /r "$" ^<oldfile.txt') DO (
IF %%i==5 (
FOR /f "tokens=1,2* delims= " %%L IN ("%%j") DO >>newfile.txt ECHO %%L %replacement% %%N
) ELSE (>>newfile.txt ECHO.%%j)
)

TYPE oldfile.txt
ECHO ==== separator =======
FC oldfile.txt newfile.txt

结果:

Line one should not be changed
Line two should not be changed
Line three should not be changed
Line four should not be changed
changeme iwillbereplaced but only on this line
and notbereplaced on subsequent lines

including the previous line which was empty
==== separator =======
Comparing files oldfile.txt and NEWFILE.TXT
***** oldfile.txt
Line four should not be changed
changeme iwillbereplaced but only on this line
and notbereplaced on subsequent lines
***** NEWFILE.TXT
Line four should not be changed
changeme THIS IS THE REPLACEMENT TEXT but only on this line
and notbereplaced on subsequent lines
*****

存在困难 - 特别是如果文件中的行以冒号开头或包含带引号的字符串或任何常见的批处理问题,例如%


所以 - 最后我已经弄明白你在做什么了。您省略了%%F中的字符串不仅被引用的关键信息,它还包含空格。

如果你说首先,你会进入你的项目几个小时,我会被一小撮阿司匹林更富有。

为了使用带引号的字符串加载%% F,我已从文件中读取字符串。

@ECHO OFF
SETLOCAL enabledelayedexpansion
:: Replace token 2 (space-separated) in line 5 of a file with REPLACEMENT
:: Assumed the file exists, etc. and no line begins ":"
SET replacement="THIS IS THE REPLACEMENT TEXT"
DEL newfile.txt 2>NUL

:: iwbr.txt just contains
:: "i will be replaced"
:: on a single line for loading into %%F as that is the target to be replaced


FOR /f "delims=" %%F IN (iwbr.txt) DO (
FOR /f "tokens=1*delims=:" %%i IN (
  'findstr /n /r "$" ^<oldfile.txt'
 ) DO >>newfile.txt (
  IF %%i==5 (
   SET newline=%%j
   CALL SET newline=%%newline:%%F=%replacement%%%
    ECHO.!newline!
  ) ELSE (ECHO.%%j)
  )
)
TYPE oldfile.txt
ECHO ==== separator =======
FC oldfile.txt newfile.txt

结果:

Line one should not be changed
Line two should not be changed
Line three "should" not be changed
Line four should not be changed
changeme "i will bereplaced" but only on this line
and notbereplaced on subsequent lines

including the "previous" line which was empty
including this "unbalanced-quote line...
==== separator =======
Comparing files oldfile.txt and NEWFILE.TXT
***** oldfile.txt
Line four should not be changed
changeme "i will bereplaced" but only on this line
and notbereplaced on subsequent lines
***** NEWFILE.TXT
Line four should not be changed
changeme "THIS IS THE REPLACEMENT TEXT" but only on this line
and notbereplaced on subsequent lines
*****