I need to get the drive letter of the usb pen drive.the command CHDIR >drive.txt gives me the drive letter L:.How do I read/get this info "L:" without quotes into a variable in my batch file
Whatever chdir
outputs can also be used by the pseudo-variable %CD%
. So you already have a variable with that information.
Otherwise you can use for
:
for /f "delims=" %%x in ('chdir') do set "drive=%%x"
But I think %CD%
is much easier :-)
EDIT: You said that with the quotes already. You won't have any quotes in the variable unless you want them in there.
As for the trailing backslash: You can use the following instead of %CD%
:
%CD:~0,-1%
which will expand %CD%
and remove the last character, which you know is the backslash.
Still, will only work if you're in the root directory of that drive. Otherwise you can also use
for %%x in (%cd%) do @set drive=%%~dx
Since cd
will output something like:
c:\documents and settings\pax
you need to just grab the first field using "\"
as the delimiter as follows:
for /f "delims=\" %d in ('cd') do set curdrv=%d
This will set the environment variable curdrv
to c:
for the aforementioned output.
Don't forget to use the double-% (%%d) variant if running inside a cmd file.
Using, in your batch, something like:
set Drive=%cd:~0,2%
Would yeld a content of L: as you asked, if this is the current directory the script is in, at the point when you use it. If you have some popd/pushd, cd, etc.. within the batch, take care when you use it. The rest is up to you
Providing how you would use it (a part of your batch maybe) would have help alot to receive a better answer. We only can guess. The reason, I think, for the couple of for loops in answers. Might not be needed but we're guessing.