Move the searched file from XML to a directory batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
newbie2013
Posts: 16
Joined: 26 Aug 2014 22:57

Move the searched file from XML to a directory batch file

#1 Post by newbie2013 » 27 Aug 2014 00:08

I am a newbie to batch file creation. Your help is much appreciated. Kindly help. My requirement is as like below.

1) I have a directory with different files. (Eg: 10225_1409069145475_desc.xml, 24185_1409063749859.pdf, 881_1409076406553.htm etc).

2) I have to read all XML files one after the other among the available files in the above directory and compare the string. String is definitely embedded between <reportSearchPath> and </reportSearchPath> tags with different special characters (Eg: <reportSearchPath>/content/folder[@name='Finance - Governed Reports']/folder[@name='Management and Board Pack']/folder[@name='Production Version']/folder[@name='Reports modified for 2015']/folder[@name='Reports ready for UAT']/report[@name='MGT 9a - Project Resource Summary Report']</reportSearchPath>).

3) When the search string matches, it has to pull file name from the same xml. Filename is again embedded between <fileName> and </fileName> tags.

4) File name that found in the above has to be moved to different directory. FYI, searched file resides in the same directory.

I would like to schedule this batch once a day to move the files to another directory.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Move the searched file from XML to a directory batch fil

#2 Post by aGerman » 27 Aug 2014 07:58

Batch isn't good in reading and matching strings in XML files for various reasons:
- Batch reads files line by line while XML doesn't care about lines. Instead it workes with node objects, pathes to the nodes, differentiates between node names, properties, values, CDATA objects, etc.
- Line lengthes in Batch are restricted (depending on the code where you process them its max. 8191 characters). Because linebreaks and indentations are ignored in XML the entire file could be theoretically written in a single line.
- The character encoding may differ. While Batch normally reads, processes and writes in a (language dependend) ASCII code page an XML file is encoded in UTF-8 by default (or explicitly named otherwise).
And furter more ...

Even if this is a Batch forum and usually we try to solve everything possible in Batch I only can recomment you to use another (scripting-) language. E.g. JScript is able to use the XML DOM and could be included into a Batch code. Provided you would post the whole content of such an XML file and you would explain again how it should work (because none of the strings in your XML file matches a file name that you gave in your examples) we are probably able to help out in JScript as well.

Regards
aGerman

Squashman
Expert
Posts: 4470
Joined: 23 Dec 2011 13:59

Re: Move the searched file from XML to a directory batch fil

#3 Post by Squashman » 27 Aug 2014 10:22

Somebody just posted a vbscript here the other day to do something similar. Will see if I can dig that thread up.

newbie2013
Posts: 16
Joined: 26 Aug 2014 22:57

Re: Move the searched file from XML to a directory batch fil

#4 Post by newbie2013 » 27 Aug 2014 19:18

Thank you for your prompt reply. Please see XML sample. Squashman, please give me VB script also.

Code: Select all

<?xml version="1.0" encoding="utf-8" ?> 
- <!--   Copyright (C) 2007 Cognos Incorporated.  All Rights Reserved.
  Cognos (R) is a trademark of Cognos Incorporated.

  -->
- <!--   Experimental.
  Product features described in this file may not be supported in future releases.

  -->
- <outputDescriptor xmlns="http://developer.cognos.com/schema/OutputDescriptor/1.0" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <asOfTime>2014-08-27T00:35:14.346Z</asOfTime>
  <burstKey />
  <contact />
  <defaultDescription />
  <defaultName>2014-08-27T00:35:49.859Z</defaultName>
  <fileName>24185_1409063749859.pdf</fileName>
  <locale>en-au</locale>
  <owner>Administrator</owner>
  <ownerNamespace>Default</ownerNamespace>
- <parameterValues>
- <item>
  <name>parmExclude</name>
- <value>
- <item xsi:type="simpleParmValueItem">
  <inclusive>true</inclusive>
  <display>include Overheads</display>
  <use>ZZZZZ</use>
  </item>
  </value>
  </item>
- <item>
  <name>parmDeptLevel5</name>
  <value />
  </item>
- <item>
  <name>parmDeptLevel1</name>
  <value />
  </item>
- <item>
  <name>parmDeptTreeDate</name>
- <value>
- <item xsi:type="simpleParmValueItem">
  <inclusive>true</inclusive>
  <display>20140701</display>
  <use>20140701</use>
  </item>
  </value>
  </item>
- <item>
  <name>Month</name>
- <value>
- <item xsi:type="simpleParmValueItem">
  <inclusive>true</inclusive>
  <display>Jul 2014</display>
  <use>201501</use>
  </item>
  </value>
  </item>
