Find string in line, match with other string [SOLVED]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
newklear
Posts: 7
Joined: 20 Aug 2014 08:06

Find string in line, match with other string [SOLVED]

#1 Post by newklear » 20 Aug 2014 08:39

Hello Everyone

I have been tasked with this at work and have had a crash course in batch scripting over the 2 days, I am not a developer or scripter but am keen to learn so please bare with me. I am almost complete in achieving what I need to do, please kindly help.

Problem
I need to find a string in a line contained in an XML file using inputs from an INI file. When I find that string, I need to find another string in that same line and rename the second string with the name of the first string, then copy that physical file from the first string into a different folder pre-determined by settings in the INI file. Then lastly to rename the second string's file name with that of the first strings filename. Hope that makes sense.

Here is my batch script:

Code: Select all

title [ + MAIN MENU + "x" TO EXIT ]
cls

set /p answer=Press 1 to Begin:
if /i "%answer%"=="1" goto Begin
if /i "%answer%"=="x" goto eof

:: FIRST SECTION
:Begin
Set inifile="settings.ini"
FOR /F "delims== tokens=2* usebackq" %%i in (`type %inifile% ^| find "xmlPath="`) do (set xmlpath=%%i)
echo %xmlpath% > xmlpath.txt

FOR /F "delims== tokens=2* usebackq" %%i in (`type %inifile% ^| find "fileinXml="`) do (set fileinxml=%%i)
echo %fileinxml% > fileinxml.txt

fart.exe %xmlpath% %fileinxml%
:: As above I used a 3rd party app to 1. find the file, 2. find the matching text, just not sure how to find the 2nd string, please see below.

::I AM STUCK HERE, I can find MENU2.JPG but need to grab the data by [b]LocalFilename[/b] which would look like this
:: [quote]<File FileId="117765" FileName="MENU2.jpg" LocalFilename="117765" Hash="968e9cb2b38b2161f350903afe9040b7" Size="96690" Type="application/octet-stream" />[/quote]

:: Then once I am able to grab the data from that 2nd string [i]LocalFilename[/i] then I can copy the MENU2.jpg to the required destination and rename the previous match.

:eof
endlocal
exit


This is my settings.ini file

Code: Select all

xmlPath=C:\campaigns\16914\document.xml
fileinXml=C:\campaigns\16914\MENU2.jpg
destination=C:\campaigns\16922\


Hope this question makes sense and that I have presented enough information. I appreciate any assistance. Thank You.
Last edited by newklear on 21 Aug 2014 08:58, edited 2 times in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find string in line, match with other string and change

#2 Post by foxidrive » 20 Aug 2014 08:58

Your description isn't very clear - Please supply actual examples of what you need to do.

newklear
Posts: 7
Joined: 20 Aug 2014 08:06

Re: Find string in line, match with other string and change

#3 Post by newklear » 20 Aug 2014 09:21

Hello foxidrive, thank you for your prompt reply.

Let me try explain in a better way.

I have this INI file.

Code: Select all

xmlPath=C:\campaigns\16914\document.xml
fileinXml=C:\campaigns\16914\MENU2.jpg
destination=C:\campaigns\16922\


1. xmlPath is the XML file that needs to be edited.
2. fileinXml is my search text, i.e I am searching with the line that contains "MENU2.jpg"
3. In the same line that MENU2.jpg is there is the digitized filename "LocalFilename"
4. Example:
<File FileId="117765" FileName="MENU2.jpg" LocalFilename="117765" Hash="968e9cb2b38b2161f350903afe9040b7" Size="96690" Type="application/octet-stream" />

5. I need to copy the 117765 file to the destination folder listed in the INI file then rename that "117765" to "MENU2.jpg" both in the XML file as well as the actual file.

There are a few more things needed to be done, but if I can get the above achieved the rest should be straight forward.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find string in line, match with other string and change

#4 Post by foxidrive » 20 Aug 2014 10:04

Test this: it changes LocalFilename= but not FileId=

This uses a helper batch file called `repl.bat` (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.

Code: Select all

@echo off
set "inifile=settings.ini"

FOR /F "tokens=1* delims==" %%a in ('find /i "xmlPath="     ^< "%inifile%" ') do set "xmlpath=%%b"
FOR /F "tokens=1* delims==" %%a in ('find /i "fileinXml="   ^< "%inifile%" ') do set "fileinXml=%%~nxb"
FOR /F "tokens=1* delims==" %%a in ('find /i "destination=" ^< "%inifile%" ') do set "destination=%%b"


for /f tokens^=2^,4^,6delims^=^" %%a in (' find /i "%fileinXml%" ^< "%xmlpath%" ') do (
  echo copying "%%c" to "%destination%\%fileinXml%"
  copy "%%c" "%destination%\%fileinXml%"
  type "%xmlpath%" |repl "(.*FileName=\q%fileinXml%\q.*LocalFilename=\q).*?(\q.*)" "$1%fileinXml%$2" xi >"%temp%\xmltmp.tmp"
  move "%temp%\xmltmp.tmp" "%xmlpath%"
)
pause

