System cannot find the path specified - batch-file

When I execute my batch file and write the output to a text file, I am getting error message "system cannot find the path specified" in the command prompt. I have to find which line in my batch script, throws this error or the path which is not found.
Note: As I have so many lines in my script, I cannot go through each line and check for the path.
I have to get the path or the line which causes this error when this error comes.

The quickest way to debug an error like this would be to
Remove #echo off if you have that at the top of your script
Remove any >nul's or similar you have for any commands
Add a pause at the very end of the script
Now run the script and look at the output for each of the commands until you find the error, and then you can see which command it did it for.

Look the first place in your batch that requires the path, i.e., a call like "copy" or "del", create a new line after it and put exit. Repeat further down the batch file until the problem appears.

Related

Running Saxon from a batch file: how do I capture errors?

I've got a batch file that calls Saxon to do an XSLT transformation. For some files, Saxon gives me an error. This appears in a new command line window which is open for about 1 second, and then closes. I'm trying to capture that error message in a file.
This is the relevant part of the batch file:
start /wait "" "C:\Program Files\Saxonica\SaxonHE9.6N\bin\Transform.exe" -s:"file.xml" -xsl:fixerrors.xslt -o:"output.xml" 1>>fixerrors.log 2>&1
The fixerrors.log file is created, but remains empty even if Saxon encounters an error and creates the new command line window.
I was able to capture error messages from another program like this, so the idea of using 1>>fixerrors.log is not wrong in itself. This seems to be specific to Saxon.
Tried two approaches suggested by #Gerhard Barnard:
start /wait "" "C:\Program Files\Saxonica\SaxonHE9.6N\bin\Transform.exe" -s:"%~n1 - original.xml" -xsl:fixerrors.xslt -o:"%~n1.xml" 1>>fixerrors.log 2>&1 & type fixerrors.log
does not work, the Saxon errors are not placed in the log file.
"C:\Program Files\Saxonica\SaxonHE9.6N\bin\Transform.exe" -s:"%~n1 - original.xml" -xsl:fixerrors.xslt -o:"%~n1.xml" 1>>fixerrors.log 2>&1
does work, the error messages are placed in the log file (in fact, they're placed twice because I'm redirecting both standard error and standard output to the file).

Unable to execute a .bat file in post build event command line

