Read a xml file and copy a perticular text to a new file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

Read a xml file and copy a perticular text to a new file

#1 Post by loc3loan » 14 Nov 2014 08:54

Hello all,

Could someone please give me a guidance of how to write a batch file that would read an xml file, and then copy the text that in bewtween <label>...........</lablel>, and then write it to a new file? Thank you so much for your help.

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

Re: Read a xml file and copy a perticular text to a new file

#2 Post by Squashman » 14 Nov 2014 09:50

loc3loan wrote:Hello all,

Could someone please give me a guidance of how to write a batch file that would read an xml file, and then copy the text that in bewtween <label>...........</lablel>, and then write it to a new file? Thank you so much for your help.

Well you could search the forums as this questioned has been asked before. But without seeing an example of the real file most people will be gun shy to start coding a script for you.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Read a xml file and copy a perticular text to a new file

#3 Post by Yury » 14 Nov 2014 12:22

Code: Select all

@echo off
 
set "in=D:\Test\example.xml"
set "out=D:\Test\new.txt"
set "from=^<label^>"
set "to=^</lablel^>"
 
type "%in%">.tmp
mshta "vbscript:CreateObject("Scripting.FileSystemObject").GetStandardStream(1).Write(Split(Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(".tmp").ReadAll(),"%from%")(1),"%to%")(0))&Close()">"%out%"
del .tmp
 
exit /b

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

Re: Read a xml file and copy a perticular text to a new file

#4 Post by foxidrive » 14 Nov 2014 13:55

XML files come in different flavours, and this will work for some of them:

Code: Select all

type "file.xml" | repl ".*<label>(.*?)</label>.*" "$1" a >"newfile.txt"


This uses a helper batch file called `repl.bat` (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.

(There is a recently updated version of REPL.BAT on this forum - am waiting to see if any further changes will be made, but the one above will work)

Post Reply