windows 使用当前路径位置设置变量的批处理脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6789491/
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
batch script to set a variable with the current path location
提问by massaki
How can I set a variable with the current location? like if I get in c:\test and set the variable it will be test .. and if I get inside c:\test\test2 the variable will be test2 ? ...
如何使用当前位置设置变量?就像如果我进入 c:\test 并设置变量,它将是 test .. 如果我进入 c:\test\test2 变量将是 test2 ?...
I`m thinking about using a for to get inside a lot of folders and check if some file exist, if the correct file exist I want to set the current folder to a variable so I can copy the path and copy this folder ...
我正在考虑使用 for 进入很多文件夹并检查是否存在某个文件,如果存在正确的文件,我想将当前文件夹设置为变量,以便我可以复制路径并复制此文件夹...
Ok so The main problem is to copy the rest of the files on the same folder as the .inf ... please help!
好的,主要问题是将其余文件复制到与 .inf 相同的文件夹中...请帮忙!
Thnak you very much
非常感谢你
回答by jeb
The current directory is in the "shadow" variable cd.
You could try
当前目录位于“shadow”变量 cd 中。
你可以试试
set "var=%cd%"
回答by tenfour
%~dp0
This expands into the drive & path of the currently running batch file. I usually surround my batch files with something like:
这将扩展为当前运行的批处理文件的驱动器和路径。我通常用以下内容包围我的批处理文件:
@echo off
pushd %~dp0
...
popd
Edit: It seems I didn't understand the OP. My example gets the location of the currently running script, not the "Current Directory". +1 to jeb.
编辑:看来我不明白 OP。我的示例获取当前运行脚本的位置,而不是“当前目录”。+1 给杰布。
回答by Aacini
I think there is a little confussion here. %CD% always have the current directory, so you don't need to add anything to have it. However, by rereading your original question, I think that you need the LAST PART of the current directory, that is, the name of the current location excluding all previous locations. If so, then you may use this:
我认为这里有点混乱。%CD% 始终拥有当前目录,因此您无需添加任何内容即可拥有它。但是,通过重新阅读您的原始问题,我认为您需要当前目录的最后一部分,即当前位置的名称,不包括所有先前位置。如果是这样,那么你可以使用这个:
set i=0
:nextdir
set /a i+=1
for /f "tokens=%i% delims=\" %%a in ("%CD%") do if not "%%a" == "" set lastdir=%%a& goto nextdir
echo Current location: %lastdir%