Reputation: 53
My cmd batch file always output:
ECHO is on. ECHO is on. ECHO is on.
The script:
SET "InputFile=abc.txt"
IF EXIST "%InputFile%" (DEL /f /q "%InputFile%")
SETLOCAL EnableExtensions EnableDelayedExpansion
FOR /F Tokens^=* %%G IN ('
"WMIC PATH Win32_DiskPartition WHERE ^(DeviceID^="Disk #0, Partition #1"^) Assoc:list /AssocClass:Win32_LogicalDiskToPartition 2>NUL"
') DO (CALL ECHO %%G>> "%InputFile%")
ENDLOCAL
How to fix?.
Upvotes: 1
Views: 156
Reputation: 38604
Your issue is simply with the DeviceID
string containing a comma, this can easily be overcome using a variable to hold it. In order not to return Echo
status messages, you need to add a non space character after Echo
, the best chartacters for doing that are (
, =
and /
.
Try this!
@Echo Off
Set "InputFile=abc.txt"
Set "SearchStr=Disk #0, Partition #1"
If Exist "%InputFile%" Del /F "%InputFile%"
For /F Tokens^=* %%A In ('
"WMIC Partition Where (DeviceID="%%SearchStr%%") Assoc:List /AssocClass:Win32_LogicalDiskToPartition 2>NUL"
')Do >>"%InputFile%" Echo(%%A
If you don't want to have empty lines in your file output then use another nested For
loop:
@Echo Off
Set "InputFile=abc.txt"
Set "SearchStr=Disk #0, Partition #1"
If Exist "%InputFile%" Del /F "%InputFile%"
For /F Tokens^=* %%A In ('
"WMIC Partition Where (DeviceID="%%SearchStr%%") Assoc:List /AssocClass:Win32_LogicalDiskToPartition 2>NUL"
')Do For /F Tokens^=* %%B In ("%%A")Do >>"%InputFile%" Echo(%%B
However as you're outputting everything, there's no need for a For
loop at all:
@Set "InputFile=abc.txt"
@Set "SearchStr=Disk #0, Partition #1"
@If Exist "%InputFile%" Del /F "%InputFile%"
@WMIC /Output:"%InputFile%" Partition Where (DeviceID="%SearchStr%") Assoc:List /AssocClass:Win32_LogicalDiskToPartition 2>NUL
[Edit /]
As this is one of many questions all seemingly based around the same task, if you're only trying to determine the drive letter associated with Disk #0, Partition #1
you can do that similarly:
@Set "SearchStr=Disk #0, Partition #1"
@For /F Skip^=2Tokens^=2Delims^=^" %%A In ('"WMIC Partition Where (DeviceID="%%SearchStr%%") Assoc /AssocClass:Win32_LogicalDiskToPartition 2>NUL"')Do @Echo %SearchStr% = %%A
Upvotes: 2
Reputation: 30113
1. The WHERE
clause in WQL fails if contains a comma. Use
"… WHERE ^(DeviceID^ like^ "Disk #0_ Partition #1"^) …
2. There is a known wmic
bug, see Dave Benham's article WMIC
and FOR /F
: A fix for the trailing <CR>
problem. Fix it e.g. as follows:
@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
SET "InputFile=abc56715419.txt"
IF EXIST "%InputFile%" (DEL /f /q "%InputFile%")
REM SETLOCAL EnableExtensions EnableDelayedExpansion
(
FOR /F Tokens^=* %%G IN ('
"WMIC PATH Win32_DiskPartition WHERE ^(DeviceID^ like^ "Disk #0_ Partition #1"^) Assoc:list /AssocClass:Win32_LogicalDiskToPartition 2>NUL"
') DO ( for /F "delims=" %%g in ("%%G") do (CALL ECHO(%%g))
)>>"%InputFile%"
REM debugging output
type "%InputFile%"
ENDLOCAL
Here the for
loops are
%%G
to retrieve the WMIC
output;%%g
to remove the ending carriage return in the values returned: wmic
behaviour: each output line ends with 0x0D0D0A
(<CR><CR><LF>
) instead of common 0x0D0A
(<CR><LF>
).Upvotes: 2