Pregunta

for example my volumelabel for driveletter e is volume-abc

so for example im in the shell at:

e:\directory-a\bc\>

now i need the volumelabel of driveletter e:, with the statement:

vol %CD:~0,1%:

i will get the current volumelabel, but with a bunch around it ( sorry, only in german ):

Datenträger in Laufwerk E: ist volume-abc
Volumeseriennummer: 4A69-FD21

so maybe it is possible to parse / extract the string volume-abc from this output, but it seems for me like a bad way, because the output could be vary from windows- and language-version. so i dont know how to do that string operation in robust way.

¿Fue útil?

Solución

@ECHO OFF
SETLOCAL
SET volume=%1
IF NOT DEFINED volume SET volume=%CD%
SET "volume=%volume:~0,1%:"
SET "vlabel="
SET "vserial="
FOR /f "delims=" %%a IN ('vol %volume%') DO (
 CALL :lastparm %%a
)    
ECHO LABEL is %vlabel%
ECHO Serial is %vserial%
endlocal&set vlabel=%vlabel%&set vserial=%vserial%
GOTO :EOF

:lastparm
SET $1=%2
IF DEFINED $1 shift& GOTO lastparm
IF DEFINED vlabel (SET vserial=%1) ELSE (SET vlabel=%1)
GOTO :eof

This should do what you request - within limits. Run the batch, providing you drivename as the first parameter. If no parameter is provided, the current drive will be chosen.

In English, the response from VOL may be

 Volume in drive C has no label.
 Volume Serial Number is 830B-46FA

 Volume in drive M is XPC
 Volume Serial Number is 62FF-EBB1

So it would seem that a language-independent version may be a little difficult. The above code would give label. for C:, not no label. What any other language would generate, I've no idea.

It could be however that the fact that the response is in lower-case may be a way of defining "unlabelled" - I believe a real label must be upper-case.

And it appears that a volume label may now be up to 32 characters, and I've no idea whether spaces are now allowed...

Edit : added ENDLOCAL line to set values found in caller's environment. The method doesn't seem logical at first glance, but exploits the parsing procedure used by cmd. The entire endlocal line is parsed and %var%s replaced by their contents. Then the line is executed, which first disposes of all environment updates made since the setlocal and then assigns the values of interest back to the variables.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top