Batch file can't find specified path when run from excel hyperlink

947 views Asked by At

I have an excel file that has a list of IP addresses and I have linked a batch file that runs a simple ping test and outputs the results to a text log.

When I run the file from the network location it runs no problem. But when I run the excel (Also located in the same directory)...the ping returns "The system cannot find the path specified"

Note - It is returning Ping as what cannot be found. Not the file output.

Anything I'm missing?

ping -n 1 **EXAMPLE** >> Logs\%date:~-4,4%%date:~-10,2%%date:~-7,2%_Test.txt

enter image description here

1

There are 1 answers

1
Mofi On BEST ANSWER

Excel is usually started with the program files directory of Excel as current working directory. Therefore your batch file most likely tries to write the output of ping into a subfolder Logs in program files directory of Excel which does not exist. Even if there would be a subdirectory Logs program files directory of Excel, it would be most likely write-protected. Your batch code should not depend on whatever is the current working directory on starting the batch file.

Jeeped posted two of 4 solutions. My answer contains two more with last solution being the easiest if the path to Logs directory is fixed.

  1. ThisWorkbook.Path is passed as parameter with double quotes to batch file and referenced from within the batch file.

    %SystemRoot%\System32\ping.exe -n 1 **EXAMPLE** >>"%~1\Logs\%date:~-4,4%%date:~-10,2%%date:~-7,2%_Test.txt"
    
  2. Command cd is used in the batch file.

    cd /D "Path to Logs directory"
    %SystemRoot%\System32\ping.exe -n 1 **EXAMPLE** >>%date:~-4,4%%date:~-10,2%%date:~-7,2%_Test.txt
    
  3. Commands pushd and popd are used in the batch file.

    pushd "Path to Logs directory"
    %SystemRoot%\System32\ping.exe -n 1 **EXAMPLE** >>%date:~-4,4%%date:~-10,2%%date:~-7,2%_Test.txt
    popd
    

    This solution was provided by JosefZ and was added here for completeness.

  4. The full path and name is specified for the text file in directory Logs.

    %SystemRoot%\System32\ping.exe -n 1 **EXAMPLE** >>"Path to Logs directory\%date:~-4,4%%date:~-10,2%%date:~-7,2%_Test.txt"