I am trying to write a batch file that goes over existing network shares and deletes all the non-administrative ones.
Here's my code:
FOR /F "USEBACKQ SKIP=1 TOKENS=*" %%j IN (`wmic /node:192.168.0.101 SHARE WHERE "Name like '%%[^$]'" GET NAME ^| findstr /r /v "^$"`) DO NET SHARE %%j /DELETE
SKIP=1
for skipping meta-column information on wmic return
findstr /r /v "^$"
for removing a trailing whiteline from wmic return which was getting captured in %%j
Now, this only works for share names that do not contain a space in-between.
To remedy, I have tried (which doesn't work as well):
FOR /F "USEBACKQ SKIP=1 TOKENS=*" %%j IN (`wmic /node:192.168.0.101 SHARE WHERE "Name like '%%[^$]'" GET NAME ^| findstr /r /v "^$"`) DO NET SHARE "%%j" /DELETE
Further, trying to echo the %%j
variable in quotes resulted in this:
FOR /F "USEBACKQ SKIP=1 TOKENS=*" %%j IN (`wmic /node:192.168.0.101 SHARE WHERE "Name like '%%[^$]'" GET NAME ^| findstr /r /v "^$"`) DO @ECHO "%%j"
Where only single quotes got printed
Kindly help!
You can do all of that with a single WMIC
command, without a For
loop and without the need to involve FindStr
or the Share
option of net.exe
:
@%SystemRoot%\System32\wbem\WMIC.exe /Node:192.168.0.101 Share Where "Type < 2147483648" Call Delete