找到当前文件夹的路径 - cmd [英] find path of current folder - cmd

查看:31
本文介绍了找到当前文件夹的路径 - cmd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此脚本找出当前文件夹及其 .bat 文件:

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% 

它不能正常工作,如果路径包含空格(D:ScriptsAll Scripts -> 只检索 D:Scripts,如果我放在文件夹中,它的路径没有空格它会检索完整路径).我该如何解决?

it doesn't work correctly, if the path contains spaces(D:ScriptsAll 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 - 添加了缺失信息

要检索当前目录,您可以使用保存当前活动目录的动态 %cd% 变量

To retrieve the current directory you can use the dynamic %cd% variable that holds the current active directory

set "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"

或者您可以使用另一个动态变量:%__CD__%,它将返回带有结束反斜杠的当前活动目录.

Or you can use another dynamic variable: %__CD__% that will return the current active directory with an ending backslash.

另外,记住 %cd% 变量可以直接赋值.在这种情况下,返回的值将不是当前目录,而是分配的值.您可以通过引用当前目录来防止这种情况

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 directory

for %%a in (".") do set "curpath=%%~fa"

在 Windows XP 之前,%__CD__% 变量具有相同的行为.它可以被用户覆盖,但至少从 Windows 7 开始(我无法在 Vista 上测试它),允许对 %__CD__% 进行任何更改,但是当读取变量时,更改的值被忽略并检索正确的当前活动目录(注意:使用 set 命令更改的值仍然可见).

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 read, the changed value is ignored and the correct current active directory is retrieved (note: the changed value is still visible using the set 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.

但是如果在批处理文件中使用了 shift 命令,这将失败

BUT this will fail if in the batch file the shift command has been used

shift
echo %~dp0

由于批处理文件的参数已移动,对当前批处理文件的 %0 引用丢失.

As the arguments to the batch file has been shifted, the %0 reference to the current batch file is lost.

为了防止这种情况,您可以在任何移位之前检索对批处理文件的引用,或者将语法更改为 shift/1 以确保移位操作将从第一个参数开始,而不影响参考批处理文件.如果您不能使用这些选项中的任何一个,您可以在调用子程序时检索对当前批处理文件的引用

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