[BATCH] .xml inside batch

Discussion in 'Scripting' started by mysteriously, Apr 11, 2016.

  1. mysteriously

    mysteriously Guest

    Hello,

    I would like to store content of .xml file inside .cmd file in :XML section. I'll place it's content (any lines) in .cmd manually. Later I would like to copy content of batch :XML section to other file.

    Is there any way to achieve this?
     
  2. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    895
    482
    30
  3. Compo

    Compo MDL Member

    Mar 29, 2011
    136
    106
    10
    #3 Compo, Apr 11, 2016
    Last edited by a moderator: Apr 20, 2017
    If I understand correctly that sounds like a fairly trivial matter.
    Code:
    @Echo Off
    SetLocal
    
    Rem Finding line number of :XML label
    For /F "Delims=:" %%a In ('FindStr/N ^:XML$ "%~f0"') Do Set LN=%%a
    
    Rem Writing all lines below :XML label to another file
    More "%~f0" +%LN%>AnotherFile.xml
    
    GoTo :EOF
    
    Rem XML section to write to another file
    :XML
    <?xml version="1.0"?>
    <settings>
    <lister_plugin fontColor="0" bgColor="16777215" fontSize="10" fontFx="0" font="Courier New" />
    <gui lang="" />
    <debug logLevel="0" />
    <path path_7z_dll="" path_7zG_exe="" />
    <path64bit path_7zG_exe="" path_7z_dll="" />
    <compression save="1" sfx="7z.sfx" updateSfx="0" askByContent="0" askByContentTimeout="1" alwaysWait7zip="0" extractToTempCount="20" deleteToRecycleBin="1" keySimpleMode="-1">
    <settings_7zip Level="0" Archiver="" ShowPassword="0" EncryptHeaders="0">
    <Options />
    </settings_7zip>
    </compression>
    <passwords save="0" />
    <formats save="1" />
    <formatsDisabled />
    </settings>
     
  4. mysteriously

    mysteriously Guest

    Thank you compo. Works fine if the :XML section is at the end of the batch.

    What if I want to do exactly the same with .vbs in the same batch file? How to do and where to add "stop reading here" for XML, continue the batch, "start reading here" for .vbs and stop again?
     
  5. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    895
    482
    30
    #5 KNARZ, Apr 11, 2016
    Last edited by a moderator: Apr 20, 2017
    Pretty nifty.
     
  6. Compo

    Compo MDL Member

    Mar 29, 2011
    136
    106
    10
    #6 Compo, Apr 12, 2016
    Last edited by a moderator: Apr 20, 2017
    Just for fun, here's an example utilising a new method and the one I've already provided.
    Code:
    @Echo Off
    SetLocal EnableDelayedExpansion
    
    Rem Any code goes here...
    
    If Exist AnotherFile.xml GoTo XMLEnd
    Set "SN="
    For /F "Skip=1 Delims=:" %%a In ('FindStr/N ^:XML "%~f0"') Do (
    If Not Defined SN (Set "SN=%%a") Else (Set/A NC=%%a-SN-1))
    <"%~F0" (
    For /L %%a In (1,1,%SN%) Do Set/P =
    For /L %%a In (1,1,%NC%) Do (Set LN=
    Set/P LN=
    Echo(!LN!))>AnotherFile.xml
    GoTo XMLEnd
    
    :XMLBegin
    <?xml version="1.0"?>
    <settings>
    <lister_plugin fontColor="0" bgColor="16777215" fontSize="10" fontFx="0" font="Courier New" />
    <gui lang="" />
    <debug logLevel="0" />
    <path path_7z_dll="" path_7zG_exe="" />
    <path64bit path_7zG_exe="" path_7z_dll="" />
    <compression save="1" sfx="7z.sfx" updateSfx="0" askByContent="0" askByContentTimeout="1" alwaysWait7zip="0" extractToTempCount="20" deleteToRecycleBin="1" keySimpleMode="-1">
    <settings_7zip Level="0" Archiver="" ShowPassword="0" EncryptHeaders="0">
    <Options />
    </settings_7zip>
    </compression>
    <passwords save="0" />
    <formats save="1" />
    <formatsDisabled />
    </settings>
    :XMLEnd
    
    Rem Rest of code goes here...
    Set "LN="
    For /F "Delims=:" %%a In ('FindStr/N ^:VBSBegin$ "%~f0"') Do Set LN=%%a
    More "%~f0" +%LN%>AnotherFile.vbs
    
    GoTo :EOF
    
    :VBSBegin
    Set objFS = CreateObject("Scripting.FileSystemObject") 
    Set objShell = CreateObject("WScript.Shell") 
    strCurrentDirectory = objShell.CurrentDirectory 
     
    Set objFolder = objFS.GetFolder(strCurrentDirectory) 
    Set colFiles = objFolder.Files 
     
    Set objRE = New RegExp 
    objRE.Global     = True 
    objRE.IgnoreCase = False 
    objRE.Pattern    = WScript.Arguments(0) 
     
    For Each objFile In colFiles 
       bMatch = objRE.Test(objFile.Name) 
       If bMatch Then 
          WScript.Echo objFile.Name 
       End If 
    Next
    If you can work it out, by all means use it but please note I will not be expanding upon it or answering questions on it.
     
  7. mysteriously

    mysteriously Guest

    Ok, thank you so much. I'll try my best to work it out :)