Windows cmd directory name

Command Prompt: 11 basic commands you should know (cd, dir, mkdir, etc.)

Geeks and experts love the Command Prompt because of the advanced commands it can run. Fortunately, Command Prompt is not built only on advanced commands, but also on simple ones, designed to perform basic operations. In this article, we show you how to execute commands such as changing the current directory, switching to another drive, viewing the contents of a directory, creating and renaming folders, copying, deleting files and folders, and launching applications from the Command Prompt. We are also going to show you how to get help when using this app for Windows. Let’s get started:

NOTE: The information shared in this tutorial applies to Windows 10, Windows 8.1, and Windows 7. Note that, for simplicity, we are using screenshots taken only in Windows 10.

1. How to change the directory (folder) in Command Prompt (CMD)

The first command from the list is CD (Change Directory). This command enables you to change the current directory or, in other words, to navigate to another folder from your PC. For instance, the command CD takes you to the top of the directory tree. To see how it works, after you open the Command Prompt, type cd and press Enter on your keyboard. You should see how the CD command takes you to the top of the directory tree. In this case, to the “C:” drive.

Note that the Command Prompt is not case sensitive, meaning that you can type commands using capital letters, lowercase or any combination of them. The commands CD, cd or Cd, all work the same way.

Going back to the “CD” command, now you are working on the root of the “C:” drive. If you need to go to a specific folder from this drive run the command “CD Folder.” The subfolders must be separated by a backslash character: “.” For instance, when you need to access the System32 folder located in “C:\Windows,” type “cd windows\system32” as shown below, and then press Enter on your keyboard.

When you need to go one folder up, use the “cd..” command. Let’s assume that you want to go back to the Windows folder. Type “cd..” and press Enter on your keyboard.

The effect is that your current directory changes to “C:\Windows.”

2. How to change the drive in Command Prompt (CMD)

To access another drive, type the drive’s letter, followed by “:”. For instance, if you wanted to change the drive from “C:” to “D:”, you should type “d:” and then press Enter on your keyboard.

To change the drive and the directory at the same time, use the cd command, followed by the “/d” switch. The “/d” parameter is used to change the current drive to a specific folder from another disk volume.

For instance, if you are now on the “D:” drive and you want to go back to the Windows folder from the”C:” drive, you should type “cd /d C:\Windows” and press Enter on your keyboard, like in the following screenshot.

NOTE: By typing only the drive letter you automatically move to your most recent location on that drive. For instance, if you are on “D:” drive and type “cd c:\windows” nothing seems to happen. However, if you type “c:” then the working folder changes to “c:\windows,” assuming that it was the last folder you worked with on your “C:” drive.

Читайте также:  Работа с github для windows

3. How to view the contents of a directory in Command Prompt (CMD)

You can view the contents of a folder by using a command called DIR. To test it, we have created a folder named Digital_Citizen on the D: drive, with several files and subfolders. You can see them in the screenshot below.

The last time, our working folder was “C:\Windows.” To navigate to the folder mentioned above, we have to use the command “cd /d D:\Digital_Citizen.” To view the contents of the folder, type DIR, and press Enter. The list of the files and folders contained by it is displayed, together with some details about each of them (the size and the date and time when they were last modified).

4. How to create a new directory with Command Prompt (CMD)

You can make a new folder using the MKDIR (Make Directory) or the MD command. The syntax of these commands is “MKDIR Folder” or “MD Folder.”

Let’s say we need to create a new folder called Digital_Citizen_Life that is going to be placed in the “D:\Digital_Citizen” folder. To do that, we need to type “mkdir Digital_Citizen_Life” and then press Enter, as shown below.

To test if it worked, use the DIR command again. The newly created folder appears in the list.

NOTE: Do not forget that all these commands depend on the current location in the Command Prompt. For instance, if you are on the “C:” drive and type “MKDIR test,” the new folder is created in the root of the “C:” drive.

