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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
davep
Posts: 24
Joined: 29 May 2008 14:03
Location: Nauf Kakalak

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

#1 Post by davep » 07 Jun 2010 12:42

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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 07 Jun 2010 13:24

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

davep
Posts: 24
Joined: 29 May 2008 14:03
Location: Nauf Kakalak

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

#3 Post by davep » 07 Jun 2010 15:22

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?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

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

#4 Post by aGerman » 07 Jun 2010 17:18

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

davep
Posts: 24
Joined: 29 May 2008 14:03
Location: Nauf Kakalak

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

#5 Post by davep » 07 Jun 2010 20:41

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!

davep
Posts: 24
Joined: 29 May 2008 14:03
Location: Nauf Kakalak

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

#6 Post by davep » 08 Jun 2010 13:26

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!

Post Reply