查找当前文件夹的路径 - cmd [英] find path of current folder - cmd
问题描述
我使用此脚本查找当前文件夹及其.bat文件:
for / f %% i (%0)do set curpath = %%〜dpi
echo%curpath%
它不能正常工作,如果路径包含空格(D:\Scripts\All Scripts - >仅检索D:\Scripts\,如果我放在文件夹中,其路径没有空格检索完整路径)。如何解决?
2015-03-30:已编辑 - 缺少信息
要检索当前目录,可以使用保存当前活动目录的动态%cd%
变量
setcurpath =%cd%
这将为根目录生成一个带有结尾反斜杠的值,而对于其余目录则不生成反斜杠。您可以使用
为%% a在%cd中强制和结束任何目录的反斜杠%\)do setcurpath = %%〜fa
变量:%__ CD __%
将返回当前活动目录结尾的反斜杠。
code>%cd%变量可以直接赋值。在这种情况下,返回的值将不是当前目录,而是分配的值。您可以通过引用当前目录来阻止这种情况。
\)do setcurpath = %%〜fa
code>%__ CD __%变量具有相同的行为。它可以被用户覆盖,但至少从Windows 7(我不能在Vista上测试),允许对%__ CD __%
的任何更改,但当变量被读取,被改变的值被忽略并且检索到正确的当前活动目录(注意:使用 set
命令仍然可以看到改变的值)。
但是所有先前的代码都会返回当前活动目录 ,而不是存储批处理文件的目录。
setcurpath =%〜dp0
它将返回存储批处理文件的目录,带有结尾反斜杠。
如果在批处理文件中使用
shift
命令shift
echo%〜dp0
到批处理文件已被移动,对当前批处理文件的
%0
引用丢失。
为防止这种情况,您可以在任何移动之前检索批处理文件的引用,或将语法更改为
shift / 1
以确保移位操作将从第一个参数开始,而不影响对批处理文件的引用。如果您不能使用任何此选项,您可以在调用子例程中检索对当前批处理文件的引用@echo off
setlocal enableextensions
rem销毁批处理文件引用
shift
echo批处理文件夹为%〜dp0
rem调用子程序获取批处理文件夹
call:getBatchFolder batchFolder
echo批处理文件夹是%batchFolder%
exit / b
:getBatchFolder returnVar
设置%〜1 =%〜dp0& exit / b
如果调用批处理文件名时引用了这种方法,不使用引用(请参阅此处)。
I use this script to find out the current folder with its .bat file:
for /f %%i in ("%0") do set curpath=%%~dpi echo %curpath%
it doesn't work correctly, if the path contains spaces(D:\Scripts\All Scripts -> retrieves only D:\Scripts\, if I place in the folder, whose path doesn't have spaces it retrieves the full path). How can I fix it?
解决方案2015-03-30: Edited - Missing information has been added
To retrieve the current directory you can use the dynamic
%cd%
variable that holds the current active directoryset "curpath=%cd%"
This generates a value with a ending backslash for the root directory, and without a backslash for the rest of directories. You can force and ending backslash for any directory with
for %%a in ("%cd%\") do set "curpath=%%~fa"
Or you can use another dynamic variable:
%__CD__%
that will return the current active directory with an ending backslash.Also, remember the
%cd%
variable can have a value directly assigned. In this case, the value returned will not be the current directory, but the assigned value. You can prevent this with a reference to the current directoryfor %%a in (".\") do set "curpath=%%~fa"
Up to windows XP, the
%__CD__%
variable has the same behaviour. It can be overwritten by the user, but at least from windows 7 (i can't test it on Vista), any change to the%__CD__%
is allowed but when the variable is readed, the changed value is ignored and the correct current active directory is retrieved (note: the changed value is still visible using theset
command).BUT all the previous codes will return the current active directory, not the directory where the batch file is stored.
set "curpath=%~dp0"
It will return the directory where the batch file is stored, with an ending backslash.
BUT this will fail if in the batch file the
shift
command has been usedshift echo %~dp0
As the arguments to the batch file has been shifted, the
%0
reference to the current batch file is lost.To prevent this, you can retrieve the reference to the batch file before any shifting, or change the syntax to
shift /1
to ensure the shift operation will start at the first argument, not affecting the reference to the batch file. If you can not use any of this options, you can retrieve the reference to the current batch file in a call to a subroutine@echo off setlocal enableextensions rem Destroy batch file reference shift echo batch folder is "%~dp0" rem Call the subroutine to get the batch folder call :getBatchFolder batchFolder echo batch folder is "%batchFolder%" exit /b :getBatchFolder returnVar set "%~1=%~dp0" & exit /b
This approach can also be necessary if when invoked the batch file name is quoted and a full reference is not used (read here).
这篇关于查找当前文件夹的路径 - cmd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!