为什么我的CD%myVar的%被忽略? [英] why is my cd %myVar% being ignored?

查看:111
本文介绍了为什么我的CD%myVar的%被忽略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我保存 originalPath 我原来的路径,然后移动到另一个文件夹中。在结束的时候我做的 CD%originalPath%,这是行不通的。它停留在新的路径。

I save my original path in originalPath and then move to another folder. At the end when I do cd %originalPath%, it doesn't work. It stays in the new path.

我一直在使用 PUSHD%mypath中% POPD ,但它不工作尝试。

I have tried using pushd %myPath% and popd, but it doesn't work either.

C:\\ BatchTests \\ 有我的剧本和文件夹 Subfolder1

C:\BatchTests\ has my script and the folder Subfolder1.

我的脚本:

@echo off
set myPath=Subfolder1
set folderList=input.txt
REM set originalPath=%CD%
set originalPath=%~dp0  

REM pushd %myPath%
cd %myPath%
setlocal EnableDelayedExpansion

:process

REM The rest of my script

echo %originalPath%
REM echo %originalPath% prints: C:\BatchTests\
cd %originalPath%
REM popd
pause

为什么会出现这种情况?

Why is this happening?

推荐答案

上运行一个批处理文件,而不是固定可以包含一个或多个空格不知道路径。这意味着路径字符串应该像文件名被引用。

1. Quoting unknown paths

Paths not known on running a batch file and not being fixed could contain 1 or more spaces. This means path strings should be quoted like file names.

命令的 CD 默认情况下改变当前驱动器的当前目录中唯一的。

Command CD changes by default the current directory on current drive only.

选项 / D 对于当前目录切换到以字母任何驱动器上的目录应该总是用时启动一个批处理文件的当前目录是不固定的同一驱动器上作为临时使用当前目录。

Option /D to change current directory to a directory on any drive with a letter should be always used when current directory on starting a batch file is not fixed on same drive as temporarily used current directory.

该命令的 SETLOCAL 始终创建这是使用时,破坏了环境表的新副本的 ENDLOCAL 或退出批处理文件恢复previous表。同时命令扩展和延迟扩展的状态保存和恢复。请参见这个答案用一个例子来演示如何使用会发生什么 SETLOCAL ENDLOCAL

The command setlocal creates always a new copy of the environment table which is destroyed when using endlocal or exiting the batch file restoring previous table. Also the states of command extensions and delayed expansion is saved and restored. See this answer with an example demonstrating what happens on using setlocal and endlocal.

但另外的 SETLOCAL 也节省了当前目录和 ENDLOCAL 作为下code证明其恢复。

But additionally setlocal saves also current directory and endlocal restores it as the following code demonstrates.

@echo off
set "InitialPath=%CD%"
echo 1. Current directory is: %CD%
cd /D "%windir%\Temp"
echo 2. Current directory is: %CD%
setlocal
rem Change current directory to parent directory.
cd ..\
echo 3. Current directory is: %CD%
setlocal
rem Change current directory to root of current drive.
cd \
echo 4. Current directory is: %CD%
endlocal
echo 5. Current directory is: %CD%
endlocal
echo 6. Current directory is: %CD%
cd /D "%InitialPath%"
echo 7. Current directory is: %CD%
set "InitialPath="
pause

4。尾随空格和制表符分配到环境变量

由于没有使用到环境变量赋值双引号,命令的设置追加第一等号将结束行到环境变量,包括不可见的尾随空格和制表符之后的一切。有在线尾部的空格设置originalPath =%〜DP0 ,见为什么是使用命令行上的设置VAR =文字'?了解更多详细信息后,用回声%VAR%没有字符串输出。

4. Trailing spaces and tabs assigned to environment variable

With not using double quotes on assigning a value to an environment variable, command set appends everything after first equal sign up to end of line to the environment variable including not visible trailing spaces and tabs. There are trailing spaces on line set originalPath=%~dp0  , see answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? for more details.

您code改进,以避免上面列出的所有可能出现的问题:

Your code improved to avoid all the possible issues listed above:

@echo off
set "myPath=Subfolder1"
set "folderList=input.txt"
REM set "originalPath=%CD%"
set "originalPath=%~dp0"

REM pushd "%myPath%"
cd /D "%myPath%"
setlocal EnableDelayedExpansion

:process
REM The rest of my script

endlocal
echo %originalPath%
REM echo %originalPath% prints: C:\BatchTests\
cd /D "%originalPath%"
REM popd
pause

和取3点考虑和 aschipfl 还解释好了,下面会的工作了。

And taking point 3 into account and as aschipfl also explained well, following would work, too.

@echo off
set "myPath=Subfolder1"
set "folderList=input.txt"

setlocal EnableDelayedExpansion
cd /D "%myPath%"

:process
REM The rest of my script

endlocal
pause

这篇关于为什么我的CD%myVar的%被忽略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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