如何运行结合findstr脚本

How to run combined findstr script

您好,我使用 this 页面上的信息制作了一个脚本。

我意识到 运行 两个单独的批处理文件(:SetVar:Brains)可以工作,但是当我尝试将两者合并为一个时它不起作用。

如果有人能向我解释为什么我的不起作用并告诉我我可以做些什么来使我的工作正常,我会很高兴。

:SetVar
Brains "Hello" "Goodbye" "input.txt" > "newfile.txt"
GOTO :Brains

:Brains
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
    set "line=%%B"
    if defined line (
        call set "line=echo.%%line:%~1=%~2%%"
        for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
    ) ELSE echo.
)
PAUSE

当我使用调用命令时:

call :SetVar
call :Brains
goto :eof
pause

:SetVar
:Brains "Hello" "Goodbye" "input.txt" > "newfile.txt"
goto :eof

:Brains
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
    set "line=%%B"
    if defined line (
        call set "line=echo.%%line:%~1=%~2%%"
        for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
    ) ELSE echo.
)
goto :eof

我没有收到任何错误,但我得到:

C:\Users\MALKAVIAN\Batch files\change words in file>CALL :SetVar

C:\Users\MALKAVIAN\Batch files\change words in file>goto :eof

C:\Users\MALKAVIAN\Batch files\change words in file>CALL :Brains

C:\Users\MALKAVIAN\Batch files\change words in file>goto :eof

C:\Users\MALKAVIAN\Batch files\change words in file>pause
Press any key to continue . . .
@echo off

:SetVar
call :Brains "Hello" "Goodbye" "input.txt"
GOTO :Brains

:Brains
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
    set "line=%%B"
    if defined line (
        call set "line=echo.%%line:%~1=%~2%%"
        for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
    ) ELSE echo.
)
PAUSE

最后一次尝试,因为我是一个友好的人,想要提供帮助...获取此代码,将其放入批处理文件中,并将批处理文件放入与 input.txt 相同的目录中。将文本放入该文本文件中,保存并 运行 脚本。它应该回显文档中写入的所有行,并将输出 中的 Hello 更改为 Goodbye

您可以试试这个批处理文件:

我添加了第四个参数来输出新文本文件中的替换结果:

编辑

例如,您可以找到要替换为 "https"

的子字符串 "http"
@echo off
set "NewStr=https"
set "InFile=%~dp0%~n0_InputTest.txt"
set "OutPutFile=%~dp0%~n0_OutPutTest.txt"
If Exist "%InFile%" Del "%InFile%"
If Not Exist "%InFile%" Call :CreateExampleInputFile
Call :FindReplace "" "%NewStr%" "%InFile%"
pause
set "OldStr=http"
Call :FindReplace "%OldStr%" "%NewStr%" "%InFile%" "%OutPutFile%"
Start "" "%OutPutFile%"
Pause & Exit /b
::**************************************************************
:FindReplace <OldStr> <NewStr> <InFile> <OutPutFile>
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
::FindReplace - parses a File line by line and replaces a substring"
::syntax: FindReplace <OldStr> <NewStr> <InFile> <OutPutFile>
::          OldStr [in] - string to be replaced
::          NewStr [in] - string to replace with
::          InFile [in] - file to be parsed
::          OutFile[out] - New output file
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
(
    for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
        set "line=%%B"
        if defined line (
            call set "line=echo.%%line:%~1=%~2%%"
            for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
        ) ELSE echo.
    )
)>%4
Exit /b
::**************************************************************
:CreateExampleInputFile
(
    echo http://www.hyperdebrid.com
    echo http://www.fakirdebrid.net
    echo http://www.keepfiles.fr
    echo http://www.4shared.com 
    echo http://1fichier.com
    echo http://www.mega.co.nz
    echo http://www.mediafire.com
    echo http://www.uploaded.net
    echo http://www.oboom.com
    echo http://www.letitbit.net
    echo http://www.keep2share.cc
    echo http://alfafile.net
    echo http://www.bigfile.to
    echo http://www.dailymotion.com
    echo http://www.datafile.com
    echo http://www.Depfile.com
    echo http://www.Dropbox.com
    echo http://www.Extmatrix.com
    echo http://www.Fboom.me
    echo http://www.Filefactory.com
    echo http://www.Filesmonster.com
    echo http://www.Fshare.vn
    echo http://www.Keep2share.com
    echo http://www.Mega.nz
    echo http://www.Rapidgator.net
    echo http://www.Scribd.com
    echo http://www.Soundcloud.com
    echo http://www.Speedyshare.com
    echo http://www.Turbobit.net
    echo http://www.Vimeo.com
 )>"%InFile%"
 ::**************************************************************

希望本文对您有所帮助。

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION

CALL :Brains "Hello" "Goodbye" "input.txt"
GOTO :EOF

:Brains
IF "%~1"=="" (FINDSTR/R "^::" "%~f0"
    PAUSE
    GOTO :EOF)
(FOR /F "TOKENS=1* DELIMS=]" %%A IN ('FIND /N /V ""^<%3') DO (IF "%%~B" EQU "" (
            ECHO=) ELSE (SET "line=%%B"
            CALL ECHO=%%line:%~1=%~2%%)))>"newfile.txt"
PAUSE