Another way to create a folder that does not involve being in the desired folder is to type the complete path of the new folder. For example, if you are working on the “D:” drive and you want to create a new folder in “C:,” called other_stuff, type “mkdir c:\other_stuff” and then press Enter.

When you need to create a folder with subfolders at the same time, you can use the “MKDIR FolderSubfolder” command. For instance, if we type “mkdir Digital_Citizen_Tests\Beta\Test1” three folders are created: Digital_Citizen_Tests, Beta and Test1, in a tree-like structure.

5. How to rename files and folders with Command Prompt (CMD)

To rename files and folders, you need to use the REN (Rename) command. To rename folders, type “ren Folder NewFolderName.” For example, if we wanted to rename the Digital_Citizen_Tests folder to Digital_Citizen_Final_Tests, we should run “ren Digital_Citizen_Tests Digital_Citizen_Final_Tests” and press Enter.

To rename a file, use the same command, like this: “ren filename.extension newname.extension”. For instance, to rename the Digital_Citizen_Picture1.bmp file to Image0.bmp, we have to run the command “ren Digital_Citizen_Image1.bmp Image0.bmp” command.

Read the second page of this tutorial if you want to learn how to copy files and folders, delete files and folders, start an application, and get help when using the Command Prompt.

Get current folder name by a DOS command?

Is it possible to get the current folder name (not current directory path) by using a DOS command? If so, how?

The closest I got was this but it doesn’t do it:

note: the above attempt was me attempting to tokenize the string and get the last token set as the CURR variable.

n* are supported in MS-DOS. (Windows’ cmd.exe is not DOS, it’s a native Windows program.) – user1686 Jul 7 ’10 at 12:05

10 Answers 10

Shortest way I have found:

or within a .bat script:

or in .bat with Get value in variable.

nx means file name and extension only

If you want to know the current location of the batch file (and if your Windows isn’t a very ancient release), type for /? in a ‘DOS box’ window. Scroll down. Read.

Читайте также:  Reinstall net framework windows 10

You’ll find out, that you can now read (from within the batch file) these variables:

This works for many cases. Assume, the batchfile is called mytest.bat . You may call it in different ways:

  1. ..\..\to\mytest.bat . (relative path)
  2. d:\path\to\mytest.bat . (full path)
  3. \\fileserver\sharename\mytest.bat . (path on remote share)

. and you’ll always get the right value in your variables.

dp0″ solves my problem also and its more elegant. – djangofan Aug 10 ’10 at 21:32

dpnx0 , isn’t? – luc Nov 4 ’16 at 8:49

I personally liked Toms answer, until it struggled with dots in dir names. This gave me a hint:

Tom’s answer is good, but if you have a directory name with a period in it (i.e. wxwidgets-2.9.4) you’ll only get the full name. So this would output wxwidgets-2.9 instead because the .4 has been treated as an extension (Yes, even though it’s a directory name!).

To get the full output name you have to add on the extension to the end:

and in batch file mode:

Or of course, set a variable in the batch file instead:

An other way is:

it works with «.» and spaces in pathname

What does it do?

put the whole filename (driveletter-path-filename-extension) into MyPath Var

remove filename and extension from MyPath var

It also works with UNC Paths. If you need the Backslash on the end of the Path. Remove the \ after MyPath in the second set command, eg.

dp0 – Amit Naidu Jun 16 ’13 at 11:12

You can get the current dir into a variable. One-liner:

Be careful of the backslash of the end of path, it has not to be backslash of the end.

My answer in this thread does it in 3 simple lines:

This works for me from a batch file. It returns the current working directory name.

Almost all of the above code options will lead to an error if you call the bat file from the outside by other script or program. For example, this code: for %%I in (.) do set CurrDirName=%%

nxI, when called from Notepad++, will output Notepad++, but I don’t need a parent Notepad++ folder name surely! It is a bad way. In addition, for the same reason, do not use %cd% and any other options. Use only the input parameters of this, maybe remotely located script. Otherwise, you will get the result for the calling, but not called script and what is written in its command line, but not what must be written in the command line of the called code. Use this code for avoid errors:

