Batch file not working as a scheduled task

D

David

Not strictly a command prompt problem I know, but not sure where else to look.

Running Windows 2000 SP2

The batch file works just fine when started outside of the scheduled
tasks, but when run as a scheduled task it fails.
I believe it is a problem with the Command Extensions - ie. my batch
file is using them, and they are not enabled. I get the following
errors :

'date /t' is not recognized as an internal or external command,
operable program or batch file.
'time /t' is not recognized as an internal or external command,
operable program or batch file.

The lines are :

for /f "tokens=1,2,3,4* delims=/ " %%K in ('date /t') do set day=%%L &
set month=%%M & set year=%%N
for /f "tokens=1,2 delims=: " %%T in ('time /t') do set hour=%%T & set
minute=%%U

It's an inherited script, and I have evidence of it working in the
past. I have a suspicion that SP2 has been applied since that time.

The ultimate goal is to rename a file with a time and date included
before archiving it. If there is an alternative way then I'm more
than happy to ditch what I've got now!!

TIA
David.
 
R

Ritchie

David said:
I believe it is a problem with the Command Extensions - ie. my batch
file is using them, and they are not enabled. I get the following
errors :

You can turn them on by adding 'setlocal ENABLEEXTENSIONS' to your
batch file, something like:-

@echo off & setlocal ENABLEEXTENSIONS

The method you're currently using to load date and time components into
variables is highly reliant the regional settings and delimiters used.
This is definitely not a good idea especially if the script is to run
on other machines. Here's a more reliable way (but I'm not your dad <g>)

01. @echo off & setlocal ENABLEEXTENSIONS
02. call :GetTime h n s t
03. call :GetDate y d m
04. echo/Today is: %y%-%m%-%d% %h%:%n%:%s%
05. goto :EOF
06.
07. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
08. :GetTime hh nn ss tt
09. ::
10. :: By: Ritchie Lawrence, updated 2002-10-30. Version 1.2
11. ::
12. :: Func: Loads local system time components into args 1 to 4.
13. :: For NT4/2K/XP/2003
14. ::
15. :: Args: %1 Var to receive hours, 2 digits, 00 to 23 (by ref)
16. :: %2 Var to receive minutes, 2 digits, 00 to 59 (by ref)
17. :: %3 Var to receive seconds, 2 digits, 00 to 59 (by ref)
18. :: %4 Var to receive centiseconds, 2 digits, 00 to 99 (by ref)
19. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
20. setlocal ENABLEEXTENSIONS
21. for /f "tokens=5-8 delims=:. " %%a in ('echo/^|time') do (
22. set hh=%%a&set nn=%%b&set ss=%%c&set cs=%%d)
23. if 1%hh% LSS 20 set hh=0%hh%
24. endlocal&set %1=%hh%&set %2=%nn%&set %3=%ss%&set %4=%cs%&goto :EOF
25. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
26.
27. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
28. :GetDate yy mm dd
29. ::
30. :: By: Ritchie Lawrence, 2002-06-15. Version 1.0
31. ::
32. :: Func: Loads local system date components into args 1 to 3.
33. :: For NT4/2000/XP/2003.
34. ::
35. :: Args: %1 var to receive year, 4 digits (by ref)
36. :: %2 var to receive month, 2 digits, 01 to 12 (by ref)
37. :: %3 Var to receive day of month, 2 digits, 01 to 31 (by ref)
38. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
39. setlocal ENABLEEXTENSIONS
40. set t=2&if "%date%z" LSS "A" set t=1
41. for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') do (
42. for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
43. set %%a=%%d&set %%b=%%e&set %%c=%%f))
44. endlocal&set %1=%yy%&set %2=%mm%&set %3=%dd%&goto :EOF
45. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 
R

Ritchie

David said:
The batch file works just fine when started outside of the scheduled
tasks, but when run as a scheduled task it fails.
I believe it is a problem with the Command Extensions - ie. my batch
file is using them, and they are not enabled. I get the following
errors :

'date /t' is not recognized as an internal or external command,
operable program or batch file.
'time /t' is not recognized as an internal or external command,
operable program or batch file.

That's right extensions are not enabled. Make sure these reg values are
set to 1

HKEY_USERS\.DEFAULT\Software\Microsoft\Command Processor\EnableExtensions
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\EnableExtensions

If the task was running under a user account (as opposed to the system
account) then logon as that user and check this value:-

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Command Processor\EnableExtensions
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top