I am wondering if there is any way to create a batch file that can edit a line in an XML document. The line would be identified by the preceding line. the idea would be as follows:
If line == Csetting name="BaseDirectory" serializeAs="String">
Next line = <value>User Input from begining of batch</value>
is something like that even posible or am I dreaming outside of my means? Thanks for the help and the answers.
You probably could hack something together in a batch file that works somehow. But it will be extraordinarily painful. First of all, I know of no way of reliably reading lines into variables in a batch file and writing them back to a file unaltered. You can escape most of the problematic characters (such as
<
,>
,&
,|
, ...) but there still are problems I couldn't solve1 (such as unmatched quotation marks) that will cause such attempts to fail horribly. Then you still wouldn't be able to parse XML but you'd rather to primitive text processing which may easily fail as soon as maybe single quotes are used instead of double quotes. Or an extra space is thrown in somewhere. Or the line you're looking for is split into several lines. All valid XML but painful to parse when no XML parser is around.The batch file language isn't really suited for such tasks. Heck, it barely works for text processing but XML is way beyond. You may have more luck (and fun) with using VBScript and MSXML or even PowerShell (if applicable).
VBScript is probably the most sane choice here as you can rely on it existing on virtually any modern Windows machine.
You could also use XSLT and call that from the command-line. There are enough XSLT processors out there that can be used and generating an XSLT file is actually much simpler (but will still require several escapings).
1 Note that I may be an advanced batch file user/programmer but by no means authoritative. Maybe it's easily possible and I'm just too stupid to see it.
I actually have an answer for this. Yes it is painful, however I had a similar problem and I don't actually know VBScript (though I am planning on learning it...) for the time though my problem occurred with a coworker having a customer with 20,000 files they flubbed from a conversion of outside data. All files were xml and they all were missing the same 2nd line of the XML which triggered a refile of the document we were importing.
I wrote a standard batch script in tandem with another I found on StackOverflow which allowed me to split the files into 2 parts and then in between them insert the code I wanted. Now my only problem (probably due to laziness or my lack of knowledge/patience) was that I couldn't escape the < , > problem. The script kept thinking that I was trying to write to a file, which was invalid. I tried all sorts of ways to use that character, but I wanted it in a variable form. Needless to say, I got it working (well even)...
Below is the readme I provided to my coworker, along with the code from each file.
README.txt Problem: Massive amount of files were missing a string or piece of code and need to be edited
Solution: This tools takes apart files and injects a string or piece of code and then put the files back together in another location.
There are a total of 4 files that come with this tool.
What you need to do:
Edit String_Insert_Launcher and place this file in the directory with the files you want to edit. NOTE It is imperative that this file be in the same folder as ALL of the rest of your files you want edited. You need to edit the variables in this file to match you filesystem batchpath
Edit InsertString.bat and place this file in the same directory you set the batchpath variable above You need to edit the variables in this file to match your filesystem insertpath destpath top_last_line insert_last_line bot_last_line
Edit the insert.txt and place this file in the same directory you set the insertpath above You need to put the string(s) you want to be inserted into your file inside this text document
Check your logs and make sure that the number of files in the " Modified_Filelist.txt " (found in the %insertpath%) is the same as the number of file you started with.
Breakdown of files:
* insert.txt *
Inside this file you will want to put the text that you want inserted into the files you will target. The reason for using a separate file is so that special characters (>,<,/,\,|,^,%,etc...) aren't treated like arguments within the batch file. This file HAS TO BE in the same location as the variable you will set in InsertString.bat called ' insertpath ' or referenced in the batch file as %insertpath%.
* InsertString.bat *
Inside this file you will find the variables that need to be set for the script to work. Variables included:
This file HAS TO BE in the same location as the variable you will set in String_Insert_Launcher.bat called ' batchpath ' or referenced in the batch file as %batchpath%.
* String_Insert_Launcher.bat *
This is the script you will execute to edit all the files. Launch this batch script FROM the folder with the files in it you want to edit. This file grabs all of the file names and runs the InsertString.bat ON all of these files. Inside this file you will find a varaible that nees to be set for the script to work. Variable included:
batchfilepath
- This is the location of the actual batch file that does all of the work. This location is JUST the filepath, not including any filenames.FILE #1: String_Insert_Launcher.bat
FILE #2: Insert_String.bat
FILE #3: Insert.txt
In your case you might be able to use 2 files...one with
<value>
and one with</value>
(I know this is sloppy, but it will work...) Then from my batch script InsertString.bat you would just put the :next loop 2x (one for each of your files) and in between them you would put echo.%userInputFromBeginningofBatch% >> File.xmlLike I said, I know this is messy and you can doe it a lot easier in VBScript, but for those of us that don't know it this is a solution that does work.
XML isn't line-based, so an assumption that you can look for something in the file by checking it on a line-by-line basis, is either prone to problems, or relies on other assumptions besides XML. (if you are getting your file from a certain type of software, how do you know it is always going to produce output lines in that particular way?)
Having said that, I'd take a look at JSDB Javascript, which has E4X built-in. E4X makes it particularly simple to manipulate XML, as long as you can read it all into memory; it's not a stream-based system. Though you could use JSDB without E4X and handle file I/O using streams:
Excuse me. I apologize in advance for this post. I know this is a very old topic, but after read the answers here I couldn't resist the temptation to post this answer.
The processing of a XML file via a Batch program is not just straightforward and direct, but in my humble opinion, easier than any equivalent solution in VBScript, PowerShell, etc. Here it is:
The only problem with previous program is that it delete empty lines from the XML file, but this detail may be fixed in a very easy way by adding a couple commands more. Previous program may be modified to not check the complete line (as the OP originally requested), but check three parts in the same way of the last VBScript example:
sure, natively, you can use batch, but i recommend you to learn and use vbscript instead
save the script as edit.vbs and in your batch
vbscript is the best you got besides cripple batch, if you hate the idea of using other tools like gawk/sed/Python/Perl or other xml parsers/writers. Otherwise, you should consider using these better tools.