|
Answer» Hi, I am looking for a way to SEARCH+REPLACE a string between two XML tags (see below), with a different string, using DOS BATCH.
Scenario: I have a .XML file (exported from TaskSched) that has the following lines: Code: [Select] <UserId>COMPUTERNAME\USERNAME</UserId>Code: [Select] <Actions Context="Author"> <Exec> <Command>D:\subfolder1\subfolder2\filename1.bat</Command> <WorkingDirectory>D:\subfolder1\subfolder2</WorkingDirectory> </Exec> <Exec> <Command>D:\subfolder1\subfolder2\filename2.bat</Command> <WorkingDirectory>D:\subfolder1\subfolder2</WorkingDirectory> </Exec> </Actions>Of note, the following things need to be changed: COMPUTERNAME\USERNAME with variable values contained in %COMPUTERNAME% and %USERNAME% , and the drive letter (in this case D ) to whatever the current drive letter is that the BATCH is executed from, for example C.You would be better off not using batch, because this is impossible in vanilla batch.Luigi, maybe you should restrict yourself to answering questions where you actually know something? Such an answer as that is 1. Unhelpful 2. Untrue. Using nothing but built-in batch commands, the task is easy.
1. Batch file
@echo off setlocal enabledelayedexpansion set driveletter=%~d0 echo Batch LOCATED on drive %driveletter% echo Computer name %computername% echo User name %username% if exist output.xml DEL output.xml for /f "delims=" %%A in (input.xml) do ( set thisline=%%A set "newline=!thisline:COMPUTERNAME\USERNAME=%computername%\%username%!" set "newline=!newline:D:\=%driveletter%\!" echo Input !thisline! echo Output !newline! echo !newline! >> output.xml echo. ) echo Complete Pause
2. Input.xml
<UserId>COMPUTERNAME\USERNAME</UserId> <Actions Context="Author"> <Exec> <Command>D:\subfolder1\subfolder2\filename1.bat</Command> <WorkingDirectory>D:\subfolder1\subfolder2</WorkingDirectory> </Exec> <Exec> <Command>D:\subfolder1\subfolder2\filename2.bat</Command> <WorkingDirectory>D:\subfolder1\subfolder2</WorkingDirectory> </Exec> </Actions>
3. Output.xml
<UserId>PEGASUS\user901</UserId> <Actions Context="Author <Exec> <Command>C:\subfolder1\subfolder2\filename1.bat</Command> <WorkingDirectory>C:\subfolder1\subfolder2</WorkingDirectory> </Exec> <Exec> <Command>C:\subfolder1\subfolder2\filename2.bat</Command> <WorkingDirectory>C:\subfolder1\subfolder2</WorkingDirectory> </Exec> </Actions>
4. Batch console display
Batch located on drive C: Computer name PEGASUS User name user901 Input <UserId>COMPUTERNAME\USERNAME</UserId> Output <UserId>PEGASUS\user901</UserId>
Input <Actions Context="Author"> Output <Actions Context="Author
Input <Exec> Output <Exec>
Input <Command>D:\subfolder1\subfolder2\filename1.bat</Command> Output <Command>C:\subfolder1\subfolder2\filename1.bat</Command>
Input <WorkingDirectory>D:\subfolder1\subfolder2</WorkingDirectory> Output <WorkingDirectory>C:\subfolder1\subfolder2</WorkingDirectory>
Input </Exec> Output </Exec>
Input <Exec> Output <Exec>
Input <Command>D:\subfolder1\subfolder2\filename2.bat</Command> Output <Command>C:\subfolder1\subfolder2\filename2.bat</Command>
Input <WorkingDirectory>D:\subfolder1\subfolder2</WorkingDirectory> Output <WorkingDirectory>C:\subfolder1\subfolder2</WorkingDirectory>
Input </Exec> Output </Exec>
Input </Actions> Output </Actions>
Complete Press any key to continue . . .Quote from: Luigi master on June 28, 2016, 10:06:42 AM You would be better off not using batch, because this is impossible in vanilla batch.
Your Posts will now be moderated from here out before they show up here...
patio.Luigi Master is a 16 year old gal, it seems. It's nice to see gals doing batch stuff but you need to read a bit more widely Luigi Master, to learn a bit more about this and that. You didn't make a typo about your gender there, did you?
PENchaotic, is COMPUTERNAME\USERNAME actual literal text, or someone's details?
Quote from: PENchaotic on June 28, 2016, 09:32:33 AMScenario: I have a .XML file (exported from TaskSched) that has the following lines: Code: [Select] <UserId>COMPUTERNAME\USERNAME</UserId> The line can be recreated completely in either case, and I'm only commenting that it may be variable text.Age nor gender have any bearing...seeing innaccurate advice over time is the deciding factor...I trawled through his, erm I mean her, posts before commenting, patio: I know exactly what you mean.
My waffle wasn't meant to question any decisions - I'll avoid commenting in response to forum matters, as I should have done here.
I wasn't sure how to phrase my point that Luigi didn't seem to be a gal. MEA culpa. Luigi is a boy's name, and 'master' is a male title.
|