1

需要帮助从 xml 中提取数据并将其传递给批处理文件变量...

我似乎无法获得所需的数据并能够将其传递给批处理变量。

我需要从 TAGID 中提取“AH0417”.... Catch 22-此文件更改 TAGID / PHASEID / BOXID

所以它们很少相同......如果我将 delims 更改为,我可以提取一个特定的文件

for /f "tokens=7,18-21* delims=^<![CDT[^>]] " %%a in ('find /I /N "<PHASEID>"^<Config.xml') do (
set a=%%a
)
echo %a%

这就是我现在使用所有不同的 delims 和 or 标记方法的地方,如果我使用 skip=3 它会导致意外]

任何帮助都会非常出色,我整个星期都在工作并在网上发布寻求帮助,仍然没有回复...我不知道这是否会那么困难或不可能,但我知道这是可能的,我需要正确的眼睛看着这个哈哈。提前感谢您的帮助...

    <?xml version="1.0" encoding="UTF-16"?>
-<root>-<CONFIG>-<TAGID>
<![CDATA[AH0417]]>
</TAGID>-<DATETIMESTAMP>
<![CDATA[9/18/2012 4:59:08 PM]]>
</DATETIMESTAMP>-<SYSTYPE>
<![CDATA[PTWS]]>
</SYSTYPE>-<PHASEID>
<![CDATA[WJFKB02229001]]>
</PHASEID>-<BOXID>
<![CDATA[D]]>
</BOXID></CONFIG></root>

我使用了许多不同的标记和分隔符组合,但我要么得到空白的 %PhaseID%,要么得到数字“2”而不是实际数据。这就是我想要做的......

REM ***************************************(PTLS)********************************************************


    :PTLSsetup
    sET sOURCE=%HOMEDRIVE%\ULLSA\PHASE\PTLS\DATA\XML
    rem Extract XML to Batch

    rem for /f "skip=3  tokens=* delims=[" %%G IN (C:\Ullsa\phase\ptls\data\xml\Config.xml) Do (
    rem Set Serial=%%G
    rem Set PhaseID=%%H
    rem Set BoxID=%%I
    rem )

    rem echo %serial%
    rem echo %PhaseID%
    rem echo %BoxID%

这是完整的脚本......

mode con:cols=70 lines=25
rem Created by (ROBERT)
color 0c

for /f "tokens=2-4 delims=/ " %%a in ('date /t') do set year=%%c
for /f "tokens=2-4 delims=/ " %%a in ('date /t') do set month=%%a
for /f "tokens=2-4 delims=/ " %%a in ('date /t') do set day=%%b
set today=%day%-%month%-%year%
echo %today%

rem **************************(LOG CREATION)************************************************
md c:\zMigrate\Backup
set currentd=C:\zMigrate\Backup

ping -n 6 127.0.0.1 >nul
CLS
echo.

set D=%date% %time%
set T=%time%
echo ****************************************** >>%currentd%\%today%.TXT
echo %Date% ---------------------^> Log Created on %computername% >>%currentd%\%today%.TXT
echo ****************************************** >>%currentd%\%today%.TXT
echo LOG CREATED IN %currentd%\%today%.TXT
echo LOG CREATED FROM %currentd% >>%currentd%\%today%.TXT
echo ****************************************** >>%currentd%\%today%.TXT
cls

rem ******************************(MENU)****************************************************


:Menu
echo *******************
IF EXIST "C:\ULLSA\PHASE\PTLS" ECHO PTLS IS INSTALLED
IF ERRORLEVEL 1 ECHO PTLS NOT INSTALLED
IF EXIST "C:\ULLSA\PHASE\PTWS" ECHO PTWS IS INSTALLED
IF ERRORLEVEL 1 ECHO PTWS NOT INSTALLED
echo *******************
IF EXIST "C:\ULLSA\PHASE\PTLS" ECHO %T% PTLS IS INSTALLED >>%currentd%\%today%.TXT
IF ERRORLEVEL 1 ECHO %T% PTLS NOT INSTALLED >>%currentd%\%today%.TXT
IF EXIST "C:\ULLSA\PHASE\PTWS" ECHO %T% PTWS IS INSTALLED >>%currentd%\%today%.TXT
IF ERRORLEVEL 1 ECHO %T% PTWS NOT INSTALLED >>%currentd%\%today%.TXT
echo.
echo.
echo.
echo.
echo.
echo WHAT TYPE WORKSTATION IS THIS?
echo.
echo *NOTE* THIS UTILITY WILL PHASE MIGRATION FILES!!!
echo.
echo.
ECHO 1: PTLS (Phase Team Leader Station)
echo.
ECHO 2: PTWS (Phase Team Workers Station)
echo.
ECHO 0: Leave Program
ECHO.
SET /p choice=Enter a number [0,1,2]:
    if /i [%choice%]==[0] endlocal&goto end
    if [%choice%]==[] goto Menu
    if [%choice%]==[1] goto PTLS
    if [%choice%]==[2] goto PTWS
    ECHO Unrecognized entry. Please try again.
    goto Menu


