Get the absolute path of file in batch script - windows

I am using batch script to invoke java application. Java app takes an absolute file path as an argument.
I need my batch script to be invoked with the relative or absolute path of the file as an argument and then to pass the absolute path of the file for java execution. Example:
Running script myscript from location c:/folderA
myscript folderB/file.txt
myscript c:/folderA/folderB/file.txt
should be able to get full absolute path c:/folderA/folderB/file.txt in both cases.
How do I do that?
Just to be as much concrete as possible: I need only BATCH SCRIPT code to retrieve absolute file path string after passing either relative or absolute path to ti as an argument. Not the actual part when I invoke the java app with it.

You can use %~dpnx1, which expands to the drive, path, name and extension of the first argument. Don't forget to quote the name when passing it to another command, though.

Absolute path in "for...do" statement is %~f{variable_letter}.
Absolute path in procedure call for argument %1 is %~f1 or %~dpnx1
but for an environment variable no simple solution.
Workaround is create procedure with set statement e.g (for Windows XP and newer):
#echo off
cd C:\Program Files\Microsoft Games
call :getabsolute "Purble Place\PurblePlace.exe"
echo %absolute%
call :getabsolute "Purble Place"
echo %absolute%
pause
goto :eof
:getabsolute
set absolute=%~f1
goto :eof

Related

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.

Batch file variable in an environment variable

I have a program named go.exe, and I pass it a file name example so that it can make a log file named example_golog.txt.
The problem here is that I want to only call the executable without explicitly giving a filename or making a user have to give a file name.
My solution to this was to name a system variable called GO whose value is go.exe %~n0. What's wrong with this is instead of the %~n0 part getting the file name of the batch file that called it, my go.exe file just makes a text file called %~n0_golog.txt.
Is there any way around this so that %~n0 will do it's magic in the batch files that call it in go.exe %~n0?
EDIT:
My system variable:
Name: GO
Value: go %~n0
My test.bat file:
#echo on
%GO%
pause
When I run test.bat, the %~n0 does not expand out.
So instead of test_golog.txt being made, %~n0_golog.txt is made
In your batch file, you can call it to dereference the variable.
echo %go% will report go %~n0.
call echo %go% will report go test (from a batch file named test).
You can use whatever other sets you need from there.

The filename, directory name, or volume label syntax is incorrect inside batch

When I am running the following inside batch....
set PATH='C:\Users\DEB\Downloads\10.1.1.0.4'
cd !PATH!
I get error "The filename, directory name, or volume label syntax is incorrect"
Update: There are the solutions that worked for me.
Don't use PATH as a var name
set it as "myPATH=C:\Users\DEB DAS\Downloads\10.1.1.0.4"
set myPATH="C:\Users\DEB\Downloads\10.1.1.0.4"
cd %myPATH%
The single quotes do not indicate a string, they make it starts: 'C:\ instead of C:\ so
%name% is the usual syntax for expanding a variable, the !name! syntax needs to be enabled using the command setlocal ENABLEDELAYEDEXPANSION first, or by running the command prompt with CMD /V:ON.
Don't use PATH as your name, it is a system name that contains all the locations of executable programs. If you overwrite it, random bits of your script will stop working. If you intend to change it, you need to do set PATH=%PATH%;C:\Users\DEB\Downloads\10.1.1.0.4 to keep the current PATH content, and add something to the end.
In my case, if I use cmd to run batch file, and the batch file path is not correct show this error, for example users>E:\TEST"E:\TEST.bat error, users>E:\TEST.bat works. after check my path it's fixed.

How to call a .bat file from any location in CMD on Windows

I have a Batch file which I want to execute in CMD from any directory. Something like this:
File name: MyBatch
Path: C:\MyBatch.bat
Open CMD:
c:\Program Files> MyBatch
How can I accomplish this?
Set that location in your PATH environmental variable.
I wouldn't put it the root or the system directory.
I keep a directory with all my scripts in C:\DRR\CMD
and either set it in the MyComputer GUI or run at the command script:
set PATH=%PATH%;C:\DRR\CMD
You could just put it in your c:\windows\system32 directory, as its always in the system path.
How about...
"%MyBatch%"? (the double qoutes are intended) That should work!
to change your Variable, use set MyBatch="Path\Whatever.bat"
and to ask the user for a String, use set /p MyBatch="Question? "
-- or, you can use a BAT-to-EXE converter to run the batch in a Executable.
You would need to set the PATH environment variable to include the path to your batch file
If you are talking Windows, then the PATH Environment variable is what you need to set.
The path where your bat file is placed should be appended to the PATH variable.
In your example append "C:\;" in the value for Path environment variable.
Then you can execute MyBatch.bat from anywhere on the command line.
Create a folder called Batches (lets say in your C drive).
Append C:\Batches in your path environment variable and you then can run batch files in that directory from anywhere.

can't call .bat file from within another - "not recognized as an internal or external command" error

i tried looking at the other questions regarding this, but no go. i've tried a straight call to the other bat file ("otherBat.bat," for instance), a "call" command, and even a "start" command. all of these are failing though, and i'm at a loss as to why. both .bat files are within the same folder, and i'm not changing directories, so i don't know what the problem is...
any help on this would be much appreciated ^_^
edit: sorry, here's the code :)
primary.bat:
echo Test run...enter variable1
set /p var1=:
echo Test run...enter variable2
set /p var2=:
call other.bat %var1% %var2%
pause
other.bat:
echo Working!
pause
You should either cd to the current directory in your first batch file or call the second batch file by full path.
Is the second .bat file in your path? What happens if you change your first .bat file to call it using an absolute path?
use the absolute path :
::prototype
CALL [drive:][path]filename [parameters]
::example
call C:\Users\theUserName\path-to-your-file\the-file-name.bat %your-variables-to-pass%
cf the call documentation from ss64.com
Absolute path may be replaced with %~dp0\, which means use current script's path instead of runtime context; in your case call %~dp0\other.bat %var1% %var2%.
Had same issue when running as administrator from context menu – call tried to execute batch file in System32 instead of parent BAT folder.

Resources