We help IT Professionals succeed at work.

set currentdirectory path in a variable

rtieland asked

how do i set the currentdirectory in a variable?
something like set x=...

Watch Question



Here is an ms-dos batch file that will set a value having the current directory
to an environment variable named CurDir ...

@echo off
echo @prompt set CurDir=$p>~curdir1.bat
%comspec%/c ~curdir1.bat>~curdir2.bat
call ~curdir2.bat
for %%x in (~curdir*.bat) do del %%x
echo The Current Directory and CurDir variable is:  %curdir%

The portion after the cls (clear screen) command
just echoes the contents of the variable to the screen...
Replace that with whatever you need to do with the variable.

Hope this works for you... Please post back otherwise
or if you need another method or more about the batch script...


i am searching for a solution that doesn't need write access to a file.


If memory is not an issue, you could create a RAMdisk and use rin1010's solution.



Are you doing this at a command prompt or from a batch file or how?
I know of half a dozen methods under ms-dos but all require writing at least one file.
Also, do you need only the current directory name or its full path including the drive letter?
Would you want to use an external utility? And what is your OS?

If you're just setting a variable from the command prompt
you can type CD without parameters to display the current directory
and then type SET CurDir= followed by that value.

Windows 2K has an already set variable named CD having the current directory.
Under Windows NT you can use this one line that will set a variable
named CurDir to the current directory without writing a file:

for %%x in (.) do set CurDir=%%~fx

Unfortunately ms-dos doesn't have such built-in capabilities,
and requires complex workarounds and temp file creation, etc...
But post back with the OS and version this should work under
and whether a utility to do it would help...


the OS = win98se and you may use cd or chdir as utilities.
both of them return the currentdirectory as i want it.

Original question:
> how do i set the currentdirectory in a variable?
> something like set x=...

set curd = %cd%

This one-liner works on my system, your results may vary.

Note. Flipping from too general to too specific makes this now sound very much like homework.

set CurDir = %cd%


Hi MobileOakAI,

your sollution does not work on win98, it works on win2k



You're correct about the %CD% variable being specific to Windows 2K ...
The suggestion from MobileOakAI only repeated what I previously said ...

Do you still need a method that doesn't require writing a file?
As noted, you'll need an external utility under Windows 98
but you didn't mention whether that would work for you...

If you can use a file creation method, here is an alternative
utilizing ascii machine code that you may prefer...
This sets an environment variable named CurDir
to a string containing the current directory :

@echo off
echo XPYP[*'CC-\1P\QX,=P,APZ5O!PQ2O~5aaI~}Ksx>~$.com
~$ set CurDir=>~$.bat
call ~$.bat
del ~$.*

If you need a solution for Windows 98 (or whatever os)
that doesn't require file creation or other disk writes,
I have a small .com on a server you can have a copy of
which will do as you need using only one command line...
And it "doesn't need write access to a file" as specified.

I call it manipit.com and it will set a variable having any valid name
to the complete path of the current directory without a disk write...

The syntax to set an environment variable named CurDir
to the full path of the current directory is:

manipit CurDir= truename .

You can replace CurDir with any valid variable name you choose...
But note the period at the end of the command... It is mandatory.

Download manipit.com and place it in your working directory
or in a directory in your path, such as the Windows\Command directory...
Hope it's helpful and accomplishes your task...

You can download a copy of manipit.com from here:



Explore More ContentExplore courses, solutions, and other research materials related to this topic.