Checking via WMIC to see if home folder (h:) exists

July 2, 2019 at 12:33:41
Specs: Windows 10
Afternoon I am attempting to create a robocopy backup script but I would like to check before the script processes to see if the home folder (H:) exists so I have something like this

for /F “tokens=2,3,4 ” %%i in (`wmic path Win32_LogicalDisk Where DriveType="4" get DeviceID, drivetype`) do (
if %%l equ H: (
echo %%i is home folder %%l.
set backuphome=%%i
)
)
pause
But I am new to using for token commands

Any suggestions


See More: Checking via WMIC to see if home folder (h:) exists

Report •

#1
July 2, 2019 at 13:27:58
Sounds overcomplicated.
dir h: >NUL 2>NUL && set "hExists=Y" || set "hExists=N"

How To Ask Questions The Smart Way


Report •

#2
July 2, 2019 at 20:54:52
this also seemed to work on my end:
if exist h: echo h: ok

::as for further testing, this worked:
wmic path Win32_LogicalDisk Where DriveType="3" get DeviceID, drivetype
pause
rem this did NOT: "invalid GET expression" or "invalid verb":
for /F "tokens=*" %%i in ('wmic path Win32_LogicalDisk get DeviceID, drivetype') do echo %%i
I tried your script (after making some syntax corrections), and still could not get wmic to operate inside a for loop: "invalid verb" (meaning "get", I guess.). outside a loop it shows output. Note that output from wmic is in Unicode, and batch (at least at my primitive level - win-7) does not handle Unicode. Unicode files can be "typed" to convert to ascii, which batch can handle.
I think razor has it covered, unless the drivetype is critical. another option is fsutil:
fsutil fsinfo drivetype h:

then parse the results.

message edited by nbrane


Report •

#3
July 2, 2019 at 23:17:29
Your idea is better. I like IF EXIST more.

EDIT: Drive type 4 is network drive, so if it being a network drive is important, NET USE H: could also be used.

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

Related Solutions

#4
August 22, 2019 at 06:22:41
Make sure that the command doesn't hang if you check an unexistant drive, this used to be a problem at least up until Windows 7. Specifically the IF EXIST command had that issue, the DIR command may be different.

Report •

#5
August 24, 2019 at 17:33:45
I am attempting this to confirm that the Home folder of a AD account is present so my backup of the user profile can be performed before I perform a migration from Win7 to Win10

I will try the tip from Razor
if exist h: echo h: ok

::as for the WMIC I was using the drive type of 4 due this is a NAS device
wmic path Win32_LogicalDisk Where DriveType="4" get DeviceID, drivetype
pause
for /F "tokens=*" %%i in ('wmic path Win32_LogicalDisk get DeviceID, drivetype') do echo %%i

But if this di not work I will simplify it
Thanks


Report •

Ask Question