Insert directory name into xml file

Microsoft / Dos
July 7, 2010 at 16:26:04
Specs: Windows XP
Christmas eve last year there was a posting entitled "Insert Filename into file using find and repl". This comes close to meeting my requirements:
All my files are .xml.
I need to insert just the directory name in which the file resides, into each each .xml file in their respective directories.
The insertion is in the middle of a line.
There are 2 instances in each .xml file with different text that the directory name has to be appended too.

See More: Insert directory name into xml file

Report •


#1
July 7, 2010 at 17:13:54
Is THIS the Christmas Eve 2009 post you speak of?

i_Xp/Vista/W7User


Report •

#2
July 8, 2010 at 00:02:40
Yes.
That's the one.
Its obvious where to change the file type:
From: for /f "tokens=* delims=" %%b in ('dir /s /b *.HTM*') do
To: for /f "tokens=* delims=" %%b in ('dir /s /b *.xml) do
The next step is extracting the directory name to be inserted. For example, all the folders reside within a parent directory i.e. C:\Files, like C:\Files\List1, C:\Files\List2, etc. so all I want inserted is List1, List2, Listx, etc. I cannot work out how that is done.
Similarly it's not so easy to correctly modify the text string to search on.
In my .xml files the details are formated for ease of reading so there are leading spaces on some lines.
Here is an extract:
<call service="SoftwareServices" method="generateVersion">
<param name="xmlConfig">
<![CDATA[
<Software ModuleType="user.mode.production" ModuleName="bcr" Version="6.11.4">
<Filename>bcr.zip</Filename>
<Description>bcr</Description>
<Type>1</Type>
<EffectiveDate>2000-01-01T00:00:00.0000000+00:00</EffectiveDate>
<ExpirationDate>2030-01-01T00:00:00.0000000+00:00</ExpirationDate>
</Software>
]]>
</param>
<attachment file="files\user.mode.production\bcr.zip" />
</call>
</webcall>

There are 2 instances of 'user.mode.production' which need to be prepended with the holding directory name, i.e. 'List1.user.mode.production'.


Report •

#3
July 8, 2010 at 19:17:52
this might work:
for /f "tokens=* delims=" %%a in ('dir /s /b *.xml) do
set xx=%%~pa.
for /f "tokens=*" %%b in ("!xx!") do set parent=%%~nxb
echo fullpath: !xx!, parent: !parent!, file: %%~nxa
)
::------ end snippet

Report •

Related Solutions

#4
July 9, 2010 at 00:16:04
Thanks for the snippet but what do I do with it?
What does it do?
Should it replace an extract in the batch file you provided on 26/12/2009?
How should I specify the text to search on?

Report •

#5
July 9, 2010 at 18:04:44
i went back and looked at my earlier script, and decided it wasn't quite right for the job, so I reworked the whole theme
(involving a lot more effort than i first imagined due to attempting to preserve the exclamations)

EDITED: renamed file "temp" to be "tempfile",
located in any convenient directory - I used a made-up "\work\" to represent such a directory.

EDIT AGAIN! might have gone a tad overboard with the debugging, but better too much than not enough! ;-)
@echo off & setlocal disabledelayedexpansion
for /f "tokens=*" %%a in ('dir /b /s /a-d *.xml') do (
:: first, get the parent directoryname
set xx=%%~dpa.
echo %xx%
set fil="%%a"
call :bigloop
)
goto :eof
:bigloop
:: finish getting the parentname
for /f "tokens=*" %%b in ("%xx%") do set parent=%%~nxb& set fil="%%a"
echo file: %fil%, parent directory: %xx%
:: build list of the linenumbers of lines that have the target-string
for /f "tokens=1 delims=[]" %%b in ('find /n /i "user.mode.production" ^< %fil%') do set line%%b=%%b

