1

vbscript またはバッチ ファイルを作成して、ほぼ 100 個以上のファイル (同じディレクトリ内) を実行し、次の操作を実行したいと考えています。

文字列で始まる各行"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

(「two」という単語の後にスペースが 1 つだけあります。)

4

3 に答える 3

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 に答える
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
@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 に答える