1

我想编写一个 vbscript 或批处理文件来运行近一百个文件(在同一目录中)并执行以下操作:

对于以字符串开头的每一行"component "(组件后有一个空格),我想在该行的末尾添加一个空格。其他线路不受影响。

例如:

this is line one
component this is line two
this is line three

将更改为:

this is line one
component this is line two (<=space)
this is line three

(“二”后面只有一个空格。)

4

3 回答 3

0
@echo off
for /f "tokens=1,* delims= " %%a in (file.txt) do (
if "%%a"=="component" (
echo %%a %%b >>temp.txt
) else (
echo %%a %%b>>temp.txt
)
)
del file.txt /f /s /q
ren temp.txt file.txt
于 2012-11-27T17:01:34.537 回答
0
@Echo OFF

:: By Elektro H@cker

Set "File=Test.txt"
Set "Word=Component"

FOR /F "Usebackq Tokens=* Delims=" %%# IN ("%FILE%") DO (
    Echo "%%#"| FINDSTR /I "^\"%WORD%.*\"" >NUL && (
        Echo %%# >>".\Output.txt"
    ) || (
        Echo %%#>>".\Output.txt"
    )
)

Pause&Exit
于 2012-11-27T17:10:10.020 回答
0

在 VBScript 中,您可以使用正则表达式来做到这一点:

in  = "input.txt"
out = "output.txt"

Set fso = CreateObject("Scripting.FileSystemObject")

text = fso.OpenTextFile(in).ReadAll
text = Replace(text, vbCrLf, vbLf)         ' remove CR b/c it interferes with
                                           ' multiline matches

Set re = New RegExp
re.Pattern   = "^(component .*)$"
re.MultiLine = True
re.Global    = True

text = re.Replace(text, "$1 ")
text = Replace(text, vbLf, vbCrLf)         ' restore CRLF

fso.OpenTextFile(out, 2, True).Write(text)

fso.DeleteFile in
fso.MoveFile out, in
于 2012-11-27T17:13:31.233 回答