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

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.

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"

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.

Batch script does not run in temporary folder after decompressing SFX WinRAR file

I have a program with a separate setup for 32 and 64 bits. My goal is to create a single executable that can run the appropriate setup. So I created a folder and placed the two setups inside, then wrote the following script:
#echo off
if %PROCESSOR_ARCHITECTURE%==AMD64 goto :x64
if %PROCESSOR_ARCHITEW6432%==AMD64 goto :x64
:x86
"%cd%"\setup.exe
exit
:x64
"%cd%"\setup-x64.exe
exit
Afterwards, I created the SFX file with this folder in WinRAR, pointing to the BAT file. But when I run it, a command line window pops up and shuts down instantly and nothing happens. I go to the temporary folder and double click the BAT file and the setup starts. The same happens in the original folder. What is happening and how can I fix it? Thanks!
%cd% reffers to the directory of the call of the batch-file.
For example a batch-file is in %USERPROFILE%\Desktop\Folder\bat.bat:
echo %cd%
pause
and you start it for example from the command-line like this:
C:\>%USERPROFILE%\Desktop\Folder\bat.bat
it should echo C:\ as that is where you called it from.
Two ways from the comments to solve the problem:
Push the directory of the batch-file using pushd "%~dp0" -> will result in a change of the variable value of %cd%
or
do not use "%cd%" but "%~dp0"
Both ways use the fact that the zeroth argument of a batch-file is its path.
You can prevent the command-line window from closing if you are debugging the file from the command-prompt itself if possible. With that you should have seen an error that state something like ...\setup.exe not found. After that nothing had to be done from the batch-file so it closed.

Running batch file depending on other files from batch file

I'm writing a batch file which is supposed (among other things) to call another batch file.
The second batch file is depending on files located in the same directory, so, when I try running it from the first batch file, it fails.
Example:
Batch file #1: (Located on C:)
#echo OFF
call C:\Tests\Tests.bat
Output: "Could Not Find C:\Tests\Tests.txt
Reason: Both Tests.bat and Tests.txt are located under: C:\Tests while I'm calling it from C:.
Any ideas?
Thanks a lot,
Idan.
You can use %-dp0 within a batch file to get the path of the currently executing batch file. You can use that path every time you need to access a file from that directory. For example:
call "%~dp0\Tests.bat"
for /f "usebackq ..." ... in ("%~dp0\someFile") do ...
Another option is to temporarily set your current directory to the same path at the top of a batch script using PUSHD. From that point on you don't have to worry about providing the path to the other commands. Just be sure to use POPD before the script exits. That way the script will not impact any script that may have called it.
pushd "%~dp0"
rem Do you work
popd

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