REM ***************************************(TYPE CHECK)**************************************************


:PTLS
cls
ECHO *************************
echo ***Starting PTLS Check***
ECHO *************************

echo %T% ***Starting PTLS Check*** >>%currentd%\%today%.TXT

PING -n 3 127.0.0.1>nul
CLS
set PTLSDATA="C:\ULLSA\PHASE\PTLS\DATA\XML"
if exist %ptlsdata%\config.xml goto PTLSsetup
if errorlevel 1 goto PTLSFail

:PTLSFAIL
cls
echo ********************************************************
echo **PTLS XML DOES NOT EXIST, ENSURE PTLS IS INSTALLED...**
echo ********************************************************

echo %T% **PTLS XML DOES NOT EXIST, ENSURE PTLS IS INSTALLED...** >>%currentd%\%today%.TXT

ping -n 6 127.0.0.1>nul
goto Menu


:PTWS
cls
ECHO *************************
echo ***Starting PTWS Check***
ECHO *************************

echo %T% ***Starting PTWS Check*** >>%currentd%\%today%.TXT

PING -n 3 127.0.0.1>nul
CLS
set PTWSDATA="C:\ULLSA\PHASE\PTwS\DATA\XML"
if exist %ptwsdata%\config.xml goto PTWSsetup
if errorlevel 1 goto PTWSFail

:PTWSFAIL
cls
echo ********************************************************
echo **PTWS XML DOES NOT EXIST, ENSURE PTWS IS INSTALLED...**
echo ********************************************************

echo %T% PTWS XML DOES NOT EXIST, ENSURE PTWS IS INSTALLED. >>%currentd%\%today%.TXT

ping -n 6 127.0.0.1>nul
goto Menu


REM ***************************************(PTLS)********************************************************


:PTLSsetup
sET sOURCE=%HOMEDRIVE%\ULLSA\PHASE\PTLS\DATA\XML
rem Extract XML to Batch

rem for /f "skip=3  tokens=* delims=[" %%G IN (C:\Ullsa\phase\ptls\data\xml\Config.xml) Do (
rem Set Serial=%%G
rem Set PhaseID=%%H
rem Set BoxID=%%I
rem )

rem echo %serial%
rem echo %PhaseID%
rem echo %BoxID%

echo **********************************
echo ***Setting up PhaseID and BoxID***
echo **********************************

echo %T% ***Setting up PhaseID and BoxID*** >>%currentd%\%today%.TXT

ping -n 4 127.0.0.1>nul
CLS
rem extract xml data to phaseid and boxid
Set PhaseID=PhaseID
Set BoxID=BoxID
set Pbackup=%HOMEDRIVE%\Backup\%PhaseID%
set Bbackup=%HOMEDRIVE%\Backup\%PhaseID%\%BoxID%
Set Backup=%HOMEDRIVE%\Backup\%PhaseID%\%BoxID%
ping -n 4 127.0.0.1>nul
goto PTLSBackup

:PTLSBackup
if exist %backup% goto ptlsxfr
if errorlevel 1 goto ptlsmd

:ptlsmd
MD %backup% >>%currentd%\%today%.TXT
echo %T% Creating Phase Folder >>%currentd%\%today%.TXT
echo %T% Creating Box Folder >>%currentd%\%today%.TXT
echo Creating Destination
ping -n 6 127.0.0.1>nul
if exist %Bbackup% goto ptlsxfr
if errorlevel 1 echo %T% CAN^'T CREATE DESTINATION >>%currentd%\%today%.TXT
ping -n 3 127.0.0.1>nul
CLS
goto menu

:ptlsxfr
echo ****************************
echo ***Created Destination***
echo ****************************

echo %T% ***Created Destination*** >>%currentd%\%today%.TXT

PING -n 3 127.0.0.1>nul
CLS
echo ****************************
echo ***Starting PTLS Transfer***
echo ****************************

echo %T% ***Starting PTLS Transfer*** >>%currentd%\%today%.TXT

ping -n 3 127.0.0.1>nul
CLS
echo.
cacls "%sOURCE%" /T /E >>%currentd%\%today%.TXT
cacls "%backup%" /T /E >>%currentd%\%today%.TXT
CLS
echo Transfering Phase:  %phaseID% Box:  %BoxID%
echo %T% Transfering Phase:  %phaseID% Box:  %BoxID% >>%currentd%\%today%.TXT
xcopy "%sOURCE%\*" "%backup%\" /Y /E /H /V /G >>%currentd%\%today%.TXT
CLS
ECHO Transfer Complete
ECHO %T% Transfer Complete >>%currentd%\%today%.TXT
ping -n 3 127.0.0.1>nul
EXIT


REM ***************************************(PTWS)********************************************************


:PTWSsetup
rem Extract XML to Batch
rem place code or cmd to call to vbs


