Open, Add Line, Save, Next File Help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dannyproc
Posts: 4
Joined: 05 May 2022 04:10

Open, Add Line, Save, Next File Help

#1 Post by dannyproc » 05 May 2022 04:24

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! :)

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Open, Add Line, Save, Next File Help

#2 Post by aGerman » 05 May 2022 11:04

Probably something about like that:

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"
)
Note: Don't run twice with the same xml files. The code would just insert another line.
Steffen

dannyproc
Posts: 4
Joined: 05 May 2022 04:10

Re: Open, Add Line, Save, Next File Help

#3 Post by dannyproc » 06 May 2022 02:59

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 244 times
errorlog.xml
(436 Bytes) Downloaded 190 times

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Open, Add Line, Save, Next File Help

#4 Post by aGerman » 06 May 2022 09:38

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

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Open, Add Line, Save, Next File Help

#5 Post by penpen » 12 May 2022 04:46

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

Andrew92
Posts: 8
Joined: 26 Apr 2022 09:15

Re: Open, Add Line, Save, Next File Help

#6 Post by Andrew92 » 20 May 2022 15:57

I was going through a similar issue and this thread helped. thanks!

dannyproc
Posts: 4
Joined: 05 May 2022 04:10

Re: Open, Add Line, Save, Next File Help

#7 Post by dannyproc » 23 May 2022 11:16

Hi Guys

just wanted to say a thanks for helping me with this! it's simple, but it got the job done :D

massive thank you

Post Reply