Trying to understand behavior of current directory and PATH variable.


  1. Posts : 3
    Windows 7 Professional 64 bit
       #1

    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 Computer


  2. Posts : 640
    Windows 7 Ultimate x64
       #2

    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.
    SmartShark said:
    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.
    Trying to understand behavior of current directory and PATH variable. Attached Files
      My Computer


  3. Posts : 3
    Windows 7 Professional 64 bit
    Thread Starter
       #3

    Duzzy said:
    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


  4. Posts : 640
    Windows 7 Ultimate x64
       #4

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


 

  Related Discussions
Our Sites
Site Links
About 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 14:34.
Find Us