将当前路径存储在变量中并重用它

时间:2021-09-13 23:14:10

I know similar questions have already been asked, but somehow I am unable to figure out the mistake in my code. I'm making a .bat file with the following code

我知道已经提出了类似的问题,但不知怎的,我无法弄清楚我的代码中的错误。我正在使用以下代码制作.bat文件

echo off 
echo %cd%
set curr_directory = "%cd%"
echo $curr_directory
pause

OUTPUT is :

输出是:

C:\Users\MyDesktop>echo off
C:\Users\MyDesktop>
$curr_directory
Press any key to continue . . .

So what I dont get is why the value of variable curr_directory is not being printed. What i eventually want to do is use the variable to change the directory something like this: cd $curr_directory

所以我没有得到的是为什么没有打印变量curr_directory的值。我最终想要做的是使用变量更改目录,如下所示:cd $ curr_directory

Thanks

2 个解决方案

#1


2  

I don't know where to start. EVERYTHING about your code is wrong. This should work:

我不知道从哪里开始。关于你的代码的一切都是错误的。这应该工作:

@echo off 
echo %cd%
set curr_directory=%cd%
echo %curr_directory%
pause

In batch you access variables via %var% and not $var. Further, DO NOT PUT SPACES behind =. SET x=123 will store 123 in x but SET x= 123 will store _123 (_ means space) in x.

在批处理中,您可以通过%var%而不是$ var访问变量。此外,请勿在=后面放置空间。 SET x = 123将在x中存储123,但SET x = 123将在x中存储_123(_表示空格)。

EDIT: As SomethingDark stated, the first line should be @echo off except you actually want the message echo off to be printed. And yes, SET x = 123 means %x % is 123

编辑:正如SomethingDark所述,第一行应该是@echo off,除非你真的希望打印消息echo。是的,SET x = 123表示%x%为123

#2


-1  

use %curr_directory% instead of $curr_directory

使用%curr_directory%而不是$ curr_directory

Avoid spaces inbetween like the below one

避免像下面那样的空间

"set curr_directory = %cd%"

“set curr_directory =%cd%”

below should work

下面应该工作

echo off 
echo %cd%
set curr_directory=%cd%
echo curr_directory is %curr_directory%
pause

#1


2  

I don't know where to start. EVERYTHING about your code is wrong. This should work:

我不知道从哪里开始。关于你的代码的一切都是错误的。这应该工作:

@echo off 
echo %cd%
set curr_directory=%cd%
echo %curr_directory%
pause

In batch you access variables via %var% and not $var. Further, DO NOT PUT SPACES behind =. SET x=123 will store 123 in x but SET x= 123 will store _123 (_ means space) in x.

在批处理中,您可以通过%var%而不是$ var访问变量。此外,请勿在=后面放置空间。 SET x = 123将在x中存储123,但SET x = 123将在x中存储_123(_表示空格)。

EDIT: As SomethingDark stated, the first line should be @echo off except you actually want the message echo off to be printed. And yes, SET x = 123 means %x % is 123

编辑:正如SomethingDark所述,第一行应该是@echo off,除非你真的希望打印消息echo。是的,SET x = 123表示%x%为123

#2


-1  

use %curr_directory% instead of $curr_directory

使用%curr_directory%而不是$ curr_directory

Avoid spaces inbetween like the below one

避免像下面那样的空间

"set curr_directory = %cd%"

“set curr_directory =%cd%”

below should work

下面应该工作

echo off 
echo %cd%
set curr_directory=%cd%
echo curr_directory is %curr_directory%
pause