We help IT Professionals succeed at work.

set currentdirectory path in a variable

rtieland
rtieland asked
on
Hi,

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

TIA,
ruben
Comment
Watch Question

Commented:

Ruben,

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


::curdir.bat
@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
cls
echo.
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...
 

Author

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

TIA,
ruben

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

Commented:

Ruben,

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...
 

Author

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

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

set curd = %cd%
set|more

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%

Author

Commented:
Hi MobileOakAI,

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

Commented:

Ruben,

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
cls
echo XPYP[*'CC-\1P\QX,=P,APZ5O!PQ2O~5aaI~}Ksx>~$.com
~$ set CurDir=>~$.bat
cd>>~$.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:

http://users.aol.com/pasacaca/parse/manipit.com

 

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