Cmd location in windows

Cmd location in windows

This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions.

Answered by:

Question

I use the command prompt very often. In order to actually do anything there, I have to run the cmd.exe program as administrator. Each time I open cmd.exe as administrator, it places me in C:\Windows\System32. Then, I have to enter a cd command into my working directory, which is NEVER C:\Windows\System32. Is there a way to change the default location cmd.exe uses as administrator?

Answers

You can try the following:

Change the properties of C:\Windows\System32 to enforce the option «Run this program as an administrator», under Compatibility tab. You change the settings for all users as well under this tab.

Once done, anywhere in Explorer (like C:\temp), press Shift key and right click in empty space. You can see the regular pop-up menu with option «Open command window here». Click on the option to open cmd in administrator mode and in the selected location.

  • Proposed as answer by MeipoXu Microsoft contingent staff Tuesday, December 22, 2015 8:52 AM
  • Marked as answer by Kate Li Microsoft employee Tuesday, December 29, 2015 3:18 AM

I would make an apology. The «quick access» option is not available on Windows 7 but the «shortcut» suggestion should work.

How can I return to the previous directory in windows command prompt?

I often want to return to the previous directory I was just in in cmd.exe, but windows does not have the «cd -» functionality of Unix. Also typing cd ../../.. is a lot of typing.

Is there a faster way to go up several directory levels?

And ideally return back afterwards?

4 Answers 4

On Windows CMD, I got used to using pushd and popd . Before changing directory I use pushd . to put the current directory on the stack, and then I use cd to move elsewhere. You can run pushd as often as you like, each time the specified directory goes on the stack. You can then CD to whatever directory, or directories , that you want. It does not matter how many times you run CD. When ready to return , I use popd to return to whatever directory is on top of the stack. This is suitable for simple use cases and is handy, as long as you remember to push a directory on the stack before using CD.

Читайте также:  Образ windows 10 pro x64 с драйверами

Run cmd.exe using the /k switch and a starting batch file that invokes doskey to use an enhanced versions of the cd command.

Here is a simple batch file to change directories to the first parameter (%1) passed in, and to remember the initial directory by calling pushd %1.

md_autoruns.cmd:

We will also need a small helper batch file to remember the directory changes and to ignore changes to the same directory:

mycd.bat:

And a small aliases file showing what to do to make it all work:

aliases:

Now you can go up a directory level by typing ..

Add another . for each level you want to go up.

When you want to go back, type cd — and you will be back where you started.

Aliases to jump to directories like wk or tools (shown above) swiftly take you from location to location, are easy to create, and can really help if you work in the command line frequently.

Location of Command Prompt Commands

Where are the commands located for Command Prompt? I know this is possible in Linux, but what about windows?

I want to rename certain commands so I can make my own using the original name.

Im doing this on a 32bit Windows XP if that helps.

More specifically, where can I find the tree command?

3 Answers 3

in windows 7 there’s the where command.

You can overwrite the standard commands, but this will break a lot of stuff. You would also have to modify your path environment variable

in windows 7 tree is located in C:\Windows\System32\tree.com

This might be a useful site for you http://ss64.com/nt/

Use a bat file of the same name to override an inbuilt external command. Some commands are internal so you have to specify .bat. Also if calling the overridden program specify the extension.

There are two types of commands in Windows command prompt. The internal commands are built-in inside the command processor program, that is called cmd.exe . You may inspect such commands if you open cmd.exe file with an Hex editor, or even directly with Notepad (although doing that requires a lot of patience!); to know the location of cmd.exe file, type: set comspec . Examples of internal commands are DIR, COPY, TYPE, MD, etc. (the simplest ones) and all Batch-file commands, like GOTO, IF, FOR, REM, etc. There is no way to change the name of an internal command, unless you modify the cmd.exe file (for example, using the same Hex editor) that, although possible, is NEVER recommended.

Читайте также:  Linux вычисление числа пи

The external commands reside in disk files with .com or .exe extension that are grouped in certain subdirectories, usually in C:\Windows\System or C:\Windows\System32; these subdirectories must appear in %PATH% variable in order to execute the external commands directly in the command prompt. You may locate the folder where an executable file resides using the following command, that review folders included in PATH variable only (you can not use wild-cards in this case):

How do I find the location of an executable in Windows?

I remembered that I used a tool called as where to find locations for any executable programs like this in a console:

Now I cannot find this tool. Not sure if Windows has a build-in tool to do that search?

14 Answers 14

According to the StackOverflow answer at Is there an equivalent of ‘which’ on windows?, where.exe does this on Windows 7 and Windows Server 2003 and later:

Example

Output:

In powershell use where.exe , Get-Command (or its abbreviation gcm ), as where is the default alias for Where-Object .

EDIT: I should have added, if you can’t use the WHERE command from the command prompt, check your PATH variable. (Just use the «path» command.) Make sure C:\Windows\System32 is in your path. That’s where «where.exe» is located.

WHERE is the command you’re looking for! WHERE is like a cross between the UNIX shell built-in «which» and the «locate» command, in that it works for both command executables and regular files.

It’s also somewhat more complex than either of those two, although, in general a simple

It’s different from the «locate» command in that it’s not looking through the entire filesystem. Instead, the default behavior is to look for files in two locations:

  • The current directory.
  • All of the directories in the PATH variable.

So, any command that you can run directly from a command prompt without specifying the directory, will be found by the WHERE command. (Because any command like that is already in the PATH variable list.)

If you want to search only in the command path variable, you can use:

Читайте также:  Как разделять диск при установке windows

If, on the other hand, you want to find all copies of a file in a directory tree, you can use:

Finally, WHERE will find commands and any files with an extension from the PATHEXT variable without including the extension. All other files have to be specified either exactly or with wildcards.

Take for example the files «dxdiag.exe» and «dxdiagn.dll». Note the following command and its output:

It succeeds in returning all versions of «dxdiag.exe» because «.exe» is one of the extensions in the PATHEXT variable. (Note: «WHERE dxdiag» would have worked as well, because C:\Windows\System32 is in the PATH variable.)

on the other hand, fails to return any result, because «.dll» is not in PATHEXT.

In this case, look at the result that adding a wildcard gives us:

It successfully returns all versions of dxdiagn.dll.

For more information, use «WHERE /?». Hope this helps!

find the location of the cmd.exe file

I have built a c++ program that depends on «cmd.exe» to execute some of the tasks. For now I and for test purposes the path to that file is «c:\windows\system32\cmd.exe». My question is there any c++ API that returns the path of that file knowing that my program must work on win32 and on win64.

2 Answers 2

The interpreter path is available in the %COMSPEC% environment variable which can be read with GetEnvironmentVariable() .

GetSystemDirectory is one option. For a 32-bit app, it will return the 32-bit system directory. For an x64 app, it will return the 64-bit native system directory.

You can also use CreateProcess or ShellExecuteEx with cmd.exe and it should find it without the path unless you are specifically concerned about someone manipulating the search path and getting the wrong cmd.exe .

If you are launching a .cmd file, then you can just do it with ShellExecuteEx with the open verb. Generally for a Windows desktop app, use of ShellExecuteEx is recommended for launching other programs. For example, here’s some code that will launch the command prompt running the test.cmd script and wait for the result:

You can also use:

The primary reason to use ShellExecuteEx instead of CreateProcess is that ShellExecuteEx can handle admin elevation requests for exes with the proper manifest elements. CreateProcess would fail if the target EXE requires higher privileges than your current process.

Оцените статью