问题描述
我将原始路径保存在 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
为什么会这样?
推荐答案
1.引用未知路径
运行批处理文件时未知且未修复的路径可能包含 1 个或多个空格.这意味着路径字符串应该像文件名一样被引用.
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.
Command 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 或退出批处理文件恢复以前的表时,该副本会被破坏.命令扩展和延迟扩展的状态也被保存和恢复.请参阅此答案,并举例说明使用 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 恢复它,如下面的代码所示.
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.分配给环境变量的尾随空格和制表符
在为环境变量赋值时不使用双引号,命令 set 将第一个等号后的所有内容附加到环境变量的行尾,包括不可见的尾随空格和制表符.set originalPath=%~dp0
行有尾随空格,请参阅 为什么没有带有 ' 的字符串输出在命令行上使用 'set var = text' 后回显 %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.
您的代码得到改进以避免上面列出的所有可能的问题:
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% 被忽略了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!