Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 批处理脚本提取两个字符串之间的内容_Windows_Batch File - Fatal编程技术网

Windows 批处理脚本提取两个字符串之间的内容

Windows 批处理脚本提取两个字符串之间的内容,windows,batch-file,Windows,Batch File,我正在尝试编写此批处理脚本,以从如下所示的XML文件中提取两个参数: <?xml version="1.0" encoding="utf-8" ?> <!--<var name="SqlConnection" value="data source=SERVERNAME;initialcatalog=DB_NAME;user id=JackDaniels;password=G235X" />--> <var name="SqlConnection" val

我正在尝试编写此批处理脚本,以从如下所示的XML文件中提取两个参数:

<?xml version="1.0" encoding="utf-8" ?>
<!--<var name="SqlConnection" value="data source=SERVERNAME;initialcatalog=DB_NAME;user id=JackDaniels;password=G235X" />-->
<var name="SqlConnection" value="data source=SERVERNAME;initial catalog=DB_Name;Integrated Security=SSPI" />
@echo off
setlocal enableextensions disabledelayedexpansion
set "connectionString="
set result=""
set "INPUT_FILE=DBConnection.config"
FOR /F "tokens=* delims=<var eol=!" %%x in (%INPUT_FILE%) DO (
    ECHO %%x
)
PAUSE

我的目标是从未注释掉的行中提取SERVERNAME和DB_NAME

到目前为止,我的代码如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<!--<var name="SqlConnection" value="data source=SERVERNAME;initialcatalog=DB_NAME;user id=JackDaniels;password=G235X" />-->
<var name="SqlConnection" value="data source=SERVERNAME;initial catalog=DB_Name;Integrated Security=SSPI" />
@echo off
setlocal enableextensions disabledelayedexpansion
set "connectionString="
set result=""
set "INPUT_FILE=DBConnection.config"
FOR /F "tokens=* delims=<var eol=!" %%x in (%INPUT_FILE%) DO (
    ECHO %%x
)
PAUSE
@echo关闭
setlocal enableextensions disabledelayedexpansion
设置“connectionString=”
set result=“”
设置“INPUT_FILE=DBConnection.config”
对于/F“tokens=*delims=
@ECHO Off”
SETLOCAL
设置“sourcedir=U:\sourcedir”
设置“filename1=%sourcedir%\q42420941.txt”
对于%%v IN(初始目录数据源初始目录数据源),请设置“%%v=”

对于('findstr/B/L/C:\
中的/f“delims=“%%z”),提取此数据的适当方法不是通过批处理文件,而是使用注释中建议的方法。但是,下面的批处理文件以相对简单的方式执行此提取:

@echo off
setlocal EnableDelayedExpansion

rem From the line that have "<var" followed by "value"...
for /F "delims=" %%a in ('findstr "\<var.*value" input.txt') do (
   rem ... process the parts separated by space or equal sign, excepting if enclosed in quotes...
   for %%b in (%%a) do (
      rem ... and store the part after "value" variable
      if "!var!" equ "value" set "value=%%~b"
      set "var=%%~b"
   )
)

rem Separate "value" string at semicolons and assign each part via SET command
for %%a in ("%value:;=" "%") do set %%a

rem Show results:
echo data source=%data source%
echo initial catalog=%initial catalog%
echo Integrated Security=%Integrated Security%

当然,如果代码中描述的数据格式发生变化,程序将失败…

因为你们中的许多人都认为批处理不适合这样做,所以我决定使用PowerShell,只需几行代码和一些正则表达式就可以完成我想要的

$configPath = $PSScriptRoot + "DBConnection.config"
[xml]$XmlDocument = Get-Content -Path $configPath
$dataSource = $XmlDocument.var.Attributes[1].Value.ToString() # Extract the uncommented line, and the second attribute "value"

$serverName = ($dataSource -split 'data source=([^;]*);')[1] 
$db_name = ($dataSource -split 'initial catalog=([^;]*);')[1] 
$user_id = ($dataSource -split 'id=([^;]*);')[1]
$user_pass = ($dataSource -split 'password=([^;]*)')[1] 

您的问题的答案是:不。
;)
更简单的方法是使用脚本语言,该语言能够读取纯格式的XML文件。VBscript、Jscript和Powershell都具有此功能。@Squashman谢谢!我决定编写一个鲍威尔脚本,使用正则表达式完成这项工作。我把代码贴在下面。