Skip to content
⌘ NSIS Forum Archive

Get the first free drive letter

5 posts

meoit#

Get the first free drive letter

I am trying with code:

Var FirstFreeLetter
${For} $0 A Z
IfFileExists $0\*.* 0 Done
StrCpy $FirstFreeLetter $0
${ExitFor}
Done:
${Next}
but not success.

Thanks.
Anders#
The drive root is a bit special, there is no ".." directory.

"\*" detects directory and/or root.

"\?" detects directory but not root?

"\." detects something but I don't remember what exactly.
ahmett#
var /global comspec
ExpandEnvStrings $comspec "%COMSPEC%"
nsexec::exectostack '"$comspec" /C wmic logicaldisk get size,freespace,caption'
Pop $0
Pop $0
${WordFind} "$0" "$\n" "*" $1
${For} $2 1 $1
${WordFind} "$0" "$\n" "+$2" $3
${WordFind} "$3" ":" "+1}" $4
${if} $3 != $4
${WordFind} "$3" " " "+1" $4
${WordFind} "$3" " " "+2" $5
${WordFind} "$3" " " "+3" $6
Math::Script 'r7 = f((r6=r6/1048576)-(r5=r5/1048576))'
Strcmp $7 0 +2
detailprint "Drive $4 Total:$6MB Free:$5MB Used:$7MB"
${endif}
${Next}
meoit#
OK. Now it's working.


StrCpy $0 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
StrCpy $1 0
start:
StrCpy $2 $0 1 $1
StrCmp $2 '' end
IntOp $1 $1 + 1
;MessageBox MB_OK '$$0={$0}$\n$$1={$1}$\n$$2={$2}'
${If} ${FileExists} '$2:\*'
Goto start
${Else}
StrCpy $0 '$2:'
Goto end
${EndIf}
end:
MessageBox MB_OK|MB_USERICON '$0'