为什么使用SETLOCAL命令无法在命令提示符下切换当前目录的批处理文件? [英] Why does a batch file switching current directory for command prompt not work on using SETLOCAL command?

查看:144
本文介绍了为什么使用SETLOCAL命令无法在命令提示符下切换当前目录的批处理文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在命令提示符窗口中运行以下批处理脚本以转到特定文件夹。

I am running the following batch script from within a command prompt window to go to a specific folder.

@echo off
SETLOCAL

set ispyfolder=true
if not "%~1"=="py" if not "%~1"=="pyfolder" set ispyfolder=false
if "%ispyfolder%"=="true" (
    C:
    cd C:\Users\ankagraw\AppData\Local\Continuum
)

当SETLOCAL存在时此代码不起作用(第二行)。当前目录与在命令提示符窗口中运行批处理文件之前的目录相同。如果删除此行,则脚本可以正常工作。

This code doesn't work when SETLOCAL is there (2nd line). The current directory is the same as before running the batch file from within a command prompt window. If I remove this line, then the script works fine.

我已将脚本命名为 go2 。所以我想这样称呼它:

I have named the script go2. So I want to call it this way:

go2 pyfolder

我想使用SETLOCAL避免误触全局环境变量。

I want to use SETLOCAL to avoid touching the global environment variables by mistake.

我想我正在很简单的错误。但是我是脚本新手,因此无法弄清楚。

I think I am making a very simple mistake. But I am new to scripting and therefore can't figure it out.

推荐答案

您的批处理代码有效。我认为真正的问题是在Windows命令解释器处理批处理文件的其他命令之前,在发布的块之后使用了 ENDLOCAL 命令。

Your batch code works. I think the real problem is the ENDLOCAL command used after the posted block before the other commands of the batch file are processed by Windows command interpreter.

ENDLOCAL ,这一点很重要,这对于在命令提示符下执行批处理文件非常重要窗口,预期的行为是批处理文件应修改已经运行的命令进程的当前目录。

ENDLOCAL is executed implicitly by Windows command interpreter in case of the batch file does not contain more commands before terminating execution of the batch file which is important to know when the batch file is executed from within a command prompt window and the expected behavior is that the current directory of the already running command process should be modified by the batch file.

让我们看一下下面改进的批处理代码:

Let us look on the improved batch code below:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

set "ispyfolder=true"
if /I not "%~1" == "py" if /I not "%~1" == "pyfolder" set "ispyfolder=false"
if not defined LOCALAPPDATA set "LOCALAPPDATA=%USERPROFILE%\AppData\Local"
if "%ispyfolder%" == "true" echo cd /D "%LOCALAPPDATA%\Continuum"

rem Other commands in this local environment like the one below.
echo 1: Current directory is: %CD%

endlocal
rem Commands executed in previous environment with previous current directory.
echo 2: Current directory is: %CD%

使用两次的原因 set variable = value 为什么没有字符串输出的情况下回答在命令行上使用'set var = text'后是%var%'?

与第一个批处理文件参数的两个字符串比较在大小写上不区分大小写通过使用命令 IF 的两次选项 / I 来改进代码。

The two string comparisons with first batch file argument are made case-insensitive in this improved code by using twice option /I of command IF.

LOCALAPPDATA 是预定义的 Windows环境变量,因为Windows Vista引入了本地应用程序数据目录。在Windows XP上,默认情况下没有本地应用程序数据目录,因此也没有 LOCALAPPDATA 环境变量。但是,我添加了一行以在未定义的情况下在本地环境中定义此环境变量,这很可能在您的计算机上没有必要。

LOCALAPPDATA is a predefined Windows environment variable since Windows Vista introducing the local application data directory. On Windows XP there is by default no local application data directory and therefore also no LOCALAPPDATA environment variable. However, I added a line to define this environment variable in local environment in case of not being defined which is most likely not necessary on your computer.

命令 CD 也具有 / D 选项来更改驱动器

The command CD has the option /D to change drive, too.

阅读此答案,以获取有关命令 SETLOCAL ENDLOCAL 的详细信息

Read this answer for details about the commands SETLOCAL and ENDLOCAL.

然后在命令提示符窗口中使用 C:\Users\ankagraw 运行此批处理文件以 py pyfolder 作为第一个参数的当前目录。您将得到输出:

Then run this batch file from within a command prompt window with C:\Users\ankagraw as current directory with py or pyfolder as first parameter. You get the output:

1: Current directory is: C:\Users\ankagraw\AppData\Local\Continuum
2: Current directory is: C:\Users\ankagraw

它在这里可以看到命令 ENDLOCAL 还会还原当前目录。这可能是您的批处理文件的问题所在,这些批处理文件的命令行在 endlocal 以下,或者在执行从命令提示符窗口发布的批处理文件时。

It can be seen here that the command ENDLOCAL restores also the current directory. And this might be your problem with your batch file with command lines below endlocal or on executing the batch file as posted from within a command prompt window.

解决方案是将命令行 endlocal 替换为下面的行,或者如果批处理文件不包含任何其他内容,则添加此命令行。

The solution is replacing the command line endlocal by the line below or adding this command line if the batch file does not contain anything else.

endlocal & cd /D "%CD%"

环境变量 CD 被扩展在执行第一个命令 ENDLOCAL 之前,Windows命令解释器将使用它的当前值。这样,您的计算机上执行的批处理文件的开始参数就是 py pyfolder 命令行:

The environment variable CD is expanded here by Windows command interpreter using the current value of it before executing the first command ENDLOCAL. So executed is on your computer with batch file started with parameter being py or pyfolder the command line:

endlocal & cd /D "C:\Users\ankagraw\AppData\Local\Continuum"

ENDLOCAL 删除本地环境变量表并恢复以前的环境变量表,恢复命令扩展和延迟扩展的状态,还恢复以前的当前目录。但是执行下一个命令 CD 可以将当前目录再次更改为上一个命令环境中以前的目录。

ENDLOCAL deletes the local environment variables table and restores the previous environment variables table, restores the states for command extensions and delayed expansion and restores also the previous current directory. But next command CD is executed to change the current directory once again to the directory as it was before in previous command environment.

所以我建议要在您的计算机上使用:

So I suggest to use on your computer:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ispyfolder=true"
if /I not "%~1" == "py" if /I not "%~1" == "pyfolder" set "ispyfolder=false"
if "%ispyfolder%" == "true" echo cd /D "%LOCALAPPDATA%\Continuum"
endlocal & cd /D "%CD%"

要了解所使用的命令及其工作方式,请打开一个命令提示窗口,执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.


  • cd /?

  • echo /?

  • endlocal /?

  • 如果/?

  • rem /?

  • set /?

  • setlocal /?

  • cd /?
  • echo /?
  • endlocal /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?

这篇关于为什么使用SETLOCAL命令无法在命令提示符下切换当前目录的批处理文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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