从批处理脚本中的文本文件中读取变量值

时间:2015-07-27 10:33:12

标签: windows batch-file

在我的BAT文件中,我想从.txt文件中读取这些数据,并需要将每个数据设置为一个变量

    SQLUserName=MFiles
    WrapperSQLServerName=usa.com
    WrapperDatabase=Wrapper
    WrapperAssemblyLocation=D:\Assembly
    MFilesNetworkAddress=USA-S1
    MFilesVaultName=MF2
    MFilesUsername=User
    MFilesPassword=
    MFilesVaultGUID={26F30-E120-408C-8035-04D85D6}
    MFilesWebServerURL=www.WebServer.com
    SQLMailProfileName=NoReply
    WrapperSupportEmail=thejus@WebServer.com

我尝试使用此代码

FOR /F "tokens=2 delims==" %%a IN ('find "WrapperSupportEmail" ^<config.txt') DO SET SupportEmail=%%a

但它会抛出错误

find is not recognized as an internal or external command operable program or batch file

请帮帮我

Thejus T V

3 个答案:

答案 0 :(得分:2)

如果输入文件的架构是固定的(keyword = value),并且您想要将所有值分配给名为keyword的环境变量,则非常非常容易。试试这个:

for /f "tokens=1,2 delims==" %i in (config.txt) do set %i=%j

如果要将此调用放入cmd文件,请记住将%i和%j更改为%% i和%% j。

答案 1 :(得分:0)

when two members with high reputation tell you it works, then it shouldn't be the worst of all ideas to at least try it.

rem @echo off
for /f "delims=" %%i in (config.txt) do set _%%i
pause
set _

I REMd the @echo off, so you can watch, how every single line get's processed.

(Your original error find is not recognized ... is probably because you messed up your %path%-variable, but you don't need find for this task.)

答案 2 :(得分:0)

我的代码是正确的。唯一的问题是Windows无法找到 find.exe 。我将.exe包含在同一位置,之后一切正常。

它在Win 7和Win 10中正常工作,但没有添加 Find.exe