title: Batch File: Edit multiple files and save with names based on variables, plus insert text into output files
body: <p>I have XML files that I need to split into multiple files, and I already have a batch file that does this well for single files.  I need to edit the batch file to handle a large number of XML files in a single directory.  I've attempted several different ways  resolve this, but just can't seem to get it working.</p>
  
  <p>As an added bonus, I'd also like to have the output filename contain items from two different lines within each output file, along with inserted text at the beginning and end of each file.</p>
  
  <p>Here's my current batch file:  </p>
  
  <pre><code>@ECHO OFF  
  SETLOCAL ENABLEDELAYEDEXPANSION  
  SET /a destadd=0  
  SET "OrderString=parentName"  
  SET "found="  
  FOR /f "delims=" %%a IN (outbound_shipment_20161023.xml) DO (  
   ECHO("%%a"|FIND "%NRTOrderString%" &gt;NUL  
   IF NOT ERRORLEVEL 1 (SET found=Y&amp;set/a destadd+=1)  
   IF DEFINED found &gt;&gt;"Case!destadd!.xml" ECHO %%a  
  )  
  
  GOTO :EOF  
  </code></pre>
  
  <p>An example of the output file after processing with the above:  </p>
  
  <pre><code>&lt;Order parentName="ABC-Corp"&gt;  
  &lt;ClientName&gt;ABC&lt;/ClientName&gt;  
  &lt;GroupName&gt;Client&lt;/GroupName&gt;  
  &lt;SiteName&gt;Home&lt;/SiteName&gt;  
  &lt;CaseNumber&gt;634713&lt;/CaseNumber&gt;  
  &lt;ShippingDate&gt;2016-10-20&lt;/ShippingDate&gt;  
  &lt;ShippingMethod&gt;UPS&lt;/ShippingMethod&gt;  
  &lt;TrackingNumber&gt;89273496512349823723423&lt;/TrackingNumber&gt;  
  &lt;OrderDetail&gt;  
    &lt;ItemShipped&gt;ABC123&lt;/ItemShipped&gt;  
    &lt;Quantity&gt;1&lt;/Quantity&gt;  
    &lt;ClientOrderId&gt;ABCDEF876&lt;/ClientOrderId&gt;  
  &lt;/OrderDetail&gt;  
  </code></pre>
  
  <p>  </p>
  
  <p>Each original XML file contains several of the individual outputs shown above, that are then separated using the batch file.</p>
  
  <p>The batch file above outputs files with names Case1.xml, Case2.xml, etc.  I'd like that to change to be "Case_CaseNumber_ClientOrderID.xml".  In addition it would need to have "Shipment" inserted on new lines at the beginning and end of each output file.</p>
  
  <p>Thanks for any help.</p>