I have a .cmd a file and I want to write some code in it. But first of all I want a code to open the .cmd file path and when someone clicks my .cmd file, it's dir opens in command prompt. Can anyone help me with this?
Thanks
Asked
Active
Viewed 158 times
-1

Parham Lashkar
- 1
- 4
-
Either begin the batch file with ```@CD /D "%~dp0"```, or ```@PushD "%~dp0"``` – Compo Jul 06 '22 at 23:16
2 Answers
0
fixed
Just copy this code in your .cmd file:
cd /d %~dp0
call cmd

Parham Lashkar
- 1
- 4
-
`cd /d %~dp0` does not work on batch file is stored, for example, in the directory `C:\Temp\Development & Test(!)_100%`. Yes, that is an awful directory name, but a valid directory name. I have seen every possible character in user account names and so in often used directory paths. The first command line does also not work on file being stored on a network resource and executed by using its UNC path, for example `\\Server\Share\Batch Folder\BatchFile.cmd` because of `cmd.exe` denies by default setting a UNC path as current directory. – Mofi Jul 07 '22 at 09:55
-
Please run in a command prompt window also `call /?` and read the output usage help which explains `%~dp0` (drive and path of argument 0 which is the full path of the directory containing the currently processed batch file) and explains in short description also that the command `call` is for calling a batch file from within a batch file or run command lines below a label like a subroutine in a new batch file context. It is not needed for running an executable like `cmd.exe` specified in your second command line just with its file name `cmd` instead of its fully qualified file name `%ComSpec%` – Mofi Jul 07 '22 at 09:58
-
Hint: Use a www search engine with searching for `Windows Explorer open command prompt here` and look on the found pages. You will find out that your batch file is not needed at all for the purpose you most likely want to achieve, open a command prompt window with current directory being the folder currently selected in Windows File Explorer. – Mofi Jul 07 '22 at 10:01
0
What's mean 'dir opens in command prompt'? If you want to open new cmd window from current dir, just add to your file the following command:
start cmd
Or even
start cmd @cmd /k dir
if you want to see a content of the current directory in that window. Also you can open the current folder in Windows Explorer - just add to your file this command:
explorer .

liketaurus
- 152
- 5
-
Thanks. It works but it opens two cmd tabs. I think it's better to use "cd /d %~dp0" – Parham Lashkar Jul 06 '22 at 16:52