echo **********************************
echo ***Setting up PhaseID and BoxID***
echo **********************************

echo %T% ***Setting up PhaseID and BoxID*** >>%currentd%\%today%.TXT

ping -n 4 127.0.0.1>nul
CLS
rem extract xml data to phaseid and boxid
Set PhaseID=PhaseID
Set BoxID=BoxID
set Pbackup=%HOMEDRIVE%\Backup\%PhaseID%
set Bbackup=%HOMEDRIVE%\Backup\%PhaseID%\%BoxID%
Set Backup=%HOMEDRIVE%\Backup\%PhaseID%\%BoxID%
sET sOURCE=%HOMEDRIVE%\ULLSA\PHASE\PTWS\DATA\XML
goto PTWSBackup

:PTWSBackup
if exist %backup% goto ptWsxfr >>%currentd%\%today%.TXT
if errorlevel 1 goto ptWsmd >>%currentd%\%today%.TXT

:ptWsmd
MD %backup% >>%currentd%\%today%.TXT
echo %T% Creating Phase Folder >>%currentd%\%today%.TXT
echo %T% Creating Box Folder >>%currentd%\%today%.TXT
echo Creating Destination
ping -n 6 127.0.0.1>nul
if exist %Bbackup% goto ptWsxfr
if errorlevel 1 echo %T% CAN^'T CREATE DESTINATION >>%currentd%\%today%.TXT
ping -n 3 127.0.0.1>nul
goto menu

:ptWsxfr
echo ****************************
echo ***Created Destination***
echo ****************************

echo %T% ***Created Destination*** >>%currentd%\%today%.TXT

PING -n 2 127.0.0.1>nul
cls
echo ****************************
echo ***Starting PTWS Transfer***
echo ****************************

echo %T% ***Starting PTWS Transfer*** >>%currentd%\%today%.TXT

ping -n 3 127.0.0.1>nul
CLS
echo.
cacls "%sOURCE%" /T /E >>%currentd%\%today%.TXT
cacls "%backup%" /T /E >>%currentd%\%today%.TXT
CLS
echo Transfering Phase:  %phaseID% Box:  %BoxID%
echo %T% Transfering Phase:  %phaseID% Box:  %BoxID% >>%currentd%\%today%.TXT
xcopy "%sOURCE%\*" "%backup%\" /Y /E /H /V /G >>%currentd%\%today%.TXT
ECHO Transfer Complete
ECHO %T% Transfer Complete >>%currentd%\%today%.TXT
ping -n 3 127.0.0.1>nul
EXIT
4

1 回答 1

0

完成!!!!!使用 POWERSHELL 太简单了!!!!将 Powershell 的输出返回到批处理文件变量时出现问题....

在将 Powershell 输出提取到批处理文件变量中时弄清楚了......

我写了 3 个单独的 PS 脚本,所以我以后可以在其他脚本中单独使用它们....

PowerShell 脚本 TAG.PS1

[代码]

[xml]$config1=get-content .\config.xml

foreach($tagid in $config1.root.config.tagid)

    {

    write-output $tagid.innertext

    }

[/代码]

PowerShell 脚本 PHASE.PS1

[代码]

[xml]$config1=get-content .\config.xml

foreach($phaseid in $config1.root.config.phaseid)

    {

    write-output $phaseid.innertext

    }

[/代码]

PowerShell 脚本 BOX.PS1

[代码]

[xml]$config1=get-content .\config.xml

foreach($Boxid in $config1.root.config.Boxid)

    {

    write-output $Boxid.innertext

    }

[/代码]

Windows 批处理文件 *.bat

[代码]

for /f "tokens=1" %%a in ('powershell.exe .\TAG.ps1') do set tagid=%%a

for /f "tokens=1" %%a in ('powershell.exe .\PHASE.ps1') do set phaseid=%%a

for /f "tokens=1" %%a in ('powershell.exe .\BOX.ps1') do set boxid=%%a

echo %tagid%

echo %phaseid%

echo %boxid%

timeout /t 3 /nobreak



Set Backup=%HOMEDRIVE%\Backup\%tagid%\%PhaseID%\%BoxID%

ETC..........

[/代码]

输出如下:

C:\Users\Administrator>for /F "tokens=1" %a in ('powershell.exe .\TAG.ps1') do set tag=%a

C:\Users\Administrator>set tag=AH0417

C:\Users\Administrator>for /F "tokens=1" %a in ('powershell.exe .\PHASE.ps1') do set phase=%a

C:\Users\Administrator>set phase=WJFKB02229001

C:\Users\Administrator>for /F "tokens=1" %a in ('powershell.exe .\BOX.ps1') do set box=%a

C:\Users\Administrator>set box=A

C:\Users\Administrator>echo AH0417
AH0417

C:\Users\Administrator>echo WJFKB02229001
WJFKB02229001

C:\Users\Administrator>echo A
A

C:\Users\Administrator>pause
于 2012-09-23T22:56:02.493 回答