0

我正在尝试查找和替换。我正在使用 for 循环。我正在学习,但我快到了。我试图找到答案,但来源对我来说有点太混乱了。

delim 是一个空格,正如您所知道的,我正在跳过 4 行并执行第二个标记。

我需要在那个地方找到的东西被var5a替换。我把它倒过来,因为我需要%%F等于var5a,而不是相反(就像我现在写的那样)。但是不知道怎么写。请解释如何做到这一点。我试过使用 <<= 但没有运气。

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

我正在学习,所以请善待。

4

2 回答 2

0

如果你有Henri%%F 并且你想将 的值设置为thenHenri的当前值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被设置为错误的值。或者它可能崩溃了。我们无法从您的回复中看出。

于 2013-03-21T05:49:39.347 回答
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
*****
于 2013-03-21T08:01:16.103 回答