My post build command is
call "$(ProjectDir)MyFile.bat"
And getting build error:
Error 1 The command "call
C:\MyProject\MyFile.bat"
exited with code 1. C:\Program Files
(x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets 4548 5
What am I missing here? Working on TFS source code, is that the reason getting this error?
For testing in MyFile.bat the only code is mkdir MYTestFolder, but then I am also getting the same error.
It seems like the syntax of the call is correct. Therefore I believe it's failing inside the batch file. I suggest putting the first few lines of the batch to write the time into a log file, so you can confirm it's being called. Do this before any of the actual work, so it can be sure that the batch is being executed.
call "$(ProjectDir)MyFile.bat"
MyFile.bat
#echo off
echo time /t > MyTempFolderPath\logfile.txt
It is the code inside the batch file that is failing - what is it doing?
You have a few options:
a - I would use process monitor and watch process exits and see the status codes of whatever the batch file launches.
b - You could also try running the batch file from the same folder as msbuild and see if you get any errors
Ed

My batch script is reporting an error stating "was unexpected at this time"

The Windows batch file I am trying to run contains the following assignment:
set ANT_HOME="C:/Program Files/apache-ant-1.8.4"
The offending line is:
call %ANT_HOME%/bin/ant -f ../config/common.xml start_db
And when I run the script with echo on I get:
call "C:/Program_Files/apache-ant-1.8.4"/bin/ant -f ../config/common.xml start_db
Files/apache-ant-1.8.4""=="" was unexpected at this time.
I've moved the second quote to to the end of the path, after the ant, but I receive the same error message.
If ant were an .exe I would say your code should work. But I suspect that ant is a batch file, and the error is occurring within the ant script.
I base my conclusion on your error message - specifically the following portion of it: ""=="". The error message is a batch parsing error, and I don't see how your code could generate those characters. So I figure ant must be a batch script that is causing the problem.
I suspect ant.bat has #echo off at the top, so you are not seeing the actual line that is failing.
Not having access to the ant.bat script, I couldn't possibly diagnose exactly what is failing, nor can I guess on how to fix it.
Update - exact problem found
I found a copy of ant.bat online.
It has the following line of code within:
if "%ANT_HOME%"=="" set ANT_HOME=%DEFAULT_ANT_HOME%
Your definition of ANT_HOME includes enclosing quotes, so the code is trying to execute
if ""C:/Program Files/apache-ant-1.8.4""=="" set ANT_HOME=%DEFAULT_ANT_HOME%
The space is not quoted, and you have your error.
All you need to do to fix everything is to remove the quotes from the definition of ANT_HOME, and then add quotes to your CALL statement:
set "ANT_HOME=C:/Program Files/apache-ant-1.8.4"
call "%ANT_HOME%/bin/ant" -f ../config/common.xml start_db
Forward-slashes are not always reliable as folder delimiters within Windows. See Why does the cmd.exe shell on Windows fail with paths using a forward-slash ('/'') path separator?.
Better to use back-slashes.
set "ANT_HOME=C:\Program Files\apache-ant-1.8.4"
call "%ANT_HOME%\bin\ant" -f ..\config\common.xml start_db
The quotes have to completely surround a file name. You can't use them for partial names. Try this instead:
set ANT_HOME=C:\Program Files\apache-ant-1.8.4
call "%ANT_HOME%\bin\ant" -f ../config/common.xml start_db
Oh, and I changed some of your slashes to backslashes (DOS doesn't like forward slashes). I assume you are allowed to use / in that parameter you are passing though.

Creating a .bat file

I am trying to execute a command CSRUN.exe with certain parameters from command prompt. I am able to do this using command prompt. Everytime instead of invoking this from the command prompt, i thought of writing a batch file, where in a single click will help me and also i forward this to someone who wants to execute.
Following is the one i am executing from the command prompt, which i want to have in a batch file
C:\Program Files\Windows Azure SDK\v1.1\bin>csrun.exe E:\Publish\ServiceConfiguration.csx E:\Publish\ServiceConfiguration.cscfg /launchbrowser
Can somebody suggest me how to create a batch file for invoking this command?
Just put those commands in a file
csrun.exe E:\Publish\ServiceConfiguration.csx E:\Publish\ServiceConfiguration.cscfg /launchbrowser
and name it something.bat
Just copy
csrun.exe E:\Publish\ServiceConfiguration.csx E:\Publish\ServiceConfiguration.cscfg /launchbrowser
inside an empty file and save it as a .bat..
Try using the start command and, if csrun.exe is not in your path you will need to specify an exact path for it:
start csrun.exe E:\Publish\ServiceConfiguration.csx E:\Publish\ServiceConfiguration.cscfg /launchbrowser
Save the above in a .bat file.
Also remember to put double quotes around paths with spaces in them.

Stupid Batch File Behavior. Tries to execute comments

I have tried prefixing lines with semicolons, 'REM', etc.. but no matter what when I run my batch file I keep getting "unknown command REM whatever"
"REM test" It is not recognized, and it is windows vista. I simply get "rem" output back to my console.
That's entirely normal behavior. Batch files are simply sequences of commands that are run one after another. So every line will get output to the console as if it were typed there.
H:\>echo rem test > test.cmd
H:\>test
yields the output
H:\>rem test
as if I typed rem test directly to the console.
You can suppress this by either prefixing the line with #:
#rem test
or by including echo off in the batch file:
#echo off
rem test
If I put ":: test" and execute it I get back "Test".
Can't reproduce here.
If I put "; test" it recursively executes itself
A semicolon at the start of the line seemingly gets ignored.
If you're talking about cmd.exe batch files under Windows, you can use:
rem this method or
:: this method.
For bash and a lot of other UNIX-type shells, you use:
# this method.
I'm pretty certain you're not using cmd.exe since that would give you an error like:
'rem' is not recognized as an internal or external command,
operable program or batch file.
rather then:
Unknown command ...
If you are using a UNIX-type shell, the # character is almost certainly what you're after. If you let us know exactly the shell you're using, we can probably help out further.
you probably created an UNICODE file. These files contain 2 bytes header named BOM
which is not shown by any editor but cmd attempts to execute them and fails.
To make sure this is indeed an issue: type any other command at the very beginning
of your file and see it throws the same error - for example #echo test
To fix it, just create a new plain text file and copy content of the original file there.
then remove the original file and replace it by the newly created one.
In my case the problems are line endings. Somehow Maven or the Jenkins pipeline running on a Linux machine changed the line endings from Windows style (CR LF) to Unix style (LF). Changing them back solves the issue for me.

Resources