- <item>
  <name>parmDeptLevel4</name>
  <value />
  </item>
- <item>
  <name>_P3086448151</name>
- <value>
- <item xsi:type="simpleParmValueItem">
  <inclusive>true</inclusive>
  <display>1000 - Bulk Meter Replacement-Phase 1</display>
  <use>1000</use>
  </item>
  </value>
  </item>
- <item>
  <name>parmMthOrDate</name>
- <value>
- <item xsi:type="simpleParmValueItem">
  <inclusive>true</inclusive>
  <display>Select Fiscal Year and Month</display>
  <use>MTH</use>
  </item>
  </value>
  </item>
- <item>
  <name>parmProjTreeDate</name>
- <value>
- <item xsi:type="simpleParmValueItem">
  <inclusive>true</inclusive>
  <display>20120701</display>
  <use>20120701</use>
  </item>
  </value>
  </item>
- <item>
  <name>parmDeptSelection</name>
- <value>
- <item xsi:type="simpleParmValueItem">
  <inclusive>true</inclusive>
  <display>Run for selected departments</display>
  <use>Yes</use>
  </item>
  </value>
  </item>
- <item>
  <name>parmDeptLevel6</name>
  <value />
  </item>
- <item>
  <name>parmDeptLevel2</name>
  <value />
  </item>
- <item>
  <name>parmProjectID</name>
- <value>
- <item xsi:type="simpleParmValueItem">
  <inclusive>true</inclusive>
  <display>1000 - Bulk Meter Replacement-Phase 1</display>
  <use>1000</use>
  </item>
  </value>
  </item>
- <item>
  <name>parmDateRange</name>
  <value />
  </item>
- <item>
  <name>parmDeptLevel3</name>
  <value />
  </item>
- <item>
  <name>parmDeptLevel7</name>
  <value />
  </item>
- <item>
  <name>parmDept</name>
  <value />
  </item>
- <item>
  <name>ACTreeDate</name>
- <value>
- <item xsi:type="simpleParmValueItem">
  <inclusive>true</inclusive>
  <display>20140701</display>
  <use>20140701</use>
  </item>
  </value>
  </item>
  </parameterValues>
  <reportSearchPath>/content/folder[@name='Finance - Governed Reports']/folder[@name='Management and Board Pack']/folder[@name='Production Version']/folder[@name='Reports modified for 2015']/folder[@name='Reports ready for UAT']/report[@name='MGT 9a - Project Resource Summary Report']</reportSearchPath>
  <reportViewSearchPath xsi:nil="true" />
  </outputDescriptor>

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Move the searched file from XML to a directory batch fil

#5 Post by foxidrive » 27 Aug 2014 20:54

newbie2013 wrote:Please see XML sample.


The sample appears to have been edited as it doesn't look like valid XML to me.

The problem with the sample is that we don't know the original format of the file wrt line breaks - can you put an accurate file up for download?

newbie2013
Posts: 16
Joined: 26 Aug 2014 22:57

Re: Move the searched file from XML to a directory batch fil

#6 Post by newbie2013 » 27 Aug 2014 23:16

foxidrive wrote:
newbie2013 wrote:original format of the file wrt line breaks - can you put an accurate file up for download?


I am sorry to ask you this silly question. I am not finding any option on this post to upload the XML file.
If the option to upload is not available, please tell me the process/format to put the file here so you can download.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Move the searched file from XML to a directory batch fil

#7 Post by foxidrive » 28 Aug 2014 02:04

The forum does not have storage for files. but you can use Dropbox or any other web storage to save the file to and put the link in a reply.

Squashman
Expert
Posts: 4470
Joined: 23 Dec 2011 13:59

Re: Move the searched file from XML to a directory batch fil

#8 Post by Squashman » 28 Aug 2014 07:46

newbie2013 wrote:Squashman, please give me VB script also.

Didn't say I would write or give you the script. I just said I would search for the previous thread that talked about it.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Move the searched file from XML to a directory batch fil

#9 Post by aGerman » 28 Aug 2014 14:04

Please run the Notepad Editor (notepad.exe). Menu File > Open File. Choose All Files (*.*) instead of only text files. Navigate to your XML file and open it. Now you should be able to copy the content in a valid manner.

Althoug it's still not clear to me what strings should be matched and where to find them in the real file system.

Regards
aGerman

newbie2013
Posts: 16
Joined: 26 Aug 2014 22:57

Re: Move the searched file from XML to a directory batch fil

#10 Post by newbie2013 » 28 Aug 2014 22:52

foxidrive wrote:The forum does not have storage for files. but you can use Dropbox or any other web storage to save the file to and put the link in a reply.


