Defrag.exe scheduled task

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am in the process of creating a scheduled task to run disk defrag
afterhours. The command line I am using is "defrag c: -f -v filename.txt".
How to I adjust this line so that the file name automatically consists of the
phrase "dfrgLog" plus the current date, as in "dfrgLog031005"?
 
Candace said:
I am in the process of creating a scheduled task to run disk defrag
afterhours. The command line I am using is "defrag c: -f -v filename.txt".
How to I adjust this line so that the file name automatically consists of
the phrase "dfrgLog" plus the current date, as in "dfrgLog031005"?
Hi

You can do it using e.g the batch date time functions from Ritchie
Lawrence batch library available at http://www.commandline.co.uk/lib

I also suggest you use the ISO 8601 date format yyyymmdd or yyyy-mm-dd
so it is unambiguous and easily sortable in Explorer.


This bat file should do the job:

--------------------8<----------------------
@echo off
setlocal
call :GetDate year month day

:: using ISO 8601 date format yyyymmdd so it is unambiguous
:: and easily sortable in Explorer.
set currdate=%year%%month%%day%

defrag.exe c: -f -v dfrgLog%currdate%.txt
endlocal
goto :EOF

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetDate yy mm dd
::
:: By: Ritchie Lawrence, 2002-06-15. Version 1.0
::
:: Func: Loads local system date components into args 1 to 3. For NT4/2K/XP
::
:: Args: %1 var to receive year, 4 digits (by ref)
:: %2 var to receive month, 2 digits, 01 to 12 (by ref)
:: %3 Var to receive day of month, 2 digits, 01 to 31 (by ref)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') do (
for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
set %%a=%%d&set %%b=%%e&set %%c=%%f))
endlocal&set %1=%yy%&set %2=%mm%&set %3=%dd%&goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

--------------------8<----------------------
 
Back
Top