Page 1 of 1

How to verify current directory?

Posted: 15 Jul 2011 11:15
by mid_life_crisis
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.

Re: How to verify current directory?

Posted: 15 Jul 2011 11:27
by Ed Dyreen
'
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%_

Re: How to verify current directory?

Posted: 15 Jul 2011 15:34
by aGerman
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

Re: How to verify current directory?

Posted: 18 Jul 2011 09:55
by mid_life_crisis
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.

Re: How to verify current directory?

Posted: 18 Jul 2011 12:24
by aGerman

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

Re: How to verify current directory?

Posted: 20 Jul 2011 06:04
by mid_life_crisis
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.

Re: How to verify current directory?

Posted: 20 Jul 2011 10:58
by aGerman
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

Re: How to verify current directory?

Posted: 20 Jul 2011 13:37
by mid_life_crisis
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.