64

How can I set a variable with the current location? For instance, if I get in c:\test and want to set the variable to test and if I get inside c:\test\test2 the variable will be set to test2.

I'm thinking about using a for to get inside a lot of folders and check if some file exists, if the correct file exist I want to set the current folder to a variable so I can copy the path and copy the folder.

The main problem deals with copying the rest of the files is the same folder as the .inf file.

3 Answers 3

118

The current directory is in the "shadow" variable cd.
You could try

set "var=%cd%"
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, it worked!! Although How do I do a for that get inside folders and subfolders and every level he add a variable to the %cd% .. so every folder the gets in the variable will be the current path ..
This is for someone like me just added extra spaces alongside with =. set "var = %cd%" Have to remove spaces.
For those complete unfamiliar with DOS batch scripting, it might be helpful to know that var in this example should be replaced with whatever name you choose for your variable, while %cd% should be left as-is. For example: set "currentDirectory=%cd%. Then use the variable elsewhere as %currentDirectory%. As mentioned, don't add spaces to this command.
17
%~dp0

This expands into the drive & path of the currently running batch file. I usually surround my batch files with something like:

@echo off
pushd %~dp0

...

popd

Edit: It seems I didn't understand the OP. My example gets the location of the currently running script, not the "Current Directory". +1 to jeb.

4 Comments

%~dp0 can be the current location, but only if the batch file is in the current directory. But your answer is helpful, as this is often the requirement
@massaki: it doesn't work from the command prompt; it works only in batch files. %dp0 is the drive & path of the 0'th argument to the batch file, which is the full path of the batch file itself.
FWIW I came here looking for the answer you provided - so the OP's title was close enough that I followed the link. Thanks.
Me too, Basic. I tried jeb 's command, but then realised that yours worked more efficiently my script. Thank you to everyone.
4

I think there is a little confussion here. %CD% always have the current directory, so you don't need to add anything to have it. However, by rereading your original question, I think that you need the LAST PART of the current directory, that is, the name of the current location excluding all previous locations. If so, then you may use this:

set i=0
:nextdir
set /a i+=1
for /f "tokens=%i% delims=\" %%a in ("%CD%") do if not "%%a" == "" set lastdir=%%a& goto nextdir
echo Current location: %lastdir%

1 Comment

I realize this is pretty old, but just wanted to let you know this errors with: delims=\" was unexpected at this time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.