Category Archives: Windows Command Prompt

Basic file commands in Windows Command prompt

Here is the list of basic file commands in Windows.

  1. cd (chdir)
  2. dir
  3. mkdir (md), rmdir (rd)
  4. copy con
  5. copy, xcopy
  6. move (ren or rename)
  7. erase and del
  8. tree

Changing directory

You can use “cd” command to change the directory.
cd /d <path>

To go to parent directory, you can use below command.

cd ..

To display current directory, you can use below command.

echo %cd%

If the name of directory contains spaces, you should enclose the name with double quotes.

dir command in Windows command prompt

You can use “dir” command to list of all files and directories.
dir

To display the files and directories recursively, you can use below command.

dir /s

To display the files and directories in ascending order, you can use below command.

dir /ON

To display the files and directories in descending order, you can use below command. Notice that only difference between above command and this command is that we have used “-N” instead of “N”

dir /O-N

To display only directories, you can use below command.

dir /AD

To display only files, you can use below command.

dir /A-D

Creating and deleting directory

You can use “mkdir” or “md” command to create new directory.
mkdir <directory-name>

To delete directory, you can use “rmdir”  or “rd” command. Note that below command will delete only empty directories.

rmdir <directory-name>

To delete directory containing files, you can use below option.

rmdir <directory-name> /S

move, rename (ren) commands

move command is used to move a file or files to another location. Here is the syntax.

move <source> <destination>

You can also use move command to rename a file. Alternatively you can use below commands to rename file.

rename <old-file-name> <new-file-name>

ren <old-file-name> <new-file-name>

attrib command

You can use “attrib” command to change the attributes of files or directories.

For example – To hide the directory or file, you can use below command.

attrib +h <directory-or-file>

To un-hide the directory or file, you can use below command.

attrib -h <directory-or-file>

copy con command

“copy con” command is used create new files.

here is the syntax of command.

> copy con <file-name>

After you hit enter, you can enter data in the file and to save file, you have to use ctrl+c hotkey.

 

assoc command

assoc command is used to display file extensions and associated file types.

assoc-command-in-windows
assoc-command-in-windows

We can also associate extensions with file types using below command.
Assoc .html=Htmlfile
Assoc .txt=txtfile
Assoc .exe=exefile

To delete the file extension association, you can use below command. After executing this command, system will not know how to open the .txt file.

Assoc .txt=

Copying a file using copy and xcopy

Syntax of copy command is given below.

copy <source> <destination>

You can use below command to copy file f1 to f2.
copy f1 f2

You can use below command to copy all files from directory d1 to d2.
copy d1\* d2

When you do not specify the destination directory, files are coped to current directory.
COPY “d:\software\*.text”

Syntax of xcopy command is same as copy command. Only difference is that xcopy is more powerful. We have lot of options in xcopy command like copying only files with specific attributes, copying hidden and system files etc.

 

Deleting a file

To delete a file “f1”, you can use below command.
del f1

We can also use “erase” command to delete files.

erase f1

To delete multiple files ending with .txt, you can use below command.
del “*.txt”

tree command in Windows

tree command displays files and directories in tree format. Here is the sample output of tree command.

tree-command
tree-command