BAT file to open CMD in current directory

2019-01-29 20:01发布

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 directory they are in. This is tedious (they are in a rather deep file system, so typing out the full path is a pain, copy+paste is better but not much). I tried to create a .BAT file that I could double-click on that would open a new command-line window in the folder the .bat file exists in but it does not work. It opens a new window, but the working directory is not the directory that .bat file is in. Here's what I've got after much googling (My cmd skills ain't so great):

cd %CD%
cmd.exe

I know from when I used Linux that Konqueror had a "Command-line window here" feature, and that's the effect I'm trying to get on Windows.

16条回答
Melony?
2楼-- · 2019-01-29 20:47

You can just enter cmd into the address bar in Explorer and it starts up in that path. Likewise for PowerShell.

查看更多
贪生不怕死
3楼-- · 2019-01-29 20:47

this code works for me name it cmd.bat

@echo off
title This is Only A Test
echo.
:Loop
set /p the="%cd%"
%the%
echo.
goto loop
查看更多
\"骚年 ilove
4楼-- · 2019-01-29 20:49

Create a new file startCmdLine.bat in your directory and put this line in it

call cmd

That is it. Now double click on the .bat file. It works for me.

You can replace call with start, it will also work.

查看更多
ら.Afraid
5楼-- · 2019-01-29 20:55

you probably want to do this:

cd /d %~dp0
cmd.exe

this will set your current directory to the directory you have the batch file in

查看更多
爷、活的狠高调
6楼-- · 2019-01-29 20:55

May be a little old quest, but for the ones like me googling for this, you can hold shift and right click the folder. That way the content menu ill show open command window here.

查看更多
劳资没心,怎么记你
7楼-- · 2019-01-29 20:55

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/

查看更多
登录 后发表回答