简体   繁体   中英

Batch Script File to Extract XML Tag Data

Need help extracting Data from xml and passing it to Batch File Variables...

i cant seem to get the data needed and be able to pass it to batch variable.

i need to extract "AH0417" from the TAGID.... Catch 22- this file changes the TAGID / PHASEID / BOXID

so they are rarely the same... i can extract one specific file if i change delims to

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

thats where i am now with all the different methods of delims and or tokens, if i use skip=3 it causes unexpected ]

any help would be outstanding, i've been working this all week and posted online for help, still no replies...i dont know if this is just going to be that difficult or impossible, but i know its possible, i need the right set of eyes looking at this lol. thanks in advance for any help...

    <?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>

i have used many different combinations with tokens and delims but i either get blank %PhaseID% or i get the number "2" as a result instead of actual data. this is what i am trying to do...

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%

this is the full script.....

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

IT's DONE!!!!! WENT WITH POWERSHELL IT WAS TOOOOO EASY!!!! had a problem getting the output from Powershell back into batch file variable....

figured it out on the extracting the Powershell Output into Batch File Variable....

i wrote 3 separate PS Scripts so i can use them individually later in other scripts....

PowerShell Script TAG.PS1

[CODE]

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

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

    {

    write-output $tagid.innertext

    }

[/CODE]

PowerShell Script PHASE.PS1

[CODE]

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

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

    {

    write-output $phaseid.innertext

    }

[/CODE]

PowerShell Script BOX.PS1

[CODE]

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

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

    {

    write-output $Boxid.innertext

    }

[/CODE]

Windows Batch File *.bat

[CODE]

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..........

[/CODE]

OUTPUT AS FOLLOWS:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM