Relative paths in windows batch file - batch-file

This seems to be the directory where my batch file my-build resides.
Program\
my_app\
javasdk\
java-build.bat
app\
my-build.bat
I want to run from my batch file
1. Move one level up and run java-build.bat
2. And then run one more command in app dir
I tried
cd ..
javasdk\java-build
cd..
app\my-build
app- antbuild.bat -D env.myAppjars= C:\Source\abcd\my-app\app\lib
I am getting some errors.
Am I doing it correct?
How can I use relative path instead
C:\Source\abcd\my-app\app\lib

You are in C:\Source\abcd\my-app\javasdk and want a relative path to C:\Source\abcd\my-app\app?
Go one up and then down the other path:
cd /d "C:\Source\abcd\my-app\javasdk"
echo you are in %cd%
dir "..\app"
To go "two up" use ..\.. and so on.
Also use call to call another batchfile. If you just execute it with it's Name, there is no return to the calling file.

Related

Batch file to run cmd command

I have this cmd command (that I found here) that makes me list all image files in the current folder:
for %i in (*) do echo ^<img src="%i" /^> >> all.html
So if I want to run this I need to go to cmd and manually input the folder path every time I run said code.
Can I put this in a batch file/cmd file so I can just put the batch file/cmd file in any folder I want and it will run the code?
I believe you are looking for %~DP0 which is only available within a batch file and displays the current drive and directory in which that batch file is located note, this cannot change. It is obtained from %0 which is the batch file's name.
%CD% on the other hand is available either to a batch file or at the command prompt and expands to the drive letter and path of the current directory. Which could change by using CD for instance.
More info
Full answer is given by Compo:
#(for %%i in ("%~dp0*") do #echo ^<img src="%%i" /^>)>"all.html"

How to get the current path of my batch file? [duplicate]

I have a batch file that I intend to distribute to our customers to run a software task.
We distribute them as a folder or .zip with the files inside. Inside, there is the batch files and another folder with the files needed to run the batch.
Normally, when you make a batch, you type the path where the files are. But I won't know where the files are. The files will still be kept inside the master folder, but I need to have the batch find that folder to run the files.
So for example: If they have the master folder on the desktop and they run it, it would need to be something like "C:\Users\Username\Desktop" to run. You would have the batch CD to that location.
But what if they run it from documents? I don't know the username, so I have to somehow have the batch find this. Any code and/or instructions would be great.
There is no need to know where the files are, because when you launch a bat file the working directory is the directory where it was launched (the "master folder"), so if you have this structure:
.\mydocuments\folder\mybat.bat
.\mydocuments\folder\subfolder\file.txt
And the user starts the "mybat.bat", the working directory is ".\mydocuments\folder", so you only need to write the subfolder name in your script:
#Echo OFF
REM Do anything with ".\Subfolder\File1.txt"
PUSHD ".\Subfolder"
Type "File1.txt"
Pause&Exit
Anyway, the working directory is stored in the "%CD%" variable, and the directory where the bat was launched is stored on the argument 0. Then if you want to know the working directory on any computer you can do:
#Echo OFF
Echo Launch dir: "%~dp0"
Echo Current dir: "%CD%"
Pause&Exit
ElektroStudios answer is a bit misleading.
"when you launch a bat file the working dir is the dir where it was launched"
This is true if the user clicks on the batch file in the explorer.
However, if the script is called from another script using the CALL command, the current working directory does not change.
Thus, inside your script, it is better to use %~dp0subfolder\file1.txt
Please also note that %~dp0 will end with a backslash when the current script is not in the current working directory.
Thus, if you need the directory name without a trailing backslash, you could use something like
call :GET_THIS_DIR
echo I am here: %THIS_DIR%
goto :EOF
:GET_THIS_DIR
pushd %~dp0
set THIS_DIR=%CD%
popd
goto :EOF
You can also do
Pushd "%~dp0"
Which also takes running from a unc path into consideration.
Try in yourbatch
set "batchisin=%~dp0"
which should set the variable to your batch's location.

Start a batch file in a certain location

I want to start a batch file in a certain location. I tried start /d C:\Windows C:\Windows\branding\readWin..bat The batch file which launches this in on my desktop. Any way to do this?
Thanks.
I'm not sure exactly what you mean. You shouldn't need to change the current/start-in directory before invoking a script. If that script needs to define its working directory it should do so within.
If that's the case then just enter the full or relative batch file name:
"C:\Windows\branding\readWin..bat"
If the batch file you're wanting to invoke does not define its own current directory and you feel it's necessary then you could be sure by defining it yourself first:
CD /D "C:\Windows"
Or:
PushD "C:\Windows"
After that just run your batch file using its full or relative path as previously mentioned.
Are you running this from command prompt?
start /d "path" file.bat
The quotes around the file path are important, and just the file name follows the path.

Create a batch file to run an .exe with an additional parameter

