ChatGPT解决这个技术问题 Extra ChatGPT

Get current batchfile directory

Firstly, I saw this topic but I couldn't understand that.

Question :

There is a batch file in D:\path\to\file.bat with following content :

echo %cd%
pause

Output is :

C:\

It must be D:\path\to

What am I doing wrong?

You should read all the answers to a question, especially the higher vote getters, not just the accepted one. The answer with the highest score at your posted link already answers your question.
If you are in c:\ when you type the batch file name then c:\ is what %cd% will print.

D
Dan Dascalescu

System read-only variable %CD% keeps the path of the caller of the batch, not the batch file location.

You can get the name of the batch script itself as typed by the user with %0 (e.g. scripts\mybatch.bat). Parameter extensions can be applied to this so %~dp0 will return the Drive and Path to the batch script (e.g. W:\scripts\) and %~f0 will return the full pathname (e.g. W:\scripts\mybatch.cmd).

You can refer to other files in the same folder as the batch script by using this syntax:

CALL %0\..\SecondBatch.cmd

This can even be used in a subroutine, Echo %0 will give the call label but, echo "%~nx0" will give you the filename of the batch script.

When the %0 variable is expanded, the result is enclosed in quotation marks.

More on batch parameters.


Look, I do not need to run stm.sql in D:\Dir1\Dir2\stm.sql. I need mysql.exe -u root -p mysql < %cd%\stm.sql to execute that stm.sql commands.
@HamedKamrava is it for my SQL batch? not batch file like *.bat or *.sh?
@Stoleg- In fact, There are 2 files in D:\Dir1\Dir2\batchfile.bat and D:\Dir1\Dir2\stm.sql. batchfile.bat content is : mysql.exe -u root -p mysql < D:\Dir1\Dir2\stm.sql and stm.sql content is some MySQL commands.
System read-only variable %CD% keep the path of the caller of the batch, not the batch file location.
echo %~dp0 will return path to batch location. echo %~f0 will return path to the batch with filename.
P
Pokechu22

Very simple:

setlocal
cd /d %~dp0
File.exe

The code is short but it is not simple to understand. What is this File.exe? Is the current directory path stored in %~dp0 ?
This answer actually answers the question, I think. Kudos.
I just used cd /d %~dp0 as first line of batch file and worked
@T.Todua "setlocal" causes any environment changes, including current directory to be contained either until the batch file ends or a "endlocal" is encountered. If you're going to change environment variables or the current directory, it's good form.
Best to use "%~dp0" rather than %~dp0. Quotes will guarantee the path is taken as one token, even if there are spaces, and also protects against poison characters like &.
j
jeb

Within your .bat file:

set mypath=%cd%

You can now use the variable %mypath% to reference the file path to the .bat file. To verify the path is correct:

@echo %mypath%

For example, a file called DIR.bat with the following contents

set mypath=%cd%
@echo %mypath%
Pause

run from the directory g:\test\bat will echo that path in the DOS command window.


this is finest and simplest solution
This is not a good solution because it does not take into account the batch script being called from another file on another path.
also when run as elevated permissions the %cd% returns C:\Windows\system32 but not the directory where it is stored.
Also, you could just use %cd%. No need to copy it first.
W
Wendell Holmes

Here's what I use at the top of all my batch files. I just copy/paste from my template folder.

@echo off
:: --HAS ENDING BACKSLASH
set batdir=%~dp0
:: --MISSING ENDING BACKSLASH
:: set batdir=%CD%
pushd "%batdir%"

Setting current batch file's path to %batdir% allows you to call it in subsequent stmts in current batch file, regardless of where this batch file changes to. Using PUSHD allows you to use POPD to quickly set this batch file's path to original %batdir%. Remember, if using %batdir%ExtraDir or %batdir%\ExtraDir (depending on which version used above, ending backslash or not) you will need to enclose the entire string in double quotes if path has spaces (i.e. "%batdir%ExtraDir"). You can always use PUSHD %~dp0. [https: // ss64.com/ nt/ syntax-args .html] has more on (%~) parameters.

Note that using (::) at beginning of a line makes it a comment line. More importantly, using :: allows you to include redirectors, pipes, special chars (i.e. < > | etc) in that comment.

:: ORIG STMT WAS: dir *.* | find /v "1917" > outfile.txt

Of course, Powershell does this and lots more.


Great answer, thanks a lot! :-) Just beware your second technique (set batdir=%CD%). It is not okay, because it is the path to the caller, not the bat file itself. The only valid way to get the path to the executing bat file is %~dp0.