Please find the dropbox URL. Hope this works.

[url]

https://www.dropbox.com/s/nvj902dbdk152 ... c.xml?dl=0

[/url]

newbie2013
Posts: 16
Joined: 26 Aug 2014 22:57

Re: Move the searched file from XML to a directory batch fil

#11 Post by newbie2013 » 28 Aug 2014 23:01

aGerman wrote:Now you should be able to copy the content in a valid manner.

Regards
aGerman


I am also copying the content as suggested by you.

Code: Select all


<?xml version="1.0" encoding="utf-8"?>
<!--
  Copyright (C) 2007 Cognos Incorporated.  All Rights Reserved.
  Cognos (R) is a trademark of Cognos Incorporated.
-->
<!--
  Experimental.
  Product features described in this file may not be supported in future releases.
-->
<outputDescriptor
      xmlns="http://developer.cognos.com/schema/OutputDescriptor/1.0"
      xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      >
   <asOfTime>2014-08-27T00:35:14.346Z</asOfTime>
   <burstKey></burstKey>
   <contact/>
   <defaultDescription></defaultDescription>
   <defaultName>2014-08-27T00:35:49.859Z</defaultName>
   <fileName>24185_1409063749859.pdf</fileName>
   <locale>en-au</locale>
   <owner>Administrator</owner>
   <ownerNamespace>Default</ownerNamespace>
   <parameterValues>
      <item>
         <name>parmExclude</name>
         <value>
            <item xsi:type="simpleParmValueItem">
               <inclusive>true</inclusive>
               <display>include Overheads</display>
               <use>ZZZZZ</use>
            </item>
         </value>
      </item>
      <item>
         <name>parmDeptLevel5</name>
         <value/>
      </item>
      <item>
         <name>parmDeptLevel1</name>
         <value/>
      </item>
      <item>
         <name>parmDeptTreeDate</name>
         <value>
            <item xsi:type="simpleParmValueItem">
               <inclusive>true</inclusive>
               <display>20140701</display>
               <use>20140701</use>
            </item>
         </value>
      </item>
      <item>
         <name>Month</name>
         <value>
            <item xsi:type="simpleParmValueItem">
               <inclusive>true</inclusive>
               <display>Jul 2014</display>
               <use>201501</use>
            </item>
         </value>
      </item>
      <item>
         <name>parmDeptLevel4</name>
         <value/>
      </item>
      <item>
         <name>_P3086448151</name>
         <value>
            <item xsi:type="simpleParmValueItem">
               <inclusive>true</inclusive>
               <display>1000 - Bulk Meter Replacement-Phase 1</display>
               <use>1000</use>
            </item>
         </value>
      </item>
      <item>
         <name>parmMthOrDate</name>
         <value>
            <item xsi:type="simpleParmValueItem">
               <inclusive>true</inclusive>
               <display>Select Fiscal Year and Month</display>
               <use>MTH</use>
            </item>
         </value>
      </item>
      <item>
         <name>parmProjTreeDate</name>
         <value>
            <item xsi:type="simpleParmValueItem">
               <inclusive>true</inclusive>
               <display>20120701</display>
               <use>20120701</use>
            </item>
         </value>
      </item>
      <item>
         <name>parmDeptSelection</name>
         <value>
            <item xsi:type="simpleParmValueItem">
               <inclusive>true</inclusive>
               <display>Run for selected departments</display>
               <use>Yes</use>
            </item>
         </value>
      </item>
      <item>
         <name>parmDeptLevel6</name>
         <value/>
      </item>
      <item>
         <name>parmDeptLevel2</name>
         <value/>
      </item>
      <item>
         <name>parmProjectID</name>
         <value>
            <item xsi:type="simpleParmValueItem">
               <inclusive>true</inclusive>
               <display>1000 - Bulk Meter Replacement-Phase 1</display>
               <use>1000</use>
            </item>
         </value>
      </item>
      <item>
         <name>parmDateRange</name>
         <value/>
      </item>
      <item>
         <name>parmDeptLevel3</name>
         <value/>
      </item>
      <item>
         <name>parmDeptLevel7</name>
         <value/>
      </item>
      <item>
         <name>parmDept</name>
         <value/>
      </item>
      <item>
         <name>ACTreeDate</name>
         <value>
            <item xsi:type="simpleParmValueItem">
               <inclusive>true</inclusive>
               <display>20140701</display>
               <use>20140701</use>
            </item>
         </value>
      </item>
   </parameterValues>
   <reportSearchPath>/content/folder[@name=&apos;Finance - Governed Reports&apos;]/folder[@name=&apos;Management and Board Pack&apos;]/folder[@name=&apos;Production Version&apos;]/folder[@name=&apos;Reports modified for 2015&apos;]/folder[@name=&apos;Reports ready for UAT&apos;]/report[@name=&apos;MGT 9a - Project Resource Summary Report&apos;]</reportSearchPath>
   <reportViewSearchPath xsi:nil="true"/>
