从路径中删除当前工作目录 [英] Removing the current working directory from the path

查看:110
本文介绍了从路径中删除当前工作目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Windows中工作,想知道是否可以从路径中删除当前工作目录吗?我知道这是PowerShell中的默认行为,但是我需要它可以批量工作或在Windows命令行中工作.

I'm working in Windows and would like to know if there is a way to remove the current working directory from the path? I understand that this is the default behavior in PowerShell, but I need it to work in batch or at the Windows command-line.

在UNIX中,我只需确保我的$PATH变量不包含..有什么方法可以分批完成吗?这是当前行为:

In UNIX, I would just make sure that my $PATH variable not contain .. Is there any way to accomplish this in batch? This is the current behavior:

H:\tmp>dir
 Volume in drive H has no label.
 Volume Serial Number is E29C-7B61

 Directory of H:\tmp

04/27/2018  10:39 AM    <DIR>          .
04/27/2018  10:39 AM    <DIR>          ..
04/27/2018  10:40 AM                37 dwk.bat
               1 File(s)             37 bytes
               2 Dir(s)  987,995,770,880 bytes free

H:\tmp>dwk.bat
dwk.bat has been run.

H:\tmp>

这是所需的行为:

H:\tmp>dwk.bat
'dwk.bat' is not recognized as an internal or external command,
operable program or batch file.

H:\tmp>.\dwk.bat
dwk.bat has been run.

H:\tmp>

谢谢.

推荐答案

我建议先阅读有关Stack Overflow问题的答案:

I recommend first reading the answers on Stack Overflow questions:

  • Where is "START" searching for executables?
  • What is the reason for '...' is not recognized as an internal or external command, operable program or batch file?

非常感谢 eryksun ,因为如果没有

Many thanks to eryksun because of this answer would not exist without his comment on above referenced answer.

下一步,我建议阅读Microsoft开发人员网络(MSDN)文章:

Next I recommend reading the Microsoft Developer Network (MSDN) articles:

  • Naming Files, Paths, and Namespaces
  • NeedCurrentDirectoryForExePath function

可以通过以下方式回答问题:是的,可以在桌面应用程序和批处理文件上

The question can be answered with: Yes, it is possible for desktop applications and batch files on

  • Windows Vista和所有更高版本的Windows客户端版本,以及
  • Windows Server 2003和所有更高版本的Windows Server.

必须使用任何值定义名称为NoDefaultCurrentDirectoryInExePath的环境变量,以防止执行当前存储的脚本(.bat,.cmd,.vbs,...)或应用程序(.com,.exe).目录,而无需在Unix/Linux上显式使用.\.

An environment variable with name NoDefaultCurrentDirectoryInExePath must be defined with any value to prevent execution of a script (.bat, .cmd, .vbs, ...) or an application (.com, .exe) stored in current directory without explicitly using .\ as required on Unix/Linux.

环境变量NoDefaultCurrentDirectoryInExePath可以定义为 system 变量,以关闭在当前目录中搜索该计算机上所有帐户的脚本或应用程序.但这肯定不是一个好主意,因为它肯定会导致许多应用程序(包括安装程序和卸载程序)无法正常运行.

The environment variable NoDefaultCurrentDirectoryInExePath can be defined as system variable to turn off searching in current directory for a script or application for all accounts on this machine. But this is surely no good idea as it will result definitely in many applications including installers and uninstallers won't work anymore correct.

可以将环境变量NoDefaultCurrentDirectoryInExePath定义为 user 变量,以关闭在当前目录中搜索使用该帐户的进程的脚本或应用程序.但这当然也不是一个好主意.

The environment variable NoDefaultCurrentDirectoryInExePath can be defined as user variable to turn off searching in current directory for a script or application for processes using this account. But this is surely also no good idea.

但是在某些用例中将环境变量NoDefaultCurrentDirectoryInExePath设置为 local 变量可以有意义,以关闭当前目录中的脚本或应用程序搜索,而无需在Windows上显式使用.\具有NeedCurrentDirectoryForExePath内核功能的版本,cmd.exe在搜索文件名字符串中不包含反斜杠\(或正斜杠/)的脚本文件或应用程序之前调用cmd.exe的版本.

