无法将参数传递给批处理文件中的功能

时间:2017-02-24 10:55:06

标签: batch-file

我是批处理文件脚本的新手,我需要将参数传递给批处理文件功能,但不能这样做。

脚本如下所示。我正在尝试将 test.txt 中的原始文字替换为更新文字

set "originalText = Original"
set "updatedText = Updated"

set "sourceLocation = C:\Users\User\Desktop\Temp\Test.txt"
set "targetLocation = C:\Users\User\Desktop\Temp\Test.new"

call:replaceText %originalText% %updatedText% %sourceLocation% %targetLocation%

EXIT /B %ERRORLEVEL%

:replaceText
SETLOCAL
set "replace = %~1"
set "replaced = %~2"

set "source = %~3"
set "target = %~4"

echo %replace%
echo %replaced%
echo %source%
echo %target%

timeout /t 40

    setlocal enableDelayedExpansion
        (
           for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%') do (
              set "line=%%b"
              if defined line set "line=!line:%replace%=%replaced%!"
           )
        ) > %target%
    endlocal

DEL source
REN target source
ENDLOCAL
EXIT /B 0

0 个答案:

没有答案