1,105,625 Community Members

Windows Batch Variable Expansion

Member Avatar
overwraith
Light Poster
46 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello, am having some trouble with Windows batch variable expansion. Am trying to make a script that will make a list of all currently plugged in drives. I want to omit the system drive, since that one shows up as a blank letter. The following script can be copy+paisted directly into windows command line, you just have to turn on delayed variable expansion first, so I will paste both commands here:

    SETLOCAL ENABLEDELAYEDEXPANSION

    set mylist=& for /f "tokens=3 delims= " %A in ('echo list volume ^| diskpart ^| findstr /V "###" ^| findstr /V "System" ^| findstr "Volume"') do (set mylist=!mylist! %A:)

The script does not work right though, because when I tell the variable to echo, I get the variable name at the beginning of the output, aswell as the output printed twice.

C:\Users\UserName>echo %mylist%
!mylist! E: F: C: D: G: H: J: E: F: C: D: G: H: J:

Would be cool if somebody knows what's going on.

Member Avatar
overwraith
Light Poster
46 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Figured something out today, if the list is blank the script will fail, if I assign a space to it, it will preform as I want it to. Does anybody know why?

set mylist= & for /f "tokens=3 delims= " %A in ('echo list volume ^| diskpart ^| findstr /V "###" ^| findstr /V "System" ^| findstr "Volume"') do (set mylist=!mylist! %A)
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article