命令行的.cmd /蝙蝠脚本,如何让行书目录 [英] Command line .cmd/.bat script, how to get directory of running script
问题描述
你怎么能得到已运行脚本的目录和.cmd文件中使用它吗?
How can you get the directory of the script that was run and use it within the .cmd file?
推荐答案
雷蒙德陈有几个思路:
<一个href=\"http://blogs.msdn.com/oldnewthing/archive/2005/01/28/362565.aspx\">http://blogs.msdn.com/oldnewthing/archive/2005/01/28/362565.aspx
在这里引述在全因为MSDN档案往往是有些不可靠:
Quoted here in full because MSDN archives tend to be somewhat unreliable:
简单的方法是使用%CD%
伪变量。它扩展到
当前工作目录。
The easy way is to use the
%CD%
pseudo-variable. It expands to the current working directory.
设置OLDDIR =%CD%
结果
..做的东西..
结果
CHDIR / D%OLDDIR%放大器; REM恢复当前目录
(当然,目录中保存/恢复可以更轻松拥有
一直与 PUSHD
/ POPD
完成,但在这里,不是问题的关键。)
(Of course, directory save/restore could more easily have
been done with pushd
/popd
, but that's not the point here.)
的%CD%
伎俩是很方便的,即使在命令行。例如,我
经常发现自己在一个目录那里有我想要的文件
操作上,但是...哦,我需要更改目录到其他目录,以便
执行该操作。
The %CD%
trick is handy even from the command line. For example, I
often find myself in a directory where there's a file that I want to
operate on but... oh, I need to chdir to some other directory in order
to perform that operation.
集_ =%CD%\\ curfile.txt
结果
CD ...其他目录...
结果
somecommand ARGS%_%ARGS
(我喜欢用 _%%
作为我从头环境变量。)
(I like to use %_%
as my scratch environment variable.)
键入 SET /?
来看看该命令规定的其他伪变量
处理器。
Type SET /?
to see the other pseudo-variables provided by the command
processor.
另外,文章中的评论是非常值得扫描比如这一个:
Also the comments in the article are well worth scanning for example this one:
<一个href=\"http://blogs.msdn.com/oldnewthing/archive/2005/01/28/362565.aspx#362741\">http://blogs.msdn.com/oldnewthing/archive/2005/01/28/362565.aspx#362741
这涉及使用%〜DP0:
This covers the use of %~dp0:
如果你想知道批处理文件的生活:%〜DP0
If you want to know where the batch file lives:
%~dp0
0%
是批处理文件的名称。 〜DP
给你的驱动器和路径
指定的参数。
%0
is the name of the batch file. ~dp
gives you the drive and path of
the specified argument.
这篇关于命令行的.cmd /蝙蝠脚本,如何让行书目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!