Parse text from a batch file

I would like to do a simple analysis in a batch file.

Given the input line:

Foo: Lorem Ipsum 'The quick brown fox' Bar

I want to extract the quoted part (without quotes):

The quick brown fox

Use only the standard command-line tools available in Windows XP.

(I looked findand findstr, but they are not flexible enough to return only part of the string.)

+3
source share
1 answer

Something like this will work, but only if you have one line with quotes in the input line:

@echo OFF
SETLOCAL enableextensions enabledelayedexpansion

set TEXT=Foo: Lorem Ipsum 'The quick brown fox' Bar
@echo %TEXT%

for /f "tokens=2 delims=^'" %%A in ("abc%TEXT%xyz") do (
    set SUBSTR=%%A
)

@echo %SUBSTR%

Output, quoted string in the middle:

Foo: Lorem Ipsum 'The quick brown fox' Bar
The quick brown fox

Output, quoted string in front:

'The quick brown fox' Bar
The quick brown fox

Output, quoted string at the end:

Foo: Lorem Ipsum 'The quick brown fox'
The quick brown fox

Exit, whole line:

'The quick brown fox'
The quick brown fox
+4
source

Source: https://habr.com/ru/post/1708406/


All Articles