DOS Read, parse, then write to a file -


i have need read in filename each sub folder, find string looking for, write text file. filename each sub folder same.

edit: data structure of file...it xml files tags in them. want search these tags below, , write them text file.

here's snippet data (there multiple track tags w/in file:

             <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> 

i use "find" string. have...

@echo off   set filename=metadata.txt  /f %%d in ('dir /b /a:d') (  (find /i "<artist_name>" "%%d\%filename%") (find /i "<title>" "%%d\%filename%") (find /i "<track_number>" "%%d\%filename%")  ) 

how assign variables "artist_name", "title", , "track_number", echo/print each variable file result looks like

artist_name, title, track_number (comma delimited, example)

thanks in advance!

@echo off setlocal set filename=metadata.xml  ( /f %%d in ('dir /b /a:d') if exist "%%d\%filename%" ( call :process "%%d\%filename%" ) )>result.txt goto :eof  :process call :filter %1 art  artist_name call :filter %1 titl title call :filter %1 trak track_number echo "%art%","%titl%","%trak%" goto :eof  :filter /f "delims=" %%i in ('type %1 ^|findstr /c:"<%3>"') (set %2=%%i) call set "%2=%%%2:*%3=%%" call set "%2$=%%%2:*/%3=%%xx%3" call set "%2=%%%2:~1%%" :lop call set "%2=%%%2:~0,-1%%" call set "%2$=%%%2$:~1%%"&if defined %2$ goto lop goto :eof 

completely replaced code 20130423-0805z

caught in delayed-expansion trap.

running in test system has on 300 subdirectories. simplification , transfer didn't quite work...

this time sure...

make sure file exists in subdirectory.

filter out file (art titl trak) contents between tags (artist_name title track_number) display results

  • results redirected output file 'result.txt'

the :filter routine looks string supplied third argument ("tag") :filter, within <..> , assigns result variable given second :filter argument ("var")

  • var assigned %var:*tag=% strips end of tag string var
  • next, var$ assigned %var:*/tag%xxtag following /tag (in case more data follows /tag) + tag again + 2 characters (xx=the / , <)
  • the leading ">" removed var
  • a character lopped off of end of var , var$ (which initialised "anystraydataattheendoftheclosetagxxtag") until var$ empty, removes close-tag , data.


ah- multiple track data per file.

the >report.txt sends results file rather screen. entirely optional

@echo off setlocal set filename=%1&if not defined filename echo syntax:%~n0 startingdirname&goto :eof  pushd "%~1" set filename=metadata.xml  ( /f %%d in ('dir /b /a:d') if exist "%%d\%filename%" ( set skipto=1 call :process "%%d\%filename%" ) )>report.txt popd goto :eof  :process %%i in (art titl trak) set "%%i=" call :filter %1 art  artist_name if not defined art goto :eof call :filter %1 titl title call :filter %1 trak track_number echo "%art%","%titl%","%trak%" goto process  :filter /f "skip=%skipto% delims=" %%i in (   'type %1^|findstr /n "$"') if not defined %2 (   echo "%%i"|findstr /c:"<%3>" >nul   if not errorlevel 1 (    set "%2=%%i"    /f "delims=:" %%l in ("%%~i") set skipto=%%l    )   ) ) if not defined %2 goto :eof  call set "%2=%%%2:*%3=%%" call set "%2$=%%%2:*/%3=%%xx%3" call set "%2=%%%2:~1%%" :lop call set "%2=%%%2:~0,-1%%" call set "%2$=%%%2$:~1%%"&if defined %2$ goto lop goto :eof 

revised version.

processes each file found in :process after setting skipto 1 - clear variables used - try find art using tag - if found, repeat titl , trak, output , try again. - if not found, exit , go on next file.

the filter procedure - looks @ each line in source, numbers each line linenumber: first. first %skipto% lines skipped. - finds first line after lines skipped contains target tag - sets variable contents of entire, numbered line , grabs portion of numbered line to, not including first colon , puts skipto - other matching lines ignored because variable defined.

if variable not set in routine, there no further data items after skipto line, otherwise when next @ file, know we've processed far skipto line, next variable assigned next data item, skipping previously-found items.

(edit 20130425-1640z added pushd/popd change target relative root)


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -