批处理文件,如何输入文件以及文件输出变量

问题描述 投票:0回答:1

嗨,我想制作一个批处理文件,该文件使用批处理文件中的变量键入文件

批处理文件:

set name="relity"
set status="online"
type test.txt

测试.txt:

Name : %name%
Status : %status%`

嗨,我想制作一个批处理文件,该文件使用批处理文件中的变量键入文件

type test.txt

test.txt 看起来像这样:

Name : %name%
Status : %status%`

我想要“%name%”和“%status%”来获取批处理文件中我想要的值

set name="relity"
set status="online"
type test.txt

当它打字时,我想打印这个:

Name : relity
Status : online
batch-file cmd
1个回答
0
投票

您可以创建一个批处理文件,将

test.txt
文件中的变量替换为您在批处理文件中设置的值:

@echo off
setlocal enabledelayedexpansion

set name="relity"
set status="online"

set "inputFile=test.txt"
set "outputFile=output.txt"

REM Read the input file and replace variables
for /f "delims=" %%a in ('type "%inputFile%"') do (
    set "line=%%a"
    set "line=!line:%name%=!name!"
    set "line=!line:%status%=!status!"
    echo !line! >> "%outputFile%"
)

REM Display the result
type "%outputFile%"

endlocal

使用

.bat
扩展名保存此脚本并运行它。输出将显示在控制台上,您还可以检查
output.txt
文件中的结果。

© www.soinside.com 2019 - 2024. All rights reserved.