Page 1 of 1

get drive letters & drive types, if fixed drive, run dirms

Posted: 07 Jun 2010 12:42
by davep
I'm working on a batch that can get the drive letters and drive types via fsutil fsinfo ... but I'm stuck with the if logic that follows. If the drive type is fixed, I'd like to run a defrag utility called dirms.exe. Once this is solved, I'd have one batch file that can run as a scheduled task on every machine, regardless of drive configurations. Right now I'm redirecting to a text file, as I'm not sure what to do next.

So far I can get the drive letters and drive type:

Code: Select all

@echo off
title DefragDotBat

rem set log file name and location
set log=c:\util\DefragDotBat.log

rem get drive letters
set "drlist="
for /f "tokens=*" %%a in ('fsutil fsinfo drives^|find /v ""') do (
    set "dr=%%a"
    call set drlist=%%drlist%% %%dr:~-3%%
)
rem use drive letters to get drive type
for %%a in (%drlist%) do fsutil fsinfo drivetype %%a >> %log%

rem now defrag fixed drives and exclude optical and remote/network drives



The text file %log% contains the following:

C:\ - Fixed Drive
D:\ - CD-ROM Drive
M:\ - Remote/Network Drive

How do I run dirms.exe only on the fixed drive? I've read a bit about IF and ELSE, but I'm quite confused. Can someone nudge me in the right direction?

Thanks,
Dave

Re: get drive letters & drive types, if fixed drive, run dir

Posted: 07 Jun 2010 13:24
by aGerman
I've no idea about dirms. But if it would work like
dirms C:\
you could try this

Code: Select all

for /f "tokens=1,2*" %%a in (%log%) do (
  if "%%c"="Fixed Drive" (
    dirms %%a
  )
)


Regards
aGerman

Re: get drive letters & drive types, if fixed drive, run dir

Posted: 07 Jun 2010 15:22
by davep
Thanks! We're getting closer. I'm having trouble getting that to work. Rehearsing some commands before committing to batch tells me that something about the for/if statement is amiss. Hint: fsutil... generates C:\ and dirms.exe only needs C. Also, the blank space before "Fixed Drive" may be getting in the way. I'll keep digging. Is there any way to do this without a text file at all? Can we wrap all of this up into one giant for loop?

Re: get drive letters & drive types, if fixed drive, run dir

Posted: 07 Jun 2010 17:18
by aGerman
Yeah, try this

Code: Select all

@echo off

for /f "tokens=2" %%a in ('fsutil fsinfo drives') do (
  for /f "tokens=1* delims=:\- " %%b in ('fsutil fsinfo drivetype %%a') do (
    echo %%c|findstr /c:"Fixed Drive">nul && dirms %%b
  )
)



btw Sorry for my fault if "%%c"=="Fixed Drive" ... the second = was missing.

Regards
aGerman

Re: get drive letters & drive types, if fixed drive, run dir

Posted: 07 Jun 2010 20:41
by davep
Looks great! I knew it would be simple like that. I'll try it out when I get to the office in the morning. Thanks!

Re: get drive letters & drive types, if fixed drive, run dir

Posted: 08 Jun 2010 13:26
by davep
It works for the first fixed drive (C:) but doesn't make it to the second fixed drive (E:) on my workstation. I've also learned that while dirms.exe is fast and thorough, it doesn't always exit gracefully. I have replaced it with a call to XP-native file defrag.exe.

Thanks!