Hi.
i have a folder full of around 20 XML files that are created from a diagnostics program that does not provide a style sheet so i am adding one myself.
I am looking to create a Batch file that will open each XML, add the line "<?xml-stylesheet type="text/xsl" href="survey.xsl"?>" below the first "<?xml version="1.0" encoding="UTF-8"?>" line, then save the file, and move onto the next XML file and so on and so on until all the XML files have been edited and then stop.
The file names for the XML's would always remain the same, so if necessary the batch file could be told to run on a specific set of file names rather than operating on a "all files in a folder" statement if that helps?
Am a total beginner with these things and have only written very basic scripts so am a bit lost with this "open/add/save" one and thought it would be simple, but even begin to understand some of the examples i can find online.
Thanks in advance!
Open, Add Line, Save, Next File Help
Moderator: DosItHelp
Re: Open, Add Line, Save, Next File Help
Probably something about like that:
Note: Don't run twice with the same xml files. The code would just insert another line.
Steffen
Code: Select all
@echo off &setlocal EnableDelayedExpansion
for %%i in (*.xml) do (
<"%%~i" set /p "firstLine="
>"%%~i.~tmp" echo !firstLine!
>>"%%~i.~tmp" echo ^<?xml-stylesheet type="text/xsl" href="survey.xsl"?^>
>>"%%~i.~tmp" more +1 "%%i"
move /y "%%~i.~tmp" "%%i"
)
Steffen
Re: Open, Add Line, Save, Next File Help
Hi, thanks for the reply, that worked almost perfectly! The main file worked flawlessly, but, any idea why it did what it did in the two attached files??? it seems to have made a copy of the highlighted bits, either red to yellow or yellow to red, with the line i added in between?!?!!?
- Attachments
-
- testlog.xml
- (15.67 KiB) Downloaded 389 times
-
- errorlog.xml
- (436 Bytes) Downloaded 320 times
Re: Open, Add Line, Save, Next File Help
I tested the script with your files and it seems to just insert the line as expected. However, I have really no idea what you tried to tell about red to yellow and yellow to red. Did you mean the highlighting that your editor performs? I'm asking because highlighting is always specific to used editor. In my editor I don't even see any red or yellow highlights
Also, a notable property of your files is that they already contain line
<?xml-stylesheet type="text/xsl" href="survey.xsl"?>
but not as the second line.
Steffen
Also, a notable property of your files is that they already contain line
<?xml-stylesheet type="text/xsl" href="survey.xsl"?>
but not as the second line.
Steffen
Re: Open, Add Line, Save, Next File Help
I would expect such a result if your source files to not contain line endings at all or no line endings of the form "\r\n" (in hex "0D 0A").
On the other hand, that should have been preserved in your resulting files, which show the (windows) endline.
Do you have opened and saved the resulting files with an editor that might have replaced unusual line endings with "\r\n"?
penpen
On the other hand, that should have been preserved in your resulting files, which show the (windows) endline.
Do you have opened and saved the resulting files with an editor that might have replaced unusual line endings with "\r\n"?
penpen
Re: Open, Add Line, Save, Next File Help
I was going through a similar issue and this thread helped. thanks!
Re: Open, Add Line, Save, Next File Help
Hi Guys
just wanted to say a thanks for helping me with this! it's simple, but it got the job done
massive thank you
just wanted to say a thanks for helping me with this! it's simple, but it got the job done
massive thank you