Announcement

Collapse
No announcement yet.

Read, parse, then write to a file

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Read, parse, then write to a file

    I have to need to read in a filename from each sub folder, find a string I am looking for, then write to a text file. The filename from each sub folder is always the same.

    I use "find" to look for the string. This is what I have...

    Code:
    @ECHO OFF
    
    	
    SET filename=metadata.txt
    
    FOR /F %%D in ('dir /b /a:D') DO (
    
    (find /i "<artist_name>" "%%D\%filename%")
    (find /i "<title>" "%%D\%filename%")
    (find /i "<track_number>" "%%D\%filename%")
    
    )
    How do I assign variables for "artist_name", "title", and "track_number", then echo/print each variable to a file so that the result looks like

    artist_name, title, track_number (comma delimited, for example)

    Thanks in advance!

  • #2
    Re: Read, parse, then write to a file

    Please, write an example of metadata.txt file.

    Comment


    • #3
      Re: Read, parse, then write to a file

      Dragokas, thanks for your reply.

      As for the data structure of the file...it is a XML files with TAGS in them. I just want to search for these tags below, and write them to a text file.

      Here's a snippet for the data (there are multiple track tags w/in the file):
      Code:
                   <track>
                    <artists>
                      <artist>
                          </read_only_info>
                          <primary>true</primary>
                          <language>en</language>
                          <artist_name>Kim Wilde</artist_name>
                          <roles>
                              <role>Primary</role>
                          </roles>
                      </artist>
                  </artists>
                  <isrc>USMC18441630</isrc>
                  </audio_file>
                  <title>The Second Time</title>
                  <locales/>
                  <track_number>8</track_number>
               </track>

      Comment


      • #4
        Re: Read, parse, then write to a file

        This will be very hard using batch. I can show you an easy way to do this in VBScript. Is that an option for you?
        Jay Adams, Noxigen LLC

        Comment


        • #5
          Re: Read, parse, then write to a file

          noxigen, sure...glad to learn something new.

          Comment


          • #6
            Re: Read, parse, then write to a file

            Here you go. Just save this as SomeName.vbs:


            Code:
            xmlFile = WScript.Arguments(0)
            
            outputDelimiter = ","
            artistDelimiter = "|"
            
            Set xmlDoc = CreateObject("Msxml2.DOMDocument")
            xmlDoc.async = False
            xmlDoc.load(xmlFile)
            
            Set tracks = xmlDoc.selectNodes("//track")
            
            For Each track In tracks
            	allArtists = ""
            	
            	Set artists = track.selectNodes("./artists/artist")
            	For Each artist In artists
            		
            		Set artist_name = artist.selectSingleNode("./artist_name")
            		allArtists = allArtists & artist_name.text & artistDelimiter
            	Next
            	
            	allArtists = Left(allArtists, Len(allArtists)-Len(artistDelimiter))
            	
            	Set title = track.selectSingleNode("./title")
            	Set track_number = track.selectSingleNode("./track_number")
            	
            	WScript.Echo allArtists & outputDelimiter & title.text & outputDelimiter & track_number.text
            Next
            Then change your Batch file to something like this:

            Code:
            @ECHO OFF
            
            	
            SET filename=metadata.txt
            
            FOR /F %%D in ('dir /b /a:D') DO (
            
            cscript.exe /nologo SomeName.vbs "%%D\%filename%"
            
            )
            Let me know how it works out for you!
            Last edited by noxigen; 16th June 2013, 16:01. Reason: typo
            Jay Adams, Noxigen LLC

            Comment


            • #7
              Re: Read, parse, then write to a file

              To do this in batch, you'd have to do something like this to start:
              Code:
              set file=%temp%\%random%.txt
              findstr /I "<artist_name>" "Z:\file.txt" > !file! & set /p A1=<!file! & echo !A1!
              findstr /I "<title>" "Z:\file.txt" > !file! & set /p A2=<!file! & echo !A2!
              findstr /I "<track_number>" "Z:\file.txt" > !file! & set /p A3=<!file! & echo !A3!
              del /f/q !file!
              Then what I would do is use a for loop and delimit by ">", attempt to grab the value at token 2, and get rid of everything starting with the first "<" to get the value itself.

              I could do it, but if someone has already posted a VBScript solution then that works.

              Comment

              Working...
              X