Page 1 of 1

A question about a command inside a variable

Posted: 21 Apr 2019 04:59
by BoQsc
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

Re: A question about a command inside a variable

Posted: 21 Apr 2019 10:10
by ShadowThief
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.

Re: A question about a command inside a variable

Posted: 22 Apr 2019 03:01
by BoQsc
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

Re: A question about a command inside a variable

Posted: 24 Apr 2019 03:16
by BoQsc
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