Search code examples
batch-filewindows-server-2012

System cannot find the patch specified


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

enter image description here enter image description here


Solution

  • There are several issues in your code:

    • RLH already pointed out potential problems due to the locale-dependent date/time format in their answer; also removing the \ from the DPATH value makes perfect sense;
    • your location of the quotation marks is hard to handle; I stongly recommend to always use this 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;
    • you should read the %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;
    • I recommend to reverse the redirection syntax from 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);
    • to wait for a certain amount of time using 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;
    • you should add a window title for the 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;
    • instead of 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