windows 获取当前批处理文件目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17063947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:35:44  来源:igfitidea点击:

Get current batchfile directory

windowsbatch-fileworking-directory

提问by Hamed Kamrava

Firstly, I saw this topicbut I couldn't understand that.

首先,我看到了这个话题,但我无法理解。

Question :

题 :

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

有一个批处理文件,D:\path\to\file.bat内容如下:

echo %cd%
pause

Output is :

输出是:

C:\

It must be D:\path\to

肯定是 D:\path\to

What am I doing wrong?

我究竟做错了什么?

回答by Stoleg

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

系统只读变量%CD%保留批处理调用者的路径,而不是批处理文件位置。

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

您可以使用%0(例如scripts\mybatch.bat)获取用户键入的批处理脚本本身的名称。参数扩展可以应用于此,因此%~dp0将返回驱动器和批处理脚本的路径(例如W:\scripts\),%~f0并将返回完整路径名(例如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 %0will give the call label but, echo "%~nx0"will give you the filename of the batch script.

这甚至可以在子程序中使用,Echo %0会给出调用标签,但是echo "%~nx0"会给你批处理脚本的文件名。

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

%0变量被扩展时,结果用引号括起来。

More on batch parameters.

更多关于批处理参数

回答by shay

Very simple:

很简单:

setlocal
cd /d %~dp0
File.exe

回答by rockerron

Within your .bat file:

在您的 .bat 文件中:

set mypath=%cd%

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

您现在可以使用该变量%mypath%来引用文件的文件路径.bat。要验证路径是否正确:

@echo %mypath%

For example, a file called DIR.batwith the following contents

例如,一个文件调用DIR.bat如下内容

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

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

从目录运行g:\test\bat将在 DOS 命令窗口中回显该路径。

回答by 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.

将当前批处理文件的路径设置为 %batdir% 允许您在当前批处理文件的后续 stmts 中调用它,而不管此批处理文件更改到何处。使用 PUSHD 允许您使用 POPD 快速设置此批处理文件的路径为原始 %batdir%。请记住,如果使用 %batdir%ExtraDir 或 %batdir%\ExtraDir(取决于上面使用的版本,是否以反斜杠结尾),如果路径有空格(即“%batdir%ExtraDir”),则需要用双引号将整个字符串括起来)。您始终可以使用 PUSHD %~dp0。[https://ss64.com/nt/syntax-args.html] 有更多关于 (%~) 的参数。

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.

当然,Powershell 可以做到这一点以及更多。