Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Trying to understand behavior of current directory and PATH variable.


02 Apr 2012   #1

Windows 7 Professional 64 bit
 
 
Trying to understand behavior of current directory and PATH variable.

The PATH variable on my machine is set to

C:\Perl64\site\bin;C:\Perl64\bin;C:\Windows\System32;C:\Program Files\Java\jdk1.7\bin;C:\Program Files (x86)\Notepad++;


I have a batch file (batchfile.bat) with the following commands -
dir
path
notepad++ source.frm -n2150
PAUSE

When I double click the batch file, I get the following output.

Code:
C:\ExcelExperiments>dir
 Volume in drive C has no label.
 Volume Serial Number is FC18-1C2F

 Directory of C:\ExcelExperiments

04/02/2012  12:42 PM    <DIR>          .
04/02/2012  12:42 PM    <DIR>          ..
04/02/2012  12:43 PM                45 batchfile.bat
04/02/2012  12:32 PM                32 Commands.txt
04/02/2012  12:28 PM            16,793 Excelfile.xlsx
03/09/2012  12:44 PM           308,196 source.frm
               4 File(s)        325,066 bytes
               2 Dir(s)  858,609,614,848 bytes free

C:\ExcelExperiments>path
PATH=C:\Perl64\site\bin;C:\Perl64\bin;C:\Windows\System32;C:\Program Files\Java\jdk1.7\bin;C:\Program Files (x86)\Notepad++;

C:\ExcelExperiments>notepad++ source.frm -n2150

C:\ExcelExperiments>PAUSE
Press any key to continue . . .
So far so good. Now, in the Excelfile.xlsx which is part of the current directory, I have a hyperlink that links to the batch file. However, when I click on this hyperlink, the current directory somehow changes to the Documents directory. Also, an additional entry is made temporarily to the PATH variable-


Code:
C:\Users\owner\Documents>dir
 Volume in drive C has no label.
 Volume Serial Number is FC18-1C2F

 Directory of C:\Users\owner\Documents

03/20/2012  04:54 PM    <DIR>          .
03/20/2012  04:54 PM    <DIR>          ..
02/08/2012  10:11 AM    <DIR>          Aptana Studio 3 Workspace
02/16/2012  03:37 PM    <DIR>          My Kindle Content
03/19/2012  11:05 AM    <DIR>          My Meetings
02/22/2012  01:43 PM    <DIR>          NetBeansProjects
02/16/2012  04:04 PM    <DIR>          SQL Server Management Studio
02/16/2012  04:02 PM    <DIR>          Visual Studio 2005
03/12/2012  12:06 PM    <DIR>          Visual Studio 2008
03/12/2012  12:23 PM    <DIR>          Visual Studio 2010
               0 File(s)              0 bytes
              10 Dir(s)  858,609,233,920 bytes free

C:\Users\owner\Documents>path
PATH=C:\Program Files (x86)\Microsoft Office\Office14\;C:\Perl64\site\bin;C:\Perl64\bin;C:\Windows\System32;C:\Program Files\Java\jdk1.7\bin;C:\Program Files (x86)\Notepad++;

C:\Users\owner\Documents>notepad++ source.frm -n2150

C:\Users\owner\Documents>PAUSE
How to prevent this changing of the current directory? Also, why is the additional entry made to the PATH variable?

My System SpecsSystem Spec
.

02 Apr 2012   #2

Windows 7 Ultimate x64
 
 

Don't know why it chooses the Documents folder, if the Excel file existed there then that would be my answer but it doesn't.

One way around this is to change the directory before running the other cmds. The following code will change to the directory that the batch file is launched in.
Code:
cd "%~dp0"
There is a number of other path expansion variables too. I attached a batch file I made ages ago for reference and I thought you might like it for future reference as well.
Quote   Quote: Originally Posted by SmartShark View Post
Also, why is the additional entry made to the PATH variable?
Must have something to do with being launched from within Excel, not sure exactly why or if you can stop it.


Attached Files
File Type: bat PathExpansions.bat (510 Bytes, 11 views)
My System SpecsSystem Spec
02 Apr 2012   #3

Windows 7 Professional 64 bit
 
 

Quote   Quote: Originally Posted by Duzzy View Post
One way around this is to change the directory before running the other cmds. The following code will change to the directory that the batch file is launched in.
Code:
cd "%~dp0"
Thanks for this tip. I still don't understand why the current directory changes though.
My System SpecsSystem Spec
.


02 Apr 2012   #4

Windows 7 Ultimate x64
 
 

I can only assume it has something to do with the way Excel handles the running of the batch file.
My System SpecsSystem Spec
Reply

 Trying to understand behavior of current directory and PATH variable.




Thread Tools



Similar help and support threads for2: Trying to understand behavior of current directory and PATH variable.
Thread Forum
Variable SLEEP behavior. Performance & Maintenance
accidentally deleted PATH variable General Discussion
Solved Current working directory is read-only General Discussion
RegSvr32 not finding dll in current directory, dll exists nowhere else General Discussion
Path Variable For Installing Dig Instead OF Using NSLookup Software
Solved Cannot find the system variable path. General Discussion

Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 11:20 AM.
Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App
  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33