SPanwar
SPanwar

Reputation: 11

storing current path in variable and reusing it

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

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

Thanks

Upvotes: 0

Views: 3523

Answers (2)

TamizhanDa
TamizhanDa

Reputation: 1

use %curr_directory% instead of $curr_directory

Avoid spaces inbetween like the below one

"set curr_directory = %cd%"

below should work

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

Upvotes: -1

MichaelS
MichaelS

Reputation: 6032

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.

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

Upvotes: 4

Related Questions