Sept
Sept

Reputation: 3

Using batch to read a certain section of a file and create a variable

I've read a lot of things and I can't find away of doing this that I understand. This might be because I'm a wee newbie though.

What I'm trying to do is:

I have a file called start.txt with the contents

..\dll | toapp.exe "..\posdata\filename.xml" "..\OUT" "..\TEMP" 

I want to read the contents of that in a batchfile and take only filename.xml as a variable. From device to device inside the network it changes.

I'm then going to use that variable to copy a file from 1 machine to another but I only want that one file and I can't be sure what it's called without looking in the start.txt. I can do all the copy and checks to make sure it's looking in the correct places but just not the findstr section.

Any idea of what to do to understand would be fantastic

Upvotes: 0

Views: 78

Answers (2)

npocmaka
npocmaka

Reputation: 57272

@echo off
set "start_file=start.txt"
for /f tokens^=5^ delims^=\^" %%# in ('type "%start_file%"^|find /i "..\posdata"') do set "xml_file=%%#"

echo %xml_file%

Your question is a little bit unclear.Is this the only line in the file? Is this the exact content? How can I recognize the line with the file?

The script above will work in case there's only one line wwith "..\postdata" string and the content is exact as described in question.

Edit Covering the both cases:

@echo off
setlocal 
set "start_file=start.txt"
set "xml_file="
for /f tokens^=5^ delims^=\^" %%# in ('type "%start_file%"^|find /i "..\posdata"^|find /i /v "-storedbpath"') do set "xml_file=%%#"
if not defined xml_file (

    for /f "usebackq tokens=9" %%# in ("%start_file%") do (
        set "xml_file=%%~#"
    )
)
echo %xml_file%

Upvotes: 1

foxidrive
foxidrive

Reputation: 41242

Text this:

for /f "usebackq tokens=4" %%a in ("start.txt") do echo %%~nxa

Upvotes: 0

Related Questions