</outputDescriptor>



aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Move the searched file from XML to a directory batch fil

#12 Post by aGerman » 29 Aug 2014 03:31

Give that a shot:

Code: Select all

@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment

@echo off &setlocal
for /f "delims=" %%i in ('dir /a-s /b *.xml') do (
  set "xmlfile=%%~i"
  for /f "delims=" %%j in ('cscript //E:JScript //nologo "%~fs0" "%%~fsi"') do (
    set "folder=%%~j"
    call :proc
  )
)
pause
exit /b

:proc
echo Do something with "%folder%"
echo read in "%xmlfile%"
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
exit /b

*/
if (WScript.Arguments.Count() != 1) { WScript.Quit(1); }
var oXmlDoc = new ActiveXObject('Microsoft.XMLDOM');
oXmlDoc.async = false;
oXmlDoc.load(WScript.Arguments(0));
var oErr = oXmlDoc.parseError;
if (oErr.errorCode == 0) {
  var oNode = oXmlDoc.documentElement.selectSingleNode("//reportSearchPath");
  aText = oNode.text.split("'");
  for (i = 1; i < aText.length; i += 2)
  {
    WScript.stdOut.writeLine(aText[i]);
  }
} else {
  WScript.stdErr.write(oErr.reason + '\n' + 'At line: ' + oErr.line + ', Position: ' + oErr.linepos + '\n');
  WScript.Quit(1);
}


I still don't understand what to do in sub routine :proc.

Regards
aGerman

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Move the searched file from XML to a directory batch fil

#13 Post by Aacini » 29 Aug 2014 07:28

I tried to write a Batch file solution based on your requirements, and found that there are missing information. This is what I have so far:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem 1- I have a directory with different files.
cd "\the\target\directory"

rem 2- I have to read all XML files one after the other among the available files in the above directory and compare the string.
for %%f in (*.XML) do (

   rem String is definitely embedded between <reportSearchPath> and </reportSearchPath> tags
   for /F "delims=" %%a in ('findstr /C:"<reportSearchPath>" %%f') do set "string=%%a"
   ECHO SEARCH STRING: "!string!"

   rem 3- When the search string matches,
   if "!string:Reports modified for 2015=!" neq "!string!" (

      rem it has to pull file name from the same xml. Filename is again embedded between <fileName> and </fileName> tags.
      for /F "tokens=3 delims=<>" %%n in ('findstr /C:"<fileName>" %%f') do (

         rem 4- File name that found in the above has to be moved to different directory.
         ECHO move "%%n" "different\directory"

      )
   )
)

This is the output of previous Batch file when run on your example data:

Code: Select all

SEARCH STRING: "   <reportSearchPath>/content/folder[@name=&apos;Finance - Gover
ned Reports&apos;]/folder[@name=&apos;Management and Board Pack&apos;]/folder[@n
ame=&apos;Production Version&apos;]/folder[@name=&apos;Reports modified for 2015
&apos;]/folder[@name=&apos;Reports ready for UAT&apos;]/report[@name=&apos;MGT 9
a - Project Resource Summary Report&apos;]</reportSearchPath>"
move "24185_1409063749859.pdf" "different\directory"

You must note that previous program assume that the line with "<fileName>" tag have spaces before the tag.

Antonio

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Move the searched file from XML to a directory batch fil

#14 Post by aGerman » 29 Aug 2014 10:12

Aacini wrote:You must note that previous program assume that the line with "<fileName>" tag have spaces before the tag.

Those are the elements of uncertainty in XML :wink:

But you seem to understand the rules for moving while I still don't :?

Regards
aGerman

newbie2013
Posts: 16
Joined: 26 Aug 2014 22:57

Re: Move the searched file from XML to a directory batch fil

#15 Post by newbie2013 » 30 Aug 2014 07:42

Aacini wrote:
Antonio


Hi Antonio,

The code you have provided me is able to search the string and identify the file name, but it is not moving to different directory based on the selection. I have modified code that you have provided as below. Still the file is not moving to target directory.

Code: Select all

rem 4- File name that found in the above has to be moved to different directory.
         ECHO move "C:\Source\%%n" "C:\Target"
ECHO move "C:\Test\%%n" "C:\Target"


Post Reply