0

I have a file named hdd_list.txt.

Index: 0
Device Name: \Device\Harddisk0\Partition0
Drive: entire disk
Label: 
Type: Harddisk
Size: 55.899GB

Index: 1
Device Name: \Device\Harddisk0\Partition1
Drive: C:\
Label: 
Type: Harddisk
Size: 55.897GB

Index: 2
Device Name: \Device\Harddisk1\Partition0
Drive: entire disk
Label: 
Type: Harddisk
Size: 465.761GB

Index: 3
Device Name: \Device\Harddisk1\Partition1
Drive: E:\
Label: Backup
Type: Harddisk
Size: 465.758GB

I need to retrieve Index number corresponding to "c:\" and then subtract it minus 1 to get Index for the entire disk.

Any ideas ?

3 Answers 3

0

Try next array-like approach:

@ECHO OFF >NUL
SETLOCAL enableextensions enabledelayedexpansion
set /A "ii=-1"
For /F "usebackq tokens=1* delims=:" %%G in (
        "D:\bat\StackOverflow\files\30435794.txt") do (
    if /I "%%~G"=="Index" set /A "ii+=1"
    For /F "tokens=*" %%# in ("%%~H") do set "auxH=%%~#"
    set "__%%~G[!ii!]=!auxH!"

    if /I "!auxH!"=="C:\" set /A "__indexC=!ii!-1"

  )
set __ | findstr /I /L "[%__indexC%]"

ENDLOCAL
goto :eof

Output:

==>D:\bat\StackOverflow\30435794.bat
__Device Name[0]=\Device\Harddisk0\Partition0
__Drive[0]=entire disk
__Index[0]=0
__Size[0]=55.899GB
__Type[0]=Harddisk

==>

Resources (required reading):

Sign up to request clarification or add additional context in comments.

Comments

0
@echo off
setlocal

set "Index="
for /F "tokens=2" %%a in ('findstr "Index: Drive:" hdd_list.txt') do (
   if not defined Index (
      set /A Index=%%a-1
   ) else (
      if /I "%%a" equ "C:\" (
         goto diskFound
      ) else (
         set "Index="
      )
   )
)
:diskFound
echo The index for entire disk C:\ is: %Index%

2 Comments

That's exactly what I wanted... Nice script thanks :-).
May I ask you to select this one as best answer? You may also upvote it when you have enough rep to do so
0

I would use JREPL.BAT - a pure script (hybrid JScript/batch) regular expression text processing utility that runs natively on any Windows machine from XP onward.

@echo off
for /f %%N in (
  'jrepl "Index: (\d+)\r?\n.*\nDrive: C:\\" "$1-1" /jmatch /m /f hdd_list.txt'
) do set "index=%%N"

I use the /M option to allow searches across multiple lines, and the /JMATCH option to only return matches, and to specify the replacement string as a JScript expression (subtract 1 from the value).

The search looks for the Index line, capturing the number, followed by any line, followed by the Drive: C\ line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.