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
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.
ThisWorkbook.Path
is passed as parameter with double quotes to batch file and referenced from within the batch file.Command
cd
is used in the batch file.Commands
pushd
andpopd
are used in the batch file.This solution was provided by JosefZ and was added here for completeness.
The full path and name is specified for the text file in directory Logs.