A question about a command inside a variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BoQsc
Posts: 64
Joined: 30 Jun 2014 04:10

A question about a command inside a variable

#1 Post by BoQsc » 21 Apr 2019 04:59

Here is what I have.

Umodified script - works great

Code: Select all

SETLOCAL EnableDelayedExpansion

set "_wmic_command=wmic logicaldisk where "drivetype =2" get VolumeName^, FileSystem^, Description^, Caption"

set numOpts=0
FOR /f "usebackq  skip=1 tokens=*" %%G IN (`%_wmic_command% ^| findstr /r /v "^$"`) DO ( 
	set "line=%%G"
	
	set /A numOpts+=1
	echo !numOpts! !line!)


pause

Modified script for readability - does not work
I would like to know:
  • Why this script currently do not work. What can be done to make it work.
  • if this is good way to improve readability of my previous script, if it worked
Things I changed:
  • I put findstr /r /v $" into a variable _removeLastWmicLine
and I pipe WMIC command into it.

Code: Select all

@echo off
SETLOCAL EnableDelayedExpansion

set "_wmic_command=wmic logicaldisk where "drivetype =2" get VolumeName^, FileSystem^, Description^, Caption"

set "_removeLastWmicLine=findstr /r /v $"

set numOpts=0
FOR /f "usebackq  skip=1 tokens=*" %%G IN (`%_wmic_command% ^| %_removeLastWmicLine%`) DO ( 
	set "line=%%G"
	
	set /A numOpts+=1
	echo !numOpts! !line!)


pause

ShadowThief
Expert
Posts: 902
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: A question about a command inside a variable

#2 Post by ShadowThief » 21 Apr 2019 10:10

Right off the bat, I notice that your modified code is missing quotes and the caret in the findstr string that you're searching for.

BoQsc
Posts: 64
Joined: 30 Jun 2014 04:10

Re: A question about a command inside a variable

#3 Post by BoQsc » 22 Apr 2019 03:01

I made changes according to your critique, but now the output is the same as if I do not pipe wmic command into findstr.

The output after I applied your critique comments:

Code: Select all

1 K:       Removable Disk  FAT32       UBUNTU 19_0
2
Press any key to continue . . .
The output that I wanted to see:

Code: Select all

1 K:       Removable Disk  FAT32       UBUNTU 19_0
Press any key to continue . . .

Changes I made:

Code: Select all

set "_removeLastWmicLine=findstr /r /v $"
Into

Code: Select all

set "_removeLastWmicLine=findstr /r /v "^$""

The final code after I applied your critique:

Code: Select all

@echo off
SETLOCAL EnableDelayedExpansion

set "_wmic_command=wmic logicaldisk where "drivetype =2" get VolumeName^, FileSystem^, Description^, Caption"

set "_removeLastWmicLine=findstr /r /v "^$""

set numOpts=0
FOR /f "usebackq  skip=1 tokens=*" %%G IN (`%_wmic_command% ^| %_removeLastWmicLine%`) DO ( 
	set "line=%%G"
	
	set /A numOpts+=1
	echo !numOpts! !line!)

pause

BoQsc
Posts: 64
Joined: 30 Jun 2014 04:10

Re: A question about a command inside a variable

#4 Post by BoQsc » 24 Apr 2019 03:16

Ok, I've found what's wrong. I needed to escape caret.


Old:

Code: Select all

set "_removeLastWmicLine=findstr /v "^$""
New:

Code: Select all

set "_removeLastWmicLine=findstr /v "^^$""


Final working code.

Code: Select all

set "_wmic_command=wmic logicaldisk where "drivetype =2" get VolumeName^, FileSystem^, Description^, Caption"

set "_removeLastWmicLine=findstr /v "^^$""

set numOpts=0
FOR /f "usebackq  skip=1 tokens=*" %%G IN (`%_wmic_command% ^| %_removeLastWmicLine%`) DO ( 
	set "line=%%G"
	
	set /A numOpts+=1
	echo !numOpts! !line!)

pause

Post Reply