batch file changes (for Vista) RRS feed

  • Question

  • Hi all,

    I had until now, and worked fine on WinXP the following:
    - into my application directory, C:\Program Files\MyApp a second_app.exe which needs admin rights to run
    - into the same dir I have: MyTrace.bat which contains the following lines:
      second_app.exe > MyTrace.txt
      second_app.exe >> MyTrace.txt
    - when a user was into a very-specific trouble (meaning very seldom), he had to double-click the MyTrace.bat which would generate in the same dir the MyTrace.txt, which then should be sent somewhere.

    Now, this scenario doesn't work on Vista b/c:
    - if I double-click on the MyTrace.bat the creation of the MyTrace.txt fails as it's in Program Files and the console's ">" and ">>" will fail
    - if I right-click on the batch and say "Run as admin" the console will actually prompt to system32, so the second_app.exe won't be found to be run

    I know of course that I could change the .bat to:
      second_app.exe > %TEMP%\MyTrace.txt
      second_app.exe >> %TEMP%\MyTrace.txt
    and it would work, but I would like to find a method, so that the MyTrace.txt to be generated in the same dir where MyTrace.bat is. Is there a way to do that? (if for instance at the very beginning of the batch file I could somehow change the directory to the directory where this batch file is (don't know how to do that), and then the batch could be run with "Run as admin").

    Tuesday, June 12, 2007 1:37 PM

All replies

  • Have you tried using ./MyTrace.txt ?  This works for me.

    Thursday, November 15, 2007 8:17 AM
  • Use CD /d %~dp0

    http://www.vistax64.com/vista-general/79849-run-administrator-changes-default-directory.html has a good discussion.


    Thursday, January 10, 2008 6:56 AM
  • So adding a %~dp0 in front of things you do in the batch file give you location independence without having to do hard-coded paths or a lot of  ..\..'s all over the place. So the following will always find the "sometool.exe" sitting right next to the batch file no matter where it's run from.

    Note: As a best practice, include the quotes "%~dp0sometool.exe"
    this ensures that paths with spaces in them will be interpreted.

    Using your batch file for example...

       "%~dp0second_app.exe" > "%~dp0MyTrace.txt"
       "%~dp0second_app.exe" >> "%~dp0MyTrace.txt"

    This should work to create the output MyTrace.txt in the same folder as the second_app.exe



    Hope this helps,


    • Edited by GC-Tech Thursday, March 15, 2012 10:54 PM
    • Proposed as answer by GC-Tech Thursday, March 15, 2012 10:57 PM
    Thursday, March 15, 2012 10:53 PM