Windows shell command to get the full path to the current directory?

Is there a Windows command line command that I can use to get the full path to the current working directory?

Also, how can I store this path inside a variable used in a batch file?

14 Answers 14

Use cd with no arguments if you’re using the shell directly, or %cd% if you want to use it in a batch file (it behaves like an environment variable).

You can set a batch/environment variable as follows:

sample screenshot from a Windows 7 x64 cmd.exe.

Update: if you do a SET var = %cd% instead of SET var=%cd% , below is what happens. Thanks to jeb.

Quote the Windows help for the set command ( set /? ):

Note the %CD% — expands to the current directory string. part.

This has always worked for me:

For Windows we can use

command is there.

For Windows, cd by itself will show you the current working directory.

For UNIX and workalike systems, pwd will perform the same task. You can also use the $PWD shell variable under some shells. I am not sure if Windows supports getting the current working directory via a shell variable or not.

On Windows:

CHDIR Displays the name of or changes the current directory.

Читайте также:  Scanmaster lite для windows

In Linux:

PWD Displays the name of current directory.

Based on the follow up question (store the data in a variable) in the comments to the chdir post I’m betting he wants to store the current path to restore it after changeing directories.

The original user should look at «pushd», which changes directory and pushes the current one onto a stack that can be restored with a «popd». On any modern Windows cmd shell that is the way to go when making batch files.

If you really need to grab the current path then modern cmd shells also have a %CD% variable that you can easily stuff away in another variable for reference.

What is the current directory in a batch file?

I want to create a few batch files to automate a program.

My question is when I create the batch file, what is the current directory? Is it the directory where the file is located or is it the same directory that appears in the command prompt, or something else?

9 Answers 9

From within your batch file:

dp0 refers to the full path to the batch file’s directory (static)
%

f0 both refer to the full path to the batch directory and file name (static).

dp0 will always give the full path to the executing batch file. – dbenham Jun 12 ’13 at 11:19

dp0 gives the full path to the directory that the executing batch file is in. %

dpnx0 (which is equivalent to %

f0) gives the full path to the batch file. See robvanderwoude.com/parameters.php for more details. – deadlydog Jul 11 ’13 at 20:08

dp0 is the working directory not the batch files directory, Found this out the hard way. – trampster Jan 29 ’18 at 22:44

dp0 gives the batch file directory with trailing slash. – icc97 Feb 27 ’18 at 9:10

It usually is the directory from which the batch file is started, but if you start the batch file from a shortcut, a different starting directory could be given. Also, when you’r in cmd, and your current directory is c:\dir3 , you can still start the batch file using c:\dir1\dir2\batch.bat in which case, the current directory will be c:\dir3 .

In a batch file, %cd% is the most commonly used command for the current directory, although you can set your own variable:

So say you were wanting to open Myprog.exe. If it was in the same folder, you would use the command:

That would open Myprog from the current folder.

The other option is to make a directory in C: called AutomatePrograms. Then, you transfer your files to that folder then you can open them using the following command:

dp0 is more consistent. – icc97 Feb 27 ’18 at 9:14

Say you were opening a file in your current directory. The command would be:

I hope I answered your question.

It is the directory from where you run the command to execute your batch file.

As mentioned in the above answers you can add the below command to your script to verify:

It is the directory from where you start the batch file. E.g. if your batch is in c:\dir1\dir2 and you do cd c:\dir3 , then run the batch, the current directory will be c:\dir3 .

Just my 2 cents. The following command fails if called from batch file (Windows 7) placed on pendrive:

But this does the job:

dp0 – Ammar Mohammad Aug 3 ’19 at 12:49

%__CD__% , %CD% , %=C:%

There’s also another dynamic variable %__CD__% which points to the current directory but alike %CD% it has a backslash at the end. This can be useful if you want to append files to the current directory.

With %=C:% %=D:% you can access the last accessed directory for the corresponding drive. If the variable is not defined you haven’t accessed the drive on the current cmd session.

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