Execute command every 5 min off multiple drives

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Olyrd
Posts: 25
Joined: 05 Feb 2016 07:36

Execute command every 5 min off multiple drives

#1 Post by Olyrd » 08 Apr 2016 10:49

I have some portable drives that I'd like to never spin-down, in other words make them impossible to sleep. The reason for this is to avoid some errors due to drive's firmware which puts the drive in sleep regardless of windows os settings.


Expected result of the script:
For each drive/partition that contains the extensionless file nosleep in its root, execute the command pushd %drvLetter% dir /b /s > tempdata.txt every 5 minutes, non-stop on all found drives.


Incompleted Sample Script:
The following is something similar to what I'm trying to do but can't finish it because it is beyond my current knowledge. Added some REM comments that explains some stuff.

Code: Select all


REM Set all found drives in a variable
for /f %%a in (c d e f g h i j k l n m o p q r s t u v w x y z) do (
  if exist %%a:\nosleep (
    set dev=%%a:
  )



REM loop through each found drive letter in the variable and then loop indefinetaly the same command every 5 min on that drive.
:loop
for /f "delims=;" %%a in (%dev%) do (
  pushd %dev%
  ping 127.0.0.1 -n 300 > nul
  dir /b /s > tempdata.txt
  goto loop
)


Thank you for your time and patience.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Execute command every 5 min off multiple drives

#2 Post by foxidrive » 09 Apr 2016 07:45

Olyrd wrote:I have some portable drives that I'd like to never spin-down, in other words make them impossible to sleep. The reason for this is to avoid some errors due to drive's firmware which puts the drive in sleep regardless of windows os settings.


I like this concept.

I'd like to keep a drive awake here, but let others sleep.
...Windows doesn't allow you to deselect drives from sleep mode.


Test this to see is it works for you:

Code: Select all

@echo off
:: Looks for a file called "nosleep" in the root of every drive and
:: performs a delete and a write operation every 5 minute on those drive to avoid sleep mode.
mode con: cols=20 lines=3
echo  Preventing drives
echo   from sleeping...
set drives=c d e f g h i j k l n m o p q r s t u v w x y z
:loop
for %%a in (%drives%) do if exist "%%a:\nosleep" (
  del "%%a:\nosleep-temp-file" 2>nul
  echo bugger off sleeping drives>"%%a:\nosleep-temp-file"
  )
  ping 127.0.0.1 -n 300 > nul
  goto loop


Using Windows Task Scheduler is useful for this, instead of ping/goto/the loop, as it can run the batch file hidden, and also uses bugger all resources.

Olyrd
Posts: 25
Joined: 05 Feb 2016 07:36

Re: Execute command every 5 min off multiple drives

#3 Post by Olyrd » 09 Apr 2016 13:28

It works. But I need some help on making the script for Task Scheduler. I'd like to add an entry in Task Scheduler, to run your code without any external script necessary (ie. have the entire script in one line added in task Scheduler). Hope that makes sence.

For task Scheduler execution I only need the following block from your code:

Code: Select all

set drives=c d e f g h i j k l n m o p q r s t u v w x y z

for %%a in (%drives%) do if exist "%%a:\nosleep" (
  del "%%a:\nosleep-temp-file" 2>nul
  echo bugger off sleeping drives>"%%a:\nosleep-temp-file"
)


If you could make some magic and make the code as one-line, then it could fit in the following:

Code: Select all

set execString=YOUR CODE
SCHTASKS.exe /Create /SC ONLOGON /TN DriveWake /TR "%execString%"
echo admin_pass| SCHTASKS.exe /Change /RI 5 /TN DriveWake


This will create an entry in Task Scheduler that executes the one line code stored in variable execString, every 5 minutes.

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

Re: Execute command every 5 min off multiple drives

#4 Post by aGerman » 09 Apr 2016 14:51

It's difficult but isn't impossible.
-snip-

Code: Select all

set "execString=C:\Windows\system32\cmd.exe /c \"for %%a in (c d e f g h i j k l n m o p q r s t u v w x y z) do if exist \"%%a:\nosleep\" (del \"%%a:\nosleep-temp-file\" 2^>nul^&echo bugger off sleeping drives^>\"%%a:\nosleep-temp-file\")\""

setlocal EnableDelayedExpansion
SCHTASKS.exe /Create /SC ONLOGON /TN DriveWake /TR "!execString!"

-snip-

Regards
aGerman

Olyrd
Posts: 25
Joined: 05 Feb 2016 07:36

Re: Execute command every 5 min off multiple drives

#5 Post by Olyrd » 09 Apr 2016 16:25

Great work guys! Works as expected.
Thank you so much.
:D

Post Reply