Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: .bat file that read from .xml file and rename the .xml depending on the content  (Read 17920 times)

0 Members and 1 Guest are viewing this topic.

issux

    Topic Starter


    Starter

    hi,

    I have little experience using batch files

    I need to know how to do the following:

    i have a series of .xml files (Recovered_XML_#.XML) such as:

    C:\myfolder\Recovered_XML_1.XML
    C:\myfolder\Recovered_XML_2.XML
    C:\myfolder\Recovered_XML_3.XML
    C:\myfolder\Recovered_XML_4.XML

    I need a batch file to read the content of the xml file and copy the file name listed inside the file it self.
    i have the following path inside the xml file, which is common for all the files:

    <PATH_XML>P:\transmission\PRM_AVENGING_ANGELO\PRM_AVENGING_ANGELO.xml</PATH_XML>

    what i need is to take the name after "<PATH_XML>P:\transmission\*" and replace the name of the file Recovered_XML_4.XML

    for example:

    the file: "Recovered_XML_4.XML"
     
    contains:

    <PATH_XML>P:\transmission\PRM_AVENGING_ANGELO\PRM_AVENGING_ANGELO.xml</PATH_XML>

    i need to replace the original file name "Recovered_XML_4.XML" by "PRM_AVENGING_ANGELO.xml"

    i need this batch file because i need to change the name of 2000 recovered xml file!!
    your help is needed,


    regards,

    issux

    wbrost



      Intermediate
    • Thanked: 11
      can you please post the information in the XML files up to the PATH_XML? also is the PATH_XML always in the same place?

      issux

        Topic Starter


        Starter

        this is the complete xml data of the recovered files, Yes they are all in the same place, but there are some files that they are corrupted, so there wont be any <path_xml> visible, so we have to skip them,


         <?xml version="1.0" encoding="ISO-8859-1" ?>
        - <CVS_MEDIA>
        - <SOURCE>
          <DATE>12/02/2008</DATE>
          <HEURE>18:08:37</HEURE>
          <APPLICATION>VDCP XMIO RECORDER</APPLICATION>
          <TYPE_APPLICATION>Encoder</TYPE_APPLICATION>
          <VERSION />
          <ORIGINE>Internal</ORIGINE>
          <CAMELEON_NAME>MCT_H24</CAMELEON_NAME>
          <CAMELEON_IDENT>200</CAMELEON_IDENT>
          <DATE_ADD>12/02/2008</DATE_ADD>
          <HEURE_ADD>18:20:10</HEURE_ADD>
          </SOURCE>
        - <ARCHIVE>
          <IN_ARCHIVE>0</IN_ARCHIVE>
          </ARCHIVE>
          <ID>PRG_FISHKHEL_163</ID>
          <IDU>0</IDU>
          <STATUS>[Published]</STATUS>
          <PAD_STATUS>0</PAD_STATUS>
        - <TIMECODE>
          <SOM>00:00:00:00</SOM>
          <EOM>00:06:29:19</EOM>
          <FILE_DURATION>00:06:29:19</FILE_DURATION>
          <TCIN>00:00:00:04</TCIN>
          <TCOUT>00:06:29:19</TCOUT>
          <DURATION>00:06:29:15</DURATION>
          </TIMECODE>
        - <EDITORIAL>
          <TITRE />
          <COMMENT />
          <KEYWORD />
          <STORTYPE />
          <CATEGORIE />
          <SOUS_CATEGORIE />
          </EDITORIAL>
          <LOCK>[]</LOCK>
          <USERLOCK>0</USERLOCK>
        - <LIST_VIDEO_FILE>
        - <VIDEO_FILE>
          <FILE_TYPE>Cameleon</FILE_TYPE>
          <FILE_STATUS>Ready</FILE_STATUS>
          <PATH_XML>P:\transmission\PRM_AVENGING_ANGELO\PRM_AVENGING_ANGELO.xml</PATH_XML>
          <PATH_VIDEO>P:\transmission\PRM_AVENGING_ANGELO\PRM_AVENGING_ANGELO.avi</PATH_VIDEO>
          </VIDEO_FILE>
        - <VIDEO_FILE>
          <FILE_TYPE>SD</FILE_TYPE>
          <FILE_STATUS>Ready</FILE_STATUS>
          <PATH_XML />
          <PATH_VIDEO />
          </VIDEO_FILE>
          </LIST_VIDEO_FILE>
          <LIST_SEGMENT />
          </CVS_MEDIA>

        gh0std0g74



          Apprentice

          Thanked: 37
          if you have Python for Windows, its easy to do
          Code: [Select]
          import os,glob,sys,re
          folder=sys.argv[1]
          os.chdir(folder)
          pattern = re.compile(".*<PATH_XML>(.*?)</PATH_XML>.*",re.DOTALL|re.MULTILINE|re.IGNORECASE)
          for files in glob.glob("Recovered*"):
              data=open(files).read()
              if pattern.search(data):
                  newfilename = pattern.findall(data)[0].split("\\")[2] #get the new file name
                  try:
                      os.rename(files,os.path.join(folder,newfilename+".xml"))
                  except Exception,e:
                      print e
                  else:
                      print "%s renamed to %s.xml" %(files,newfilename)

          save the above as myscript.py and on command prompt
          Code: [Select]
          c:\test> python myscript c:\myfolder

          If you are interested, here's an executable (including sample recovered.xml files)
          usage:
          Code: [Select]

          C:\test>rename.exe c:\test
          Recovered_XML_1.XML renamed to PRM_AVENGING_ANGELO.xml
          Recovered_XML_2.XML renamed to PRM_AVENGING_MIKE.xml
          Recovered_XML_3.XML renamed to PRM_AVENGING_DURIAN.xml

          if you want also, here's a vbscript
          Code: [Select]
          Set objFS = CreateObject("Scripting.FileSystemObject")
          strFolder = "c:\test"
          Set objFolder = objFS.GetFolder(strFolder)
          Set regEx = New RegExp           
          regEx.Pattern = ".*<PATH_XML>(.*?)</PATH_XML>.*"
          regEx.IgnoreCase = True
          regEx.MultiLine = True
          For Each strFile In objFolder.Files
          strFileName = strFile.Name
          If InStr(strFileName ,"Recovered") >0 Then
          Set objFile=objFS.OpenTextFile(strFileName)
          strData = objFile.ReadAll
          Set objFile=Nothing
          Set colMatches = regEx.Execute(strData)
          For Each objMatch In colMatches
             strNew = Split(objMatch.Submatches(0),"\")
             strNewFile = strNew(2)
             strFile.Name = strNewFile & ".xml"
          Next
          End If
          Next

          save as myscript.vbs and on command prompt
          Code: [Select]
          c:\test> cscript /nologo myscript.vbs

          issux

            Topic Starter


            Starter

            thank you,
            this was very helpful, i have used the Python method...it was working great!!

            cheers,

            issux

            issux

              Topic Starter


              Starter

              if you have Python for Windows, its easy to do
              Code: [Select]
              import os,glob,sys,re
              folder=sys.argv[1]
              os.chdir(folder)
              pattern = re.compile(".*<PATH_XML>(.*?)</PATH_XML>.*",re.DOTALL|re.MULTILINE|re.IGNORECASE)
              for files in glob.glob("Recovered*"):
                  data=open(files).read()
                  if pattern.search(data):
                      newfilename = pattern.findall(data)[0].split("\\")[2] #get the new file name
                      try:
                          os.rename(files,os.path.join(folder,newfilename+".xml"))
                      except Exception,e:
                          print e
                      else:
                          print "%s renamed to %s.xml" %(files,newfilename)

              save the above as myscript.py and on command prompt
              Code: [Select]
              c:\test> python myscript c:\myfolder

              If you are interested, here's an executable (including sample recovered.xml files)
              usage:
              Code: [Select]

              C:\test>rename.exe c:\test
              Recovered_XML_1.XML renamed to PRM_AVENGING_ANGELO.xml
              Recovered_XML_2.XML renamed to PRM_AVENGING_MIKE.xml
              Recovered_XML_3.XML renamed to PRM_AVENGING_DURIAN.xml

              if you want also, here's a vbscript
              Code: [Select]
              Set objFS = CreateObject("Scripting.FileSystemObject")
              strFolder = "c:\test"
              Set objFolder = objFS.GetFolder(strFolder)
              Set regEx = New RegExp           
              regEx.Pattern = ".*<PATH_XML>(.*?)</PATH_XML>.*"
              regEx.IgnoreCase = True
              regEx.MultiLine = True
              For Each strFile In objFolder.Files
              strFileName = strFile.Name
              If InStr(strFileName ,"Recovered") >0 Then
              Set objFile=objFS.OpenTextFile(strFileName)
              strData = objFile.ReadAll
              Set objFile=Nothing
              Set colMatches = regEx.Execute(strData)
              For Each objMatch In colMatches
                 strNew = Split(objMatch.Submatches(0),"\")
                 strNewFile = strNew(2)
                 strFile.Name = strNewFile & ".xml"
              Next
              End If
              Next

              save as myscript.vbs and on command prompt
              Code: [Select]
              c:\test> cscript /nologo myscript.vbs


              hy gh0std0g74,

              i have used your python code to rename my files, it was very helpful, but it seams that my application stops reading the files from the folder, for an uncertain reason!
              what i have tried on one file, is to copy the content of this file into a new blank file and then relaunch my application, it worked! and the file was detected,
              therefor i have concluded that it seams the recovered files still have a bad flag in their file.
              so i am asking you, if it's possible to have another python script. that copy the content of file01.xml from C:\folder01 to another file01.xml inside C:\folder02, i still need to have the same name, but different file.
              i don't want to copy the file it self, only the content...

              your help is really appreciated...
              Regards,

              issux

              gh0std0g74



                Apprentice

                Thanked: 37
                well, then you should show what error you have, the input file that supposedly doesn't work for you, what your expected output you want...

                tharonald

                • Guest
                Hi Guys,

                Can somebody help me to replace the path in a itunes library file in xml format. I really don't understand the pattern stuff. I have to migrate iTunes library for + 900 users.

                Alle the paths looking at "d:\documents and settings\peter" must be changed to "c:\users\peter"

                The file looks like this:

                <?xml version="1.0" encoding="UTF-8"?>
                <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
                <plist version="1.0">
                <dict>
                   <key>Major Version</key><integer>1</integer>
                   <key>Minor Version</key><integer>1</integer>
                   <key>Application Version</key><string>10.1.1</string>
                   <key>Features</key><integer>5</integer>
                   <key>Show Content Ratings</key><true/>
                   <key>Music Folder</key><string>file://localhost/D:/Documents%20and%20Settings/peter/Start%20menu/Meijburg%20Programma's/iTunes/iTunes%20Media/</string>
                   <key>Library Persistent ID</key><string>EF4CE112D3994C31</string>
                   <key>Tracks</key>
                   <dict>
                      <key>77</key>
                      <dict>
                         <key>Track ID</key><integer>77</integer>
                         <key>Name</key><string>Alive</string>
                         <key>Artist</key><string>Pearl Jam</string>
                         <key>Album Artist</key><string>Pearl Jam</string>
                         <key>Composer</key><string>Eddie Vedder/Stone Gossard</string>
                         <key>Album</key><string>The Best of Pearl Jam</string>
                         <key>Genre</key><string>Alternative Rock</string>
                         <key>Kind</key><string>MPEG-audiobestand</string>
                         <key>Size</key><integer>13644359</integer>
                         <key>Total Time</key><integer>341088</integer>
                         <key>Track Number</key><integer>3</integer>
                         <key>Year</key><integer>1991</integer>
                         <key>Date Modified</key><date>2010-01-26T09:56:29Z</date>
                         <key>Date Added</key><date>2011-01-20T07:28:08Z</date>
                         <key>Bit Rate</key><integer>320</integer>
                         <key>Sample Rate</key><integer>48000</integer>
                         <key>Rating</key><integer>80</integer>
                         <key>Rating Computed</key><true/>
                         <key>Album Rating</key><integer>80</integer>
                         <key>Sort Album</key><string>Best of Pearl Jam</string>
                         <key>Persistent ID</key><string>5208515D81275036</string>
                         <key>Track Type</key><string>File</string>
                         <key>Location</key><string>file://localhost/D:/Documents%20and%20Settings/peter/My%20Documents/My%20Music/Pearl%20Jam/The%20Best%20of%20Pearl%20Jam/Alive.MP3</string>
                         <key>File Folder Count</key><integer>-1</integer>
                         <key>Library Folder Count</key><integer>-1</integer>
                      </dict>

                Sidewinder



                  Guru

                  Thanked: 139
                • Experience: Familiar
                • OS: Windows 10
                We sort of frown on hijacking someone's thread. We really frown on reviving a thread that is 18 months old, but other than that welcome to Computer Hope.

                Normally I would have used the XML object in VBScript for this, but the iTunes XML is either incomplete or not well formed. I took the brute force method instead.

                Code: [Select]
                Const ForReading = 1

                Set fso = CreateObject("Scripting.FileSystemObject")
                Set inFile = fso.OpenTextFile("c:\temp\iTunes.xml", ForReading)  'change drive:path/filename as necessary
                Set outFile = fso.CreateTextFile("c:\temp\iTunes.chg", True)     'change drive:path/filename as necessary

                While Not inFile.AtEndOfStream
                strData = inFile.ReadLine()
                strData = Replace(strData, "D:/Documents%20and%20Settings/peter", "c:/users/peter")
                outFile.WriteLine strData
                Wend

                inFile.Close
                outFile.Close

                Save the script with a VBS extension and run from the command line as cscript scriptname.vbs

                This is a good example of using the best tool for the job. Batch might have been used, but the payoff did not justify the effort.

                Good luck.  8)



                The true sign of intelligence is not knowledge but imagination.

                -- Albert Einstein