使用批处理将文本文件中的字符串和变量替换为字符串

时间:2014-05-25 00:22:27

标签: batch-file

我试过这个并且它无效......:

@echo off
echo seting up session...
setLocal EnableDelayedExpansion
type command1.txt > session.html
for /f "tokens=* delims= " %%a in (command2.txt) do (
set /a N+=1
set v[!N!]=%%a
)
set line1=%v[1]%

echo %line1%

endlocal
)

REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION

::BatchSubstitude - parses a File line by line and replaces a substring"
::syntax: BatchSubstitude.bat OldStr NewStr File
::          OldStr [in] - string to be replaced
::          NewStr [in] - string to replace with
::          File   [in] - file to be parsed
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.
)
start session.html
echo done! Enjoy!
pause

我想做的是:

<p><b>This is a Simulated Media Rendering Session
<br></br></b>Made by MicroTech</p><br></br>Visit us @ mtcomputers.weebly.com<br>     </br>and find SMRS Info @ mtsmrs.weeby.com<br></br>Have fun on the internet!<br></br>Here is Yor Media:</p>
<iframe width="420" height="345"
src="http://www.youtube.com/embed/code"
</iframe>
<p>
http://www.mtcopmuters.weebly.com
</p>

看到scr="http:/....事情?我需要将该行中的“代码”更改为文本文件中的变量(上面管理)。但问题是:我需要在我的批处理文件中设置一个等于command2.txt(已经完成)中的文本的变量,然后我需要将上面的html代码段中的“code”更改为分配给该变量。在任何答案,请描述我把文件名和一切等等放在哪里....谢谢!祝你有个美好的一天!

1 个答案:

答案 0 :(得分:1)

你表达的很糟糕,你想要什么。你希望我们通过说&#34;请描述我把文件名和所有内容放在哪里等等。&#34;但是你没有给我们类似的礼貌。

实际上,您已将来自不同位置的两段代码粘贴在一起,而不会生成批处理文件。例如,批处理文件的第一部分不包含退出。第二部分显然来自另一个批处理文件。这可以作为子例程呈现并调用:

@echo off
echo setting up session...
setLocal EnableDelayedExpansion
type command1.txt > session.html
for /f "tokens=* delims= " %%a in (command2.txt) do (
    set /a N+=1
    set v[!N!]=%%a
    )
set line1=%v[1]%

echo %line1%
call :BatchSubstitute code %line1% session.html

start session.html
echo done! Enjoy!
pause
:: exit main body
exit /b

:: Start of Subroutines ------
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION

::BatchSubstitude - parses a File line by line and replaces a substring"
::syntax: BatchSubstitute.bat OldStr NewStr File
::          OldStr [in] - string to be replaced
::          NewStr [in] - string to replace with
::          File   [in] - file to be parsed
:BatchSubstitute
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.
)
:: Exit Subroutine
exit /b

我希望我能够正确猜出你想要的结果.....