Trying to understand behavior of current directory and PATH variable.

SmartShark

New member
Local time
8:57 PM
Messages
3
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 Computer My Computer

OS
Windows 7 Professional 64 bit
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.
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.
 

Attachments

My Computer My Computer

Computer Manufacturer/Model Number
Self built
OS
Windows 7 Ultimate x64
CPU
Intel Pentium Dual Core E5200 2.5GHz (3.77GHz OC)
Motherboard
Asus P5Q-E
Memory
Corsair 4GB DDR2 (4x1GB CM2X1024-6400C4)
Graphics Card(s)
Palit GeForce GTS 250 (1024MB)
Sound Card
On Board (ADI AD2000B 8ch HD)
Monitor(s) Displays
Samsung 32in LCD TV
Screen Resolution
1360x768
Hard Drives
2 x 1TB Samsung 103SJ (Raid0)
2 x External 500GB Samsung 502IJ (NexStar 3 HD Enclosures)
PSU
550W Antec Neo HE 550
Case
Antec P180
Cooling
Xigmatex Red Scorpion CPU Cooler. 3x120mm Fans
Keyboard
Logitech MX5000 Laser (Combo)
Mouse
Logitech MX5000 Laser (Combo)
Internet Speed
ADSL2+ (avg 10 Mbps Down, 0.80 Mbps up)
Other Info
Gigabyte GN-WP01GS 54g Wireless Lan Card
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 Computer My Computer

OS
Windows 7 Professional 64 bit
I can only assume it has something to do with the way Excel handles the running of the batch file.
 

My Computer My Computer

Computer Manufacturer/Model Number
Self built
OS
Windows 7 Ultimate x64
CPU
Intel Pentium Dual Core E5200 2.5GHz (3.77GHz OC)
Motherboard
Asus P5Q-E
Memory
Corsair 4GB DDR2 (4x1GB CM2X1024-6400C4)
Graphics Card(s)
Palit GeForce GTS 250 (1024MB)
Sound Card
On Board (ADI AD2000B 8ch HD)
Monitor(s) Displays
Samsung 32in LCD TV
Screen Resolution
1360x768
Hard Drives
2 x 1TB Samsung 103SJ (Raid0)
2 x External 500GB Samsung 502IJ (NexStar 3 HD Enclosures)
PSU
550W Antec Neo HE 550
Case
Antec P180
Cooling
Xigmatex Red Scorpion CPU Cooler. 3x120mm Fans
Keyboard
Logitech MX5000 Laser (Combo)
Mouse
Logitech MX5000 Laser (Combo)
Internet Speed
ADSL2+ (avg 10 Mbps Down, 0.80 Mbps up)
Other Info
Gigabyte GN-WP01GS 54g Wireless Lan Card
Back
Top