1
set line = xxx/yyy/xxx/xxxxxxxx.h:32:#include "../../xxxx/xxxxx.h"

if this is the string, how can i extract just xxxxxxxx.h, between / and : in batch script?

i have included the bin path of cygwin to my windows environment variables so it can run grep commands. Current, this is my script:

   @echo off

SETLOCAL enabledelayedexpansion

set /p search_parameter="Type your search: "

rem run grep seach commmand
grep -nri %search_parameter% --colour --include=*.{c,h} > text.txt

rem filter required lines into new text file
type text.txt | findstr /I "#include" | findstr /V "examples DELIVERY_REL" > text2.txt

rem read text file line-by-line
for /F "tokens=* delims=" %%G in (text2.txt) do (
set "line=%%G"
rem echo line: !line!
for /f "delims=:" %%i in ("!line!") do set "line=%%i"
for %%i in (!line:/= !) do set "line=%%i"
echo line: !line!

)

pause

echo on 

Currently, this is my output:

line: line:/
line: line:/
line: line:/
line: line:/
line: line:/
line: line:/
line: line:/
line: line:/
line: line:/
line: line:/

The problem is this line:

for %%i in (!line:/= !) do set "line=%%i"
1
  • Did you want to get this ? Commented Nov 14, 2019 at 8:45

2 Answers 2

2

Use a for /f loop to get the part before the first :.
Then use a plain for to get the last token of this part (tokenize by replacing / with spaces):

@echo off
setlocal
set "line=xxx/yyy/xxx/xxxxxxxx.h:32:#include "../../xxxx/xxxxx.h""
set line
for /f "delims=:" %%a in ("%line%") do set "line=%%a"
set line
for %%a in (%line:/= %) do set "line=%%a"
set line

Output of this code:

line=xxx/yyy/xxx/xxxxxxxx.h:32:#include "../../xxxx/xxxxx.h"
line=xxx/yyy/xxx/xxxxxxxx.h
line=xxxxxxxx.h

due to the exact format of your string, you can just:

set "line=xxx/yyy/xxx/xxxxxxxx.h:32:#include "../../xxxx/xxxxx.h""
for /f "delims=:" %%a in ("%line%") do set "line=%%~nxa"

(Thanks, @dbenham). This works because the parser "translates" the "wrong" slash (not valid for a filename) to the "correct" backslash and %%~nxa just extracts the "filename and extension" from the "full file name". (for treats the string as a filename but doesn't even care, if it's valid)

Edit

...
rem read text file line-by-line
for /F "tokens=* delims=" %%G in (text2.txt) do (
  set "line=%%G"
  for /f "delims=:" %%i in ("!line!") do set "line=%%~nxi" 
  echo line: !line!
)
Sign up to request clarification or add additional context in comments.

9 Comments

Assuming the final content never contains a \ character, then you only need the one FOR /F by using set "line=%~nxa"
@dbenham Huh? That's unexpected. It ignores the last part because : isn't a valid char for filenames? And because it also "corrects" the "wrong (for a valid path)" / to a \? Two exploits in one command? You're in top-form today! :D
Not quite - The FOR /F "DELIMS=:" is still responsible for truncating from the : onward. The %~nxa is responsible for stripping of the leading path through the last /. Without the delims=: the : and beyond would still be preserved by %~nxa, even though it is not valid for a name or extension.
@dbenham: [facepalm] Yes, the delims - I completely forgot that... Proves that I'm not in top-form today.
@Stephan may i know what the set line for?
|
0
  @echo off

SETLOCAL enabledelayedexpansion

set /p search_parameter="Type your search: "

rem run grep seach commmand
grep -nri %search_parameter% --colour --include=*.{c,h} > text.txt

rem filter required lines into new text file
type text.txt | findstr /I "#include" | findstr /V "examples DELIVERY_REL" > text2.txt

rem read text file line-by-line
for /F "tokens=* delims=" %%G in (text2.txt) do (
set "line=%%G"
rem echo line: !line!
for /f "delims=:" %%i in ("!line!") do set "line=%%~nxi"
echo line: !line!

)

pause

echo on 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.