0

First off, I'm new to batch files.

I want to find all files with extension .txt in a folder and then scan them so that it scans the 5th line of the text file which is in the format:

txt||||open.doc|||

It then fetches this doc file and gives it as output

This is the code I am using:

@echo off

for %%f in (*.txt) do (
 if "%%~xf"==".txt" do (
   for /f "tokens=1* delims=:img|||" %%a in ('findstr /n .* %%f') do (
    if "%%a" equ "4" (
      echo.%%b
      set str=%%b
      set str=%str:~1,-1%
      echo str: %str%
    )
   )
  )
 )

pause
Compo
  • 36,585
  • 5
  • 27
  • 39
CodeMan
  • 1
  • 2

1 Answers1

0
 if "%%~xf"==".txt" do (

change to

 if /I "%%~xf"==".txt" do (

to allow txt in any case.

delims=:img||| means delimiters are :,i,m,g and |. The delimiters are a set, not a string.

Tips : Use set "var1=data" for setting string values - this avoids problems caused by trailing spaces. In comparisons; don't assign a terminal \, space or quotes - build pathnames from the elements - counterintuitively, it is likely to make the process easier. Use if "thing1" == "thing2" ... to avoid problems caused by spaces in thing1/2.

You need delayedexpansion because str is being varied within the loop. See Stephan's DELAYEDEXPANSION link

Magoo
  • 77,302
  • 8
  • 62
  • 84