How to verify current directory?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mid_life_crisis
Posts: 22
Joined: 26 May 2011 07:56

How to verify current directory?

#1 Post by mid_life_crisis » 15 Jul 2011 11:15

I have a number of batch files that get used regularly by my coworkers, but now that Win7 is everywhere, I have encountered a problem. I want to keep it simple and have them just throw a folder on the desktop temporarily and run the batch from inside it then delete the folder afterwards. The problem is that files have to be copied from the source folder to other folders. Sometimes the computer changes the working directory to Windows\system32 or syswow64, depending. Sometimes it doesn't. If it does, the batch file fails because the files are not in that directory, they are in a folder on the desktop. In the interest of KISS, I just want to detect if the working directory is no longer the folder on the desktop and if that is the case, pop up a message saying "hey, you need to put the files where they belong yourself then run the update batch file."
I'm thinking there must be an environment variable that will return the working directory, but I can't find it. Any suggestions pointing me in the right direction will be appreciated.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to verify current directory?

#2 Post by Ed Dyreen » 15 Jul 2011 11:27

'
This is for XP, 7 may be different

Code: Select all

"HKLM\SYSTEM\ControlSet001\Control\Session Manager\Environment"
Just search registry for "Environment" you should find the ones that are important.
an environment variable that will return the working directory

Code: Select all

echo.tmp=%tmp%_

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to verify current directory?

#3 Post by aGerman » 15 Jul 2011 15:34

I think I know the problem. Those happen if you run the code as administrator. The easies way could be to change the working directory to the path of your batch:

Code: Select all

@echo off
cd /d "%~dp0"
:: your stuff here


If you want to compare the pathes you could use that:

Code: Select all

if /i "%~dp0"=="%cd%\" (
  echo same directory
) else (
  echo different directory
)

Regards
aGerman

mid_life_crisis
Posts: 22
Joined: 26 May 2011 07:56

Re: How to verify current directory?

#4 Post by mid_life_crisis » 18 Jul 2011 09:55

aGerman wrote:I think I know the problem. Those happen if you run the code as administrator. The easies way could be to change the working directory to the path of your batch:

Code: Select all

@echo off
cd /d "%~dp0"
:: your stuff here


Regards
aGerman


This indicates that even though Windows is running the batch file from a different directory, it remembers where it was started from.
I am a little confused as my work machine is Win7 and when I run a sample script as Admininstrator it does not appear to change directory.
I have Folder Options set to display full titles and the Window has System32 showing in the title bar, but both the variables you shared display the desktop where my test batch file is located.

This little test batch should show two different paths when run as Administrator, if I understand this correctly, but both lines return the desktop.

Code: Select all

@echo  %cd%\
@echo  "%~dp0"
pause


Thank you.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to verify current directory?

#5 Post by aGerman » 18 Jul 2011 12:24

Code: Select all

@echo  "%cd%\"
@echo  "%~dp0"
pause


My output as normal user:

Code: Select all

 "C:\Users\aGerman\Desktop\"
 "C:\Users\aGerman\Desktop\"

C:\Users\aGerman\Desktop>pause
Drücken Sie eine beliebige Taste . . .



Right-click > Run as administrator:

Code: Select all

 "C:\windows\system32\"
 "C:\Users\aGerman\Desktop\"

C:\windows\system32>pause
Drücken Sie eine beliebige Taste . . .

As you can see the 2nd runs in "C:\windows\system32\" even if the file is placed on my desktop.

Regards
aGerman

mid_life_crisis
Posts: 22
Joined: 26 May 2011 07:56

Re: How to verify current directory?

#6 Post by mid_life_crisis » 20 Jul 2011 06:04

I don't know what is going on with my computer. There must be a setting, or perhaps some domain thing going on, because even though the path shown in the title bar is system32, with or without the quotes, both commands return the desktop. Further, when testing a batch that moves files from the desktop folder the batch file started in, they are moved on my machine, whereas on customer machines (and I bet yours, too) they generate an error as not found, because the batch is looking for them in system32.

Thank you again.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to verify current directory?

#7 Post by aGerman » 20 Jul 2011 10:58

Have a look at your registry
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor
and
HKEY_CURRENT_USER\Software\Microsoft\Command Processor

If there is a value named AutoRun you should delete it. Restart the machine and try again ...

Regards
aGerman

mid_life_crisis
Posts: 22
Joined: 26 May 2011 07:56

Re: How to verify current directory?

#8 Post by mid_life_crisis » 20 Jul 2011 13:37

Nope, no AutoRun.
I give up. I'll just have to test it in the field.

Thank you for all your help. The two variables are a world of help.

Post Reply