BAT file to open CMD in current directory

前端 未结 17 1822
清酒与你
清酒与你 2020-12-22 18:09

I have many scripts which I interact with from the command line. Everytime I need to use them, I have to open a command line window and copy+paste and CD to the path to the

相关标签:
17条回答
  • 2020-12-22 18:38

    Create a file named open_dos_here.cmd with the following lines:

    %~d1
    cd "%~p1"
    call cmd
    

    Put this file at any folder. Then, go to your Send To folder (Win+E; Alt+D;shell:sendto;Enter). Create a shortcut to point to this open_dos_here.cmd

    Then, in any folder, select any file or sub-folder. Right-click and select "Send To" and then select open_dos_here.cmd to open the DOS in that folder.

    0 讨论(0)
  • 2020-12-22 18:38

    I'm thinking that if you are creating a batch script that relies on the Current Directory being set to the folder that contains the batch file, that you are setting yourself up for trouble when you try to execute the batch file using a fully qualified path as you would from a scheduler.

    Better to add this line to your batch file too:

    REM Change Current Directory to the location of this batch file 
    CD /D %~dp0
    

    unless you are fully qualifying all of your paths.

    0 讨论(0)
  • 2020-12-22 18:40

    As a more general solution you might want to check out the Microsoft Power Toy for XP that adds the "Open Command Window Here" option when you right-click: http://www.microsoft.com/windowsxp/downloads/powertoys/xppowertoys.mspx

    In Vista and Windows 7, you'll get that option if you hold down shift and right-click (this is built in).

    0 讨论(0)
  • 2020-12-22 18:40

    The simplest command to do this:
    start

    You can always run this in command line to open new command line window in the same location. Or you can place it in your .bat file.

    0 讨论(0)
  • 2020-12-22 18:42

    A bit late to the game but if I'm understanding your needs correctly this will help people with the same issue.

    Two solutions with the same first step: First navigate to the location you keep your scripts in and copy the filepath to that directory.

    First Solution:

    • Click "Start"
    • Right-click "Computer" (or "My Computer)
    • Click "Properties"
    • On the left, click "Advanced System Settings"
    • Click "Environment Variables"
    • In the "System Variables" Box, scroll down and select "PATH"
    • Click "Edit"
    • In the "Variable Value" field, scroll all the way to the right
    • If there isn't a semi-colon (;) there yet, add it.
    • Paste in the filepath you copied earlier.
    • End with a semi-colon.
    • Click "OK"
    • Click "OK" again
    • Click "OK" one last time

    You can now use any of your scripts as if you were already that folder.

    Second Solution: (can easily be paired with the first for extra usefulness)

    On your desktop create a batch file with the following content.

    @echo off
    cmd /k cd "C:\your\file\path"
    

    This will open a command window like what you tried to do.


    For tons of info on windows commands check here: http://ss64.com/nt/

    0 讨论(0)
提交回复
热议问题