locked
Commend Prompt on Windows 8/8.1 RRS feed

  • Question

  • Hey Guys I have a problem which occurs on only Windows 8/8.1

    Older versions have no problem like that.

    Whenever I normally run a script (.bat) file it works fine and it's current directory:

    C:\Users\%username%\Desktop>


    But if I run the same script as Administrator, it's current directory:

    C:\WINDOWS\System32>

    In some cases, especially I type code such as ".\" this problem occurs and I can never run it as Administrator.

    Any idea why is this happening and how can I get rid of this problem?

    My Best Regards

    Halit Şimşek


    Sunday, September 14, 2014 4:59 PM

Answers


  • This is not new to Windows 8/8.1. When running a batch file as an admin the default location is always the system32 folder. Well, it does in a .cmd file anyway, which is the format I always use. Luckily, there's a default variable you can use which contains the folder the script was run from! The syntax of this variable is %~dp0. I always start my batch scripts with this line:

    cd %~dp0

    This line basically tells the script to go to the folder where the batch file was launched from. If you're already there nothing will change, if you're not then you'll get the same experience as starting without admin rights. You can also use the variable in a path, but you should be aware of the fact that the last character in the variable is a backslash. So if you use it like this:

    %~dp0\SomeCommand.exe /parameter

    you might not get a good result. Strangely enough this will:

    %~dp0SomeCommand.exe /parameter

    Anyway, that's my two cents. I hope you'll find it usefull.

    MicaH
    http://itmicah.wordpress.com



    • Edited by MicaH_Z Monday, September 15, 2014 8:29 PM
    • Marked as answer by Halit Şimşek Saturday, September 20, 2014 8:14 AM
    Monday, September 15, 2014 8:18 PM

All replies

  • nope, but the "why" won't fix it for you. It may be time to spell out the path in your script. Or better yet, check out the second half of my signature below :)


    Sam Boutros, Senior Consultant, Software Logic, KOP, PA http://superwidgets.wordpress.com (Please take a moment to Vote as Helpful and/or Mark as Answer, where applicable) _________________________________________________________________________________ Powershell: Learn it before it's an emergency http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

    Sunday, September 14, 2014 5:09 PM
  • Use cd %userprofile% to set back to the home base.


    ¯\_(ツ)_/¯

    Sunday, September 14, 2014 7:47 PM
  • Hey Guys I have a problem which occurs on only Windows 8/8.1

    Older versions have no problem like that.

    Whenever I normally run a script (.bat) file it works fine and it's current directory:

    C:\Users\%username%\Desktop>


    But if I run the same script as Administrator, it's current directory:

    C:\WINDOWS\System32>

    In some cases, especially I type code such as ".\" this problem occurs and I can never run it as Administrator.

    Any idea why is this happening and how can I get rid of this problem?

    My Best Regards

    Halim

    The start-up working directories can differ like below:

    • Shortcuts to cmd.exe can have their own “start-in” directory.
    • As you’ve seen, the administrator context has its own.
    • Autorun settings can dictate the working directory.

    Here’s a registry change you can make to default set the working directory at startup of cmd.exe:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor

    Autorun    REG_SZ “command to start, i.e cd <dir>



    Sunday, September 14, 2014 8:15 PM
  • This works on all versions of Windows for ALL command prompts.

    REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v AutoRun /t REG_SZ /d "IF x"%COMSPEC%"==x%CMDCMDLINE% (cd /D c:\scripts)"


    ¯\_(ツ)_/¯

    Sunday, September 14, 2014 9:16 PM

  • This is not new to Windows 8/8.1. When running a batch file as an admin the default location is always the system32 folder. Well, it does in a .cmd file anyway, which is the format I always use. Luckily, there's a default variable you can use which contains the folder the script was run from! The syntax of this variable is %~dp0. I always start my batch scripts with this line:

    cd %~dp0

    This line basically tells the script to go to the folder where the batch file was launched from. If you're already there nothing will change, if you're not then you'll get the same experience as starting without admin rights. You can also use the variable in a path, but you should be aware of the fact that the last character in the variable is a backslash. So if you use it like this:

    %~dp0\SomeCommand.exe /parameter

    you might not get a good result. Strangely enough this will:

    %~dp0SomeCommand.exe /parameter

    Anyway, that's my two cents. I hope you'll find it usefull.

    MicaH
    http://itmicah.wordpress.com



    • Edited by MicaH_Z Monday, September 15, 2014 8:29 PM
    • Marked as answer by Halit Şimşek Saturday, September 20, 2014 8:14 AM
    Monday, September 15, 2014 8:18 PM
  • Dear MicaH

    I'm glad you are here and help us. Thanks for your answer that's what I was looking for. Now I have 3 more questions.

    1) Is there any difference between .bat and .cmd

    2) How do you call these MS-DOS scripts in IT departments and how popular is it nowadays

    3) What is the best scripting language that today we are using


    My Best Regards Halit

    Saturday, September 20, 2014 8:57 AM