Absolute path needed in batch file? [duplicate] - windows

This question already has answers here:
What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?
(4 answers)
What does %~dp0 mean, and how does it work?
(7 answers)
What is the reason for batch file path referenced with %~dp0 sometimes changes on changing directory?
(6 answers)
Closed 4 years ago.
I have a Windows batch file that calls the python interpreter:
python -m ...
That works. But now I have a second batch file that calls the first batch file. When I run this batch file, I get an error:
'python' is not recognized as an internal or external command,
operable program or batch file.
I don't understand why the second batch file doesn't work.
If it helps to have a concrete example, here's one:
In helloworld.py
print("Hello, world!")
In batch1.cmd
#echo off
echo About to call python...
python -m helloworld
pause
exit
In batch2.cmd
#echo off
set "path=%~dp0batch1.cmd"
start "" %path%
Output:
About to call python
'python' is not recognized as an internal or external command, operable program or batch file.
Press any key to continue . . .

You are completely breaking the system variable %path% by setting it in the batch. Now your system cannot find python anymore.
Simply change second batch to:
#echo off
set "mypath=%~dp0batch1.cmd"
start "" %mypath%
To explain the %path% variable better. It holds the path to any possible file locations to search for files to execute or open without the need to have the full path specified each time by a user. By running a command in cmd like python, it first checks the current directory where the batch started in, if python.exe is not there, it will search each semicolon seperated path in the %path% variable. When you set a new %path% variable it only knows of the newly set path and cannot find python to execute and you get the most common cmdline error on windows.
On another note, if you want to start batch1 in the same window, perhaps consider calling batch instead of starting it
call "%mypath%"

Related

Call mvnw.cmd from a .bat file [duplicate]

This question already has answers here:
What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?
(4 answers)
Closed 2 years ago.
I am creating a container in docker using the below command.I need to use the below command from a .bat file when I execute the command from the command line it runs but does not work with the batch file.What am I doing wrong here.
Batch File
call mvnw.cmd spring-boot:build-image -Dspring-boot.build-image.imageName=service
Error:
'mvnw.cmd' is not recognized as an internal or external command,operable program or batch file.
Windows does not know where the mvnw.cmd is, either use an environment variable, with relative path, or use an absolute path, or navigate to the directory, before running the command.

Can't run .bat files properly (Windows 10)

Here is my test.bat
#!/bin/bash
#set the STRING variable
SET STRING=Hello World
#Print the contents of the variable to the screen
echo $STRING
pause
When I run test.bat on Windows 10 I get this output:
C:\Workspace\mcc-batch-jobs>#!/bin/bash
'#!' is not recognized as an internal or external command,
operable program or batch file.
C:\Workspace\mcc-batch-jobs>#set the STRING variable
'#set' is not recognized as an internal or external command,
operable program or batch file.
C:\Workspace\mcc-batch-jobs>SET STRING=Hello World
C:\Workspace\mcc-batch-jobs>#Print the contents of the variable to the screen
'#Print' is not recognized as an internal or external command,
operable program or batch file.
C:\Workspace\mcc-batch-jobs>echo $STRING
$STRING
C:\Workspace\mcc-batch-jobs>pause
Press any key to continue . . .
I can't figure it out. I've checked my PATH, installed cygwin, looked over several Stackoverflow posts, and can't seem to get the script to work. The script was created as a simple test and nothing more.
From my comment, this is a batch file for Windows, (it usually has a .cmd extension but may use .bat instead):
#ECHO Off
SET "STRING=Hello World"
REM Print the contents of the variable to the screen
ECHO %STRING%
PAUSE

Removing the current working directory from the path