:: now, process the file itself. Use any convenient existing directory to hold "tempfile"
echo clearing workfile:
echo off > \work\tempfile
echo \work\tempfile has been cleared for the above file. it should be empty at this point...
type \work\tempfile
pause
set cc=0
:: "find" is used to preserve the format of blank lines and leading spaces etc.
echo file %fil% content being iterated:
for /f "tokens=1* delims=[]" %%b in ('find /v /n ""^<"%fil%"') do call :replecho
::foll line disabled for safety-check. enable when ready for live operation
echo MOVE command content: move /y \work\tempfile %fil%
::move /y \work\tempfile %fil%
goto :eof

:replecho
set /a cc+=1
if not defined line%cc% (
:: a "regular" line
for %%d in (1) do >> \work\tempfile echo.%%c & echo.%%c
) else (
:: a "target" line, do the insertion
echo injection of %parent% at line %cc%
set line%cc%=
for %%d in (1) do set zz="%%c"
setlocal enabledelayedexpansion
set zz=!zz:user.mode.production=%parent%.user.mode.production!
>> \work\tempfile echo !zz:~1,-1!
endlocal disabledelayedexpansion
)
::--------- end
note that if any of your target lines have exclms, the script will remove them.
Had i known how this script would kick my ass, i would have used vbscript (much easier!)


Report •

#6
July 11, 2010 at 14:32:37
Much better and a lot closer to what I am trying to achieve.

Having created \work\temp folders things appear to be getting a lot closer but not quite there yet:
For each .xml file (of which there are 9) there is an "Access is denied." remark (9 remarks) in the Command Prompt Window. Presumably it is unable to write the revised files into the \work\temp folder.
Also in the folder containing the batch file (C:\files) there appears a new file called "temp". This file contains all 9 modified .xml files. And each time the batch is re-run the "temp" file grows with another 9 entries.

Hopefully it is a simple step to remedy this inability to write the contents of the "temp" file to the \work\temp folder.


Report •

#7
July 11, 2010 at 14:39:56
oooooops!!! Sorry, your last sentence red-flagged the whole issue:
"Hopefully it is a simple step to remedy this inability to write the contents of the "temp" file to the \work\temp folder."

that says it right there. just change the name of either the directory or the file. Obviously, they can't both be "temp".
maybe make the dir.name "work" and the file "Temp" or vice-versa. I'll fix it in resp.#5.


Report •

#8
July 11, 2010 at 15:30:16
Sorry that has confused things.
Now there are 250 "Access is denied." remarks and no sign of the "temp" file anymore.

Report •

#9
July 11, 2010 at 18:41:14
if you can, use a good programming language instead of batch to process xml files. Eg Python.

import os
import fileinput
root=os.path.join("C:\\","test")
for r,d,f in os.walk(root):
   for file in f:      
      if os.path.splitext(file)[-1].lower() in [".htm",".html"]:
         filename=os.path.join(r,file)
         folder=os.path.split(r)[-1]
         F= fileinput.FileInput(filename,inplace=0)
         for line  in F:
            if "<Software ModuleType=" in line:
               line=line.replace("user.mode.production",folder)
            print line  
            

And if you want advanced xml processing, you can use Python's XML modules.

Use batch for simple tasks. for anything more complex, use a programming language.

GNU win32 packages | Gawk


Report •

#10
July 11, 2010 at 19:37:13
AAAAy-MEN! vbscript is highly recommended as well.

Report •

#11
July 11, 2010 at 21:27:17
yes, vbscript too, although code is not portable to *nix, still better than using batch.

GNU win32 packages | Gawk


Report •

#12
July 12, 2010 at 05:06:26
I am not a programmer and I do not want to purchase any programming tools, I just want a simple routine that will complete a repetitive task that occurs every 4 to 6 weeks.
A batch or script seemed the obvious answer. Windows Script sorted out the folder replication for me but does not do document manipulation and the DOS batch looked like it was capable of doing what I required.
If you can offer a simpler, better alternative then I will be very grateful.

Report •

#13
July 12, 2010 at 07:53:31
why do you think you need to purchase these programming tools? they are free to download and use.

GNU win32 packages | Gawk


Report •

#14
July 12, 2010 at 07:58:14
Newbies say the most stupid things.
Hopefully it gives you something to smile about.

Thanks for the advice.

I will take a look at Python.


Report •


Ask Question