I have a bat file that will execute every 10 minutes. On the cmd I see "The system cannot find the path specified".
Any ideas why?
Below is the code:
@ECHO OFF
REM SET DATA, TIME AND PATH
SET DPATH="C:\keepalive\logs\"
SET DATES=%DATE:~7,2%_%DATE:~4,2%_%DATE:~10,4%
SET TIMES=%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%
SET DAT=%DPATH%%DATES%.log
REM PRINTING USERNAME
ECHO USER:%USERNAME% 1>>%DAT% 2>&1
ECHO %DATES%%TIMES% 1>>%DAT% 2>&1
ECHO ACCESS WEBSITE 1>>%DAT% 2>&1
START iexplore http://example.com/sitecore/service/keepalive.aspx
ECHO WAITING FOR WEBSITE 1>>%DAT% 2>&1
PING 1.1.1.1 -n 1 -w 20000 >NUL
ECHO CLOSING WEBSITE 1>>%DAT% 2>&1
taskkill /F /T /IM iexplore.exe
EXIT
There are several issues in your code:
\
from the DPATH
value makes perfect sense;set
syntax: set "VAR=Value"
, so the quotes do not become part of the variable value; so there is one place where to put the quotes effectively: reading (expansion) of the variable value, like "%VAR%"
; this is particularly helpful in case of concatenation "%VAR%\item"
, so the entire expression is enclosed within ""
, and there are no disturbing quotes within the string value;%DATE%
and the %TIME%
values in the same line or block of code for them to reflect the same point of time; using two independent command lines running exactly at midnight, although extremely improbable, could lead to the case that %DATE%
returns the previous day but %TIME%
already returns the next day;echo string 1>> "file.ext" 2>&1
to 1>> "file.ext" echo string
in order to avoid any leading spaces to be output to the file file.ext
as well; for echo
you do not need the 2>&1
part at all, because it does not output anything to the STDERR channel (2
), but only to the STDOUT channel (1
);ping
, you should use an IP that always exists and use the 1 second pause interval between multiple echo requests, because guessing a non-existing IP could fail in case it exists somewhen in the future; so for waiting for 3
seconds, you need 4
echo requests to have 3 pause intervals: ping 127.0.0.1 -n 4
;start
command, so you will never run into problems when the command is enclosed within quotation marks, because start
considers the first quoted item as title: start "" command
;exit
you should state exit /B
, because exit /B
quits the batch script while exit
terminates the containing cmd
instance; it won't change anything when executed via task scheduler, but when debugging it might be really annoying when cmd
is always closed;Here is the fixed code:
@ECHO OFF
REM SET DATE, TIME AND PATH
SET "DPATH=C:\keepalive\logs"
SET "DATES=%DATE%" & SET "TIMES=%TIME%"
SET "DATES=%DATES:~6,4%_%DATES:~3,2%_%DATES:~0,2%"
SET "TIMES=%TIMES:~0,2%_%TIMES:~3,2%_%TIMES:~6,2%"
SET "DAT=%DPATH%\%DATES%.log"
REM PRINTING USERNAME
1>> "%DAT%" ECHO USER:%USERNAME%
1>> "%DAT%" ECHO %DATES%%TIMES%
1>> "%DAT%" ECHO ACCESS WEBSITE
START "" "iexplore" "http://example.com/sitecore/service/keepalive.aspx"
1>> "%DAT%" ECHO WAITING FOR WEBSITE
> NUL PING 127.0.0.1 -n 21
1>> "%DAT%" ECHO CLOSING WEBSITE
taskkill /F /T /IM "iexplore.exe"
EXIT /B