I'm working in Windows and would like to know if there is a way to remove the current working directory from the path? I understand that this is the default behavior in PowerShell, but I need it to work in batch or at the Windows command-line.
In UNIX, I would just make sure that my $PATH variable not contain .. Is there any way to accomplish this in batch? This is the current behavior:
H:\tmp>dir
Volume in drive H has no label.
Volume Serial Number is E29C-7B61
Directory of H:\tmp
04/27/2018 10:39 AM <DIR> .
04/27/2018 10:39 AM <DIR> ..
04/27/2018 10:40 AM 37 dwk.bat
1 File(s) 37 bytes
2 Dir(s) 987,995,770,880 bytes free
H:\tmp>dwk.bat
dwk.bat has been run.
H:\tmp>
This is the desired behavior:
H:\tmp>dwk.bat
'dwk.bat' is not recognized as an internal or external command,
operable program or batch file.
H:\tmp>.\dwk.bat
dwk.bat has been run.
H:\tmp>
Thanks.
I recommend first reading the answers on Stack Overflow questions:
Where is "START" searching for executables?
What is the reason for '...' is not recognized as an internal or external command, operable program or batch file?
Many thanks to eryksun because of this answer would not exist without his comment on above referenced answer.
Next I recommend reading the Microsoft Developer Network (MSDN) articles:
Naming Files, Paths, and Namespaces
NeedCurrentDirectoryForExePath function
The question can be answered with: Yes, it is possible for desktop applications and batch files on
Windows Vista and all later Windows client versions and
Windows Server 2003 and all later Windows Server versions.
An environment variable with name NoDefaultCurrentDirectoryInExePath must be defined with any value to prevent execution of a script (.bat, .cmd, .vbs, ...) or an application (.com, .exe) stored in current directory without explicitly using .\ as required on Unix/Linux.
The environment variable NoDefaultCurrentDirectoryInExePath can be defined as system variable to turn off searching in current directory for a script or application for all accounts on this machine. But this is surely no good idea as it will result definitely in many applications including installers and uninstallers won't work anymore correct.
The environment variable NoDefaultCurrentDirectoryInExePath can be defined as user variable to turn off searching in current directory for a script or application for processes using this account. But this is surely also no good idea.
But it can make sense to set the environment variable NoDefaultCurrentDirectoryInExePath as local variable in some use cases to turn off searching in current directory for a script or application without explicitly using .\ on Windows versions with kernel function NeedCurrentDirectoryForExePath which cmd.exe calls before searching for a script file or application not containing a backslash \ (or a forward slash /) in file name string.
Example:
#echo off
pushd "%TEMP%"
set "NoDefaultCurrentDirectoryInExePath=0"
echo #echo %%0 executed successfully.>Test1.bat
echo Calling Test1.bat ...
call Test1.bat
echo Calling .\Test1.bat ...
call .\Test1.bat
echo Starting Test1.bat ...
start /wait Test1.bat ^& timeout 5
set "NoDefaultCurrentDirectoryInExePath="
echo Calling again Test1.bat ...
call Test1.bat
del Test1.bat
popd
pause
This batch file executed from within a command prompt window results in output of current console window:
Calling Test1.bat ...
'Test1.bat' is not recognized as an internal or external command,
operable program or batch file.
Calling .\Test1.bat ...
.\Test1.bat executed successfully.
Starting Test1.bat ...
Calling again Test1.bat ...
Test1.bat executed successfully.
Press any key to continue . . .
And during execution of this batch file a second console window is opened with output:
"%TEMP%\Test1.bat" executed successfully.
This second console window is closed automatically after 5 seconds.
The environment variable NoDefaultCurrentDirectoryInExePath is defined with value 0 after setting directory for temporary files as current directory with pushing current directory path on stack. The variable value does not matter because of evaluated is only existence of environment variable and not its value.
Next another batch file with name Test1.bat is created in directory for temporary files which is usually not write-protected for current user as this would cause lots of troubles.
The first approach to call Test1.bat without any path fails because of environment variable NoDefaultCurrentDirectoryInExePath is defined in local environment.
The second call of Test1.bat with relative path .\ is successful despite existence of the environment variable.
The command START ignores NoDefaultCurrentDirectoryInExePath as proven by this batch file.
Then the environment variable NoDefaultCurrentDirectoryInExePath is deleted to restore original Windows behavior.
The second approach to call Test1.bat without any path is successful now.
Finally the created Test1.bat is deleted and initial current directory is restored as current directory.
It is of course not possible to prevent execution of command DIR which is not a script file or an executable. It is an internal command of cmd.exe – Windows Command Processor – respectively of powershell.exe – Windows PowerShell.

starting a bat script from bash under windows

I am trying to start a .bat file from gow bash. I can sucessfully start a batch and return to bash with the following (I am in C:\tmp\a\ directory), the content of the file a.bat file consists a single command cd (to print the current working directory):
$ cat a.bat
cd
$ cmd "/C a.bat"
c:\tmp\a>cd
c:\tmp\a
$
Now if I try to start the program from c:\tmp (one level higher in the hierarchy of directory structure), I get an error:
$ cmd "/C a\\a.bat"
'a' is not recognized as an internal or external command,
operable program or batch file.
$ cmd '/C a\a.bat'
'a' is not recognized as an internal or external command,
operable program or batch file.
$ cmd '/C a/a.bat'
'a' is not recognized as an internal or external command,
operable program or batch file.
How can I start a batch script given a path to it, and return to bash?
I guess I found a very simple solution:
Add a dot before /C
cmd ". /C a\a.cmd"
After that gow (and cygwin and alikes) stop treating the argument as containing paths and converting it to POSIX paths.
And cmd ignores everything up to /C (well, almost everything, I think).
a.cmd:
#echo off
echo it's working
output:
bash-3.1$ cmd ". /C a\a.cmd"
it's working
bash-3.1$
[Update]
After some research I found that POSIX path conversion has a very clean description of conversion rules.
In your case the rule An argument with a leading / is converted up to the first /.. seems to be applied.
Conversion results in changing '\' to '/'. So, you get a/a.cmd, that is a with a switch /a.cmd. And this results in the observed error, of course.
The link above addresses MSYS and seems to be a bit outdated, but I've checked some of the rules in bash from my fresh Git for Windows installation (based on MSYS2), and they look working as described.
After a bit of research and posting questions to other forums I found the following additional info to run the job "correctly":
cmd //D . //C "a\a.cmd"
References:
Github issue #244, Unix to Windows path translation may corrupt command switches/arguments
Github issue #243, when bash starts a cmd, it adds a buggy double quote symbol at the end of the line

Batch script does not recognize Windows environment variable [duplicate]

This question already has answers here:
Windows Batch error: "'ping' is not recognized as an internal or external command operable program or batch file."
(3 answers)
Closed 6 years ago.
I have batch script like this, let's say test.bat. What it does is: cd into a directory path, and apply "make" commands:
set path=%SRC_PATH%
echo %path%
cd /d %path%
make clean
make all
When I run test.bat from a Windows Command Shell, I get following error despite I defined path to make.exe in Windows Environment Variable
'make.exe' is not recognized as an internal or external command,
operable program or batch file.
Note, I do not see this issue, when I use Windows command line and entered all the commands manually. Issue is only when I put the commands into a batch file.
Any pointers would be appreciated.
thanks.
You dont need to write this
set path=%SRC_PATH%
echo %path%
cd /d %path%
Just enter the path of make.exe in PATH Variable in environment variables..
PATH Variable already carries value so just add semi colon to the end enter the full folder path of make.exe(JUST FOLDER PATH)
now just calling make will work.

Resources