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
SmartShark

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
Duzzy

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
SmartShark

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
Duzzy

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
Thread Forum
Non-existant directories in System PATH variable
"echo %PATH%" gives me a list of 32 directories. 12 of these don't exist on my system, for example: C:\Windows\System32\WindowsPowerShell\v1.0\ Is there any reason to keep these directories in my System PATH variable? Is something likely to come along wanting to use one of these directories?...
General Discussion
Variable SLEEP behavior.
Running Win 7 Home Premium on a custom desktop, all updates applied. Regardless of what happens to be running at the time, when I issue the SLEEP command at night, the HD stops spinning, the monitor goes dark, and the power light starts blinking. Then the following morning: 1) 5% of the time,...
Performance & Maintenance
accidentally deleted PATH variable
Not sure if this is the right forum, I don't see any forum for troubleshooting. I was trying to edit my PATH environment variable and accidentally deleted it. Is there any way to recover it? If not, is there anyone out there who can tell me what the default path variable should be? I'm afraid to...
General Discussion
Path Variable For Installing Dig Instead OF Using NSLookup
Hey folks Just wondering if anyone can tell me if it is OK to have (create) a second PATH environment variable for Dig. I currently have 1 PATH variable already being c:\Program Files (x86)\Nmap. In the instructions for installing Dig found here dig, a DNS query tool for Windows and...
Software
Cannot find the system variable path.
Hi. Running windows 7 ultimate 32bit. I want to add to path per this below, but I cannot find the path per the info here: How do I that ?
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 17:22.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App