But it can make sense to set the environment variable NoDefaultCurrentDirectoryInExePath as local variable in some use cases to turn off searching in current directory for a script or application without explicitly using .\ on Windows versions with kernel function NeedCurrentDirectoryForExePath which cmd.exe calls before searching for a script file or application not containing a backslash \ (or a forward slash /) in file name string.

示例:

@echo off
pushd "%TEMP%"
set "NoDefaultCurrentDirectoryInExePath=0"

echo @echo %%0 executed successfully.>Test1.bat

echo Calling Test1.bat ...
call Test1.bat

echo Calling .\Test1.bat ...
call .\Test1.bat

echo Starting Test1.bat ...
start /wait Test1.bat ^& timeout 5

set "NoDefaultCurrentDirectoryInExePath="

echo Calling again Test1.bat ...
call Test1.bat

del Test1.bat
popd
pause

从命令提示符窗口中执行的此批处理文件将导致当前控制台窗口的输出:

This batch file executed from within a command prompt window results in output of current console window:

Calling Test1.bat ...
'Test1.bat' is not recognized as an internal or external command,
operable program or batch file.
Calling .\Test1.bat ...
.\Test1.bat executed successfully.
Starting Test1.bat ...
Calling again Test1.bat ...
Test1.bat executed successfully.
Press any key to continue . . . 

在执行此批处理文件期间,将打开第二个控制台窗口,并显示以下输出:

And during execution of this batch file a second console window is opened with output:

"%TEMP%\Test1.bat" executed successfully.

第二个控制台窗口在5秒钟后自动关闭.

This second console window is closed automatically after 5 seconds.

在将临时文件的目录设置为当前目录并在堆栈上推送当前目录路径之后,将环境变量NoDefaultCurrentDirectoryInExePath定义为值0.变量值无关紧要,因为所评估的只是环境变量的存在而不是其值.

The environment variable NoDefaultCurrentDirectoryInExePath is defined with value 0 after setting directory for temporary files as current directory with pushing current directory path on stack. The variable value does not matter because of evaluated is only existence of environment variable and not its value.

接下来在目录中为临时文件创建另一个名称为Test1.bat的批处理文件,该文件通常不受当前用户的写保护,因为这会引起很多麻烦.

Next another batch file with name Test1.bat is created in directory for temporary files which is usually not write-protected for current user as this would cause lots of troubles.

由于环境变量NoDefaultCurrentDirectoryInExePath是在本地环境中定义的,因此没有任何路径调用Test1.bat的第一种方法会失败.

The first approach to call Test1.bat without any path fails because of environment variable NoDefaultCurrentDirectoryInExePath is defined in local environment.

尽管存在环境变量,但使用相对路径.\Test1.bat的第二次调用仍成功.

The second call of Test1.bat with relative path .\ is successful despite existence of the environment variable.

命令 START 忽略了此批处理文件证明的NoDefaultCurrentDirectoryInExePath.

The command START ignores NoDefaultCurrentDirectoryInExePath as proven by this batch file.

然后将环境变量NoDefaultCurrentDirectoryInExePath删除,以恢复原始Windows行为.

Then the environment variable NoDefaultCurrentDirectoryInExePath is deleted to restore original Windows behavior.

在没有任何路径的情况下调用Test1.bat的第二种方法现在成功了.

The second approach to call Test1.bat without any path is successful now.

最后,删除创建的Test1.bat,并将初始当前目录恢复为当前目录.

Finally the created Test1.bat is deleted and initial current directory is restored as current directory.

当然不可能阻止执行不是脚本文件或可执行文件的命令 DIR .它是cmd.exe – Windows命令处理器–和powershell.exe – Windows PowerShell的内部命令.

It is of course not possible to prevent execution of command DIR which is not a script file or an executable. It is an internal command of cmd.exe – Windows Command Processor – respectively of powershell.exe – Windows PowerShell.

这篇关于从路径中删除当前工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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