Using Findstr to separate a line with "Empty" character

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Using Findstr to separate a line with "Empty" character

#1 Post by sambul35 » 21 Jan 2012 20:29

I got this result from Diskpart -> List Disk -> Select Disk 5 -> Detail Disk command:

Code: Select all

Volume 8    F   Windows Sev  NTFS   Partition    422 MB  Healthy  System
Volume 9        Test1        NTFS   Partition    100 MB  Healthy
Volume 10   G   Test3        NTFS   Partition     49 GB  Healthy
Volume 11                    FAT32  Partition       0 B  Healthy
Volume 12   H      DS1       NTFS   Partition    499 MB  Healthy


How to separate the Volume lines without Drive Letters (and some also without Labels), saving their Volume numbers into variables? If that's going to be !expandable! variable, pls show some code how to separate its content afterwards. Note that Volume Label can be shifted within its field left or right. I tried to use FOR /F, but several "empty" characters are substituted by one, so get nothing useful. What's better to use: Find or Findstr, and what string to look for? If you believe, the task is simple, pls give the code example instead of words. :)
Last edited by sambul35 on 22 Jan 2012 09:29, edited 1 time in total.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Using Findstr to separate a line with "Empty" character

#2 Post by !k » 22 Jan 2012 00:59

Code: Select all

diskpart /s scenario.txt |findstr /rc:"^  Volume [0-9][0-9]*       " |find /c /v ""

*tested on Russian XP http://img3.imageshack.us/img3/3764/2201201211005432.png

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Using Findstr to separate a line with "Empty" character

#3 Post by sambul35 » 22 Jan 2012 09:27

Thanks a lot - it works on Eng system too! :D

So, now I have for the above example:

Code: Select all

set sce=diskpart /s scenario.txt
FOR /F "tokens=2,3 delims= " %%A IN ('%%sce%%|findstr /rc:"^  Volume [0-9][0-9]*       "') do (set vld=!vld! %%A & set lbl=!lbl! %%B)
echo %vld% %lbl%

It prints: 9 11 Test1 FAT32

How to separate Volume numbers and Labels, and cleanup false Labels in these expanded variables? Or, is there a simpler way to save each unmounted volume number and label to separate variables?

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Using Findstr to separate a line with "Empty" character

#4 Post by aGerman » 22 Jan 2012 09:54

The question arises why do you need to save the values in variables first? I would prefer to work with %%A and %%B directly.
Your question how to detect "false" values is however more difficult. If there is always the same length of the sub strings (incl. leading or trailing spaces) you could separate them via string manipulation and apply a Trim operation to each of them.

Regards
aGerman

Post Reply