Reputation: 185
I am trying to detect the drive letter that contains removable media (i.e. a memory stick) via wmic logicaldisk get caption^,description^,drivetype 2^>NUL
then I want to use dir
to get the name of the file on the drive (there should only be one but I don't know what the name is) and pass that filename into netsh wlan add profile
.
I have this batch file I have written:
@echo off
for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (
if %%l equ 2 (
SET file= | dir %%i /b
echo %%i\%file%
netsh wlan add profile filename="%%i\%file%" user=all
)
)
pause
and I expect the output to be D:\%some file%.xml
but I am only getting D:
. It seems that the variable %file%
is not being set correctly.
I've tried many variations but I can't get it to set properly. Any suggestions welcome!
Upvotes: 1
Views: 204
Reputation: 34909
Although I do not exactly know what you are trying to accomplish, I decided to provide a modified script in which some issues are fixed. As soon as you edit your question and clarify it I will update my answer accordingly.
So here is the updated code:
@echo off
setlocal EnableDelayedExpansion
for /F "skip=1 delims=" %%L in ('
2^> nul wmic LogicalDisk ^
WHERE ^(DriveType^=2^) ^
GET Caption^,Description
') do (
for /F "tokens=1,*" %%I in ("%%L") do (
set file=myfile.xml
echo %%I\!file!
)
)
endlocal
pause
wmic
by the skip
option of for /F
;WHERE
clause to the wmic
command line to filter out DriveType
and so to not need the if
condition;Description
to the last position by removing DriveType
, so for /F
token string *
can be used, as the property value may contain delimiters (spaces) on its own (although %%J
is not used in the loop then);for /F
loop to remove artefacts from conversion of Unicode output of wmic
by first for /F
;file
to a constant value myfile.xml
just to demonstrate delayed expansion (see also setlocal
command); of course file
could be set in advance outside of the loop here;SET file= | dir %%i /b
as I have no clue what this is intended to do;After the question has been revised and clarified I can provide a solution for the requested task:
@echo off
for /F "skip=1 delims=" %%L in ('
2^> nul wmic LogicalDisk WHERE ^(DriveType^=2^) GET Caption
') do (
for /F "tokens=1" %%I in ("%%L") do (
for /F "eol=| delims=" %%F in ('dir /B "%%I\"') do (
set "FILE=%%I\%%F"
)
setlocal EnableDelayedExpansion
netsh wlan add profile filename="!FILE!" user=all
endlocal
)
)
pause
Description
from wmic
command line as it is not used anyway;for /F
loop to capture the output of dir /B
;FILE
is passed over to netsh wlan add profile
using delayed expansion, because this is required when writing and reading a variable in the same block of code; normal expansion would return the value present at the time the entire block is parsed by the command interpreter; I enabled delayed expansion inside of the loop structure in order to avoid loss of exclamation marks in the file name, because while reading for
variables like %%F
, delayed expansion need to be disabled to not lose such characters;FILE
and so delayed expansion would not be necessary if you could guarantee that there is one file available on each drive; if this is the case, remove the setlocal
and endlocal
command lines, move the netsh
command line into the inner-most for /F %%F
loop, replace !FILE!
by %%I\%%F
and remove the set
command line;Upvotes: 3