newklear
Posts: 7
Joined: 20 Aug 2014 08:06

Re: Find string in line, match with other string and change

#5 Post by newklear » 21 Aug 2014 01:34

Thank You very much for your help foxidrive.

I ran the script as is and noticed that it over writes the document.xml with
copying "117765" to "C:\bluchip\campaigns\16914\MENU2.jpg"
The system cannot find the file specified.
0 file(s) moved.
Press any key to continue . . .


and in console it displays this:
copying "" to "C:\bluchip\campaigns\16914\MENU2.jpg"
The system cannot find the path specified.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
1 file(s) moved.
Press any key to continue . . .

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find string in line, match with other string and change

#6 Post by foxidrive » 21 Aug 2014 04:23

newklear wrote:I ran the script as is and noticed that it over writes the document.xml with
copying "117765" to "C:\bluchip\campaigns\16914\MENU2.jpg"
The system cannot find the file specified.
0 file(s) moved.
Press any key to continue . . .



Firstly, the file "117765" does not exist in the current folder.

Sort that issue out first.

newklear
Posts: 7
Joined: 20 Aug 2014 08:06

Re: Find string in line, match with other string and change

#7 Post by newklear » 21 Aug 2014 04:40

foxidrive wrote:
newklear wrote:I ran the script as is and noticed that it over writes the document.xml with
copying "117765" to "C:\bluchip\campaigns\16914\MENU2.jpg"
The system cannot find the file specified.
0 file(s) moved.
Press any key to continue . . .



Firstly, the file "117765" does not exist in the current folder.

Sort that issue out first.


Sorry foxidrive, you were right, I changed folder location for testing as my document.xml was been over written.
I put the "117765" file in the correct folder.

1. The "117765" file did copy into the intended folder and was renamed MENU2.jpg - Thank You for that
2. The document.xml still gets over written with the lines:
copying "117765" to "C:\Users\Terrick\Documents\BluchipMediaWorkFiles\Projects\Primedia\BLUMEDIA3\Test\MENU2.jpg"
1 file(s) copied.
0 file(s) moved.


Press any key to continue . . .


Thank you for the help thus far.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find string in line, match with other string and change

#8 Post by foxidrive » 21 Aug 2014 05:41

Did you download repl.bat and put it in the same folder?

Test it again because your screen output says "0 files moved" so it did not overwrite the file at that time.

newklear
Posts: 7
Joined: 20 Aug 2014 08:06

Re: Find string in line, match with other string and change

#9 Post by newklear » 21 Aug 2014 08:57

foxidrive wrote:Did you download repl.bat and put it in the same folder?

Test it again because your screen output says "0 files moved" so it did not overwrite the file at that time.


Wooohooo, thank you so much for your help and patience foxidrive. File moved and copied. There are a few more things I need to do, but this has been a great help, you are a genius!

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Find string in line, match with other string and change

#10 Post by Squashman » 22 Aug 2014 06:12

newklear wrote:
foxidrive wrote:you are a genius!

Don't give him a big head. It might explode because he walks the Earth upside down!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find string in line, match with other string and change

#11 Post by foxidrive » 22 Aug 2014 06:31

Squashman wrote:
newklear wrote:
foxidrive wrote:you are a genius!

Don't give him a big head. It might explode because he walks the Earth upside down!


:) We are well known to live 'down under' as in the song https://www.youtube.com/watch?v=XfR9iY5y94s

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Find string in line, match with other string and change

#12 Post by Squashman » 22 Aug 2014 07:08

foxidrive wrote::) We are well known to live 'down under' as in the song https://www.youtube.com/watch?v=XfR9iY5y94s

I didn't even have to click that link to know that is what it was! :lol:

newklear
Posts: 7
Joined: 20 Aug 2014 08:06

Re: Find string in line, match with other string [SOLVED]

#13 Post by newklear » 22 Aug 2014 11:43

Love that song lol :-)

"Where women glow and men plunder" - Ahh so you guys are miners :D

Post Reply