I need a batch file which will do the following:
1. Open CMD and navigate to a location C:/Users/...../program.exe
2. Run the program.exe with an additional command to point it to a config file:
e.g. "program.exe C:/Users/..../configFile.bgi"
How can I do this?
I tried this but with no luck:
start "C:\Users\Ben\Desktop\BGInfo\bginfo.exe C:\Users\Ben\Desktop\BGInfo\dc_bginfo.bgi"
pause
Update
I've used the solution provided by Ganesh (below) and came up with this:
cd C:\Users\Ben\Desktop\BGInfo\
bginfo.exe C:\Users\Ben\Desktop\BGInfo\dc_bginfo.bgi
I've tested it on a local machine (changing the directories) but on the server (with the directory above) it does not work...
The folder directory with batch file:
The error
in batch file abc.bat
cd c:\user\ben_dchost\documents\
executible.exe -flag1 -flag2 -flag3
I am assuming that your executible.exe is present in c:\user\ben_dchost\documents\
I am also assuming that the parameters it takes are -flag1 -flag2 -flag3
Edited:
For the command you say you want to execute, do:
cd C:\Users\Ben\Desktop\BGInfo\
bginfo.exe dc_bginfo.bgi
pause
Hope this helps
You can use
start "" "%USERPROFILE%\Desktop\BGInfo\bginfo.exe" "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi"
or
start "" /D "%USERPROFILE%\Desktop\BGInfo" bginfo.exe dc_bginfo.bgi
or
"%USERPROFILE%\Desktop\BGInfo\bginfo.exe" "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi"
or
cd /D "%USERPROFILE%\Desktop\BGInfo"
bginfo.exe dc_bginfo.bgi
Help on commands start and cd is output by executing in a command prompt window help start or start /? and help cd or cd /?.
But I do not understand why you need a batch file at all for starting the application with the additional parameter. Create a shortcut (*.lnk) on your desktop for this application. Then right click on the shortcut, left click on Properties and append after a space character "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi" as parameter.
Found another solution for the same. It will be more helpful.
START C:\"Program Files (x86)"\Test\"Test Automation"\finger.exe ConfigFile="C:\Users\PCName\Desktop\Automation\Documents\Validation_ZoneWise_Default.finger.Config"
finger.exe is a parent program that is calling config solution.
Note: if your path folder name consists of spaces, then do not forget to add "".

What is the current directory in a batch file?

I want to create a few batch files to automate a program.
My question is when I create the batch file, what is the current directory?
Is it the directory where the file is located or is it the same directory that appears in the command prompt, or something else?
From within your batch file:
%cd% refers to the current working directory (variable)
%~dp0 refers to the full path to the batch file's directory (static)
%~dpnx0 and %~f0 both refer to the full path to the batch directory and file name (static).
See also: What does %~dp0 mean, and how does it work?
It usually is the directory from which the batch file is started, but if you start the batch file from a shortcut, a different starting directory could be given. Also, when you'r in cmd, and your current directory is c:\dir3, you can still start the batch file using c:\dir1\dir2\batch.bat in which case, the current directory will be c:\dir3.
In a batch file, %cd% is the most commonly used command for the current directory, although you can set your own variable:
set mypath=%cd%
echo %mypath% (where %mypath% is the current directory that the batch file is sitting in)
So say you were wanting to open Myprog.exe. If it was in the same folder, you would use the command:
start %mypath%\Myprog.exe
That would open Myprog from the current folder.
The other option is to make a directory in C: called AutomatePrograms. Then, you transfer your files to that folder then you can open them using the following command:
start "" "C:\AutomatePrograms\Myprog1.exe"
start "" "C:\AutomatePrograms\Myprog2.exe"
start "" "C:\AutomatePrograms\Myprog3.exe"
Say you were opening a file in your current directory. The command would be:
start %cd%\filename.filetype
I hope I answered your question.
It is the directory from where you run the command to execute your batch file.
As mentioned in the above answers you can add the below command to your script to verify:
> set current_dir=%cd%
> echo %current_dir%
%__CD__% , %CD% , %=C:%
There's also another dynamic variable %__CD__% which points to the current directory but unlike %CD% it has a backslash at the end.
This can be useful if you want to append files to the current directory. Also %CD% does not work under disabled extensions environment ,but %__CD__% always works.
With %=C:% %=D:% you can access the last accessed directory for the corresponding drive. If the variable is not defined you haven't accessed the drive on the current cmd session.
And %__APPDIR__% expands to the executable that runs the current script a.k.a. cmd.exe directory.
It is the directory from where you start the batch file. E.g. if your batch is in c:\dir1\dir2 and you do cd c:\dir3, then run the batch, the current directory will be c:\dir3.
Just my 2 cents.
The following command fails if called from batch file (Windows 7) placed on a pendrive:
%SystemRoot%\System32\xcopy.exe /e /i "%cd%Ala" "C:\KS\Ala\"
But this does the job:
%SystemRoot%\System32\xcopy.exe /e /i "%~dp0Ala" "C:\KS\Ala\"
Your bat file should be in the directory that the bat file is/was in when you opened it. However if you want to put it into a different directory you can do so with cd [whatever directory]

Resources