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.

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)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.