How to Create and Delete Files and Directories Using the Windows Command Prompt

Table of contents:

How to Create and Delete Files and Directories Using the Windows Command Prompt
How to Create and Delete Files and Directories Using the Windows Command Prompt
Anonim

This article explains how to use the Windows "Command Prompt" to create and delete files and folders (also called directories). Learning to manage and organize your data using the "Command Prompt" can be very useful when programming. Any operations you perform with the "Command Prompt" on files and folders will impact other areas of Windows. This means that creating a file or folder from the "Command Prompt" will make it accessible and manageable through Windows apps as well.

Steps

Part 1 of 4: Creating a File

Create and Delete Files and Directories from Windows Command Prompt Step 10
Create and Delete Files and Directories from Windows Command Prompt Step 10

Step 1. Open a "Command Prompt" window

The easiest way to do this is to press the key combination Win + S to open the Windows search bar, type the keyword cmd and click on the app icon Command Prompt when it appears in the results list.

Create and Delete Files and Directories from Windows Command Prompt Step 11
Create and Delete Files and Directories from Windows Command Prompt Step 11

Step 2. Navigate to the folder where you want to create a new file

When you open the "Command Prompt" window the default working folder will be "C: / Users / Username". To access the folder where you want to create the file type the command cd path_directory and press the key Enter. Replace the directory_path parameter with the real path of the folder in question.

  • For example, if you want to create a file directly on your computer desktop, you will need to execute the following cd desktop command and press the key Enter.
  • If the folder you are looking for is not present in your working folder (for example C: / Users / Username), you will need to specify the entire path in the command (for example C: / Users / Username_Diverse / Desktop / File).
Create and Delete Files and Directories from Windows Command Prompt Step 12
Create and Delete Files and Directories from Windows Command Prompt Step 12

Step 3. Create an empty file of any type

If you want to create a file with data inside, skip to the next step. To create an empty file follow these instructions:

  • Type the command type nul> filename.txt;
  • Replace the filename.txt parameter with the name you want to assign to the file you are creating. The ".txt" extension indicates that you are creating a simple text file. If you wish, you can use a different extension, for example ".docx" (Word document), ".png" (image file), ".xlsx" (Excel file) and ".rtf" (text document in "rich text" format);
  • Press the button Enter.
Create and Delete Files and Directories from Windows Command Prompt Step 13
Create and Delete Files and Directories from Windows Command Prompt Step 13

Step 4. Create a text file containing data

If you don't need to create a text document, skip to the next step. Use these steps to create a text file in which you can store data:

  • Type the command copy with text_file.txt replacing the text_file parameter with the name you want to assign to the file;
  • Press the button Enter;
  • Type the content of the document. This is a rudimentary text editor, but it is ideal for creating notes or code quickly and easily. To create a new line of text, press the key Enter;
  • Press the key combination Ctrl + Z when you are done creating the document. This way the text you created will be saved in the file;
  • Alternatively, you can run the command echo insert the text to be stored in the file> filename.txt.
Create and Delete Files and Directories from Windows Command Prompt Step 14
Create and Delete Files and Directories from Windows Command Prompt Step 14

Step 5. Create a file that has a specific disk size

If you don't have this need, skip this step. To create an empty file that has a size equal to a certain number of bytes use the following command:

  • fsutil file createnew filename.txt 1000.
  • Replace the filename parameter with the name you want to assign to the file and the value 1000 with the number of bytes the file should have.

Part 2 of 4: Deleting a File

Create and Delete Files and Directories from Windows Command Prompt Step 15
Create and Delete Files and Directories from Windows Command Prompt Step 15

Step 1. Open a "Command Prompt" window

The easiest way to do this is to press the key combination Win + S to open the Windows search bar, type the keyword cmd and click on the app icon Command Prompt when it appears in the results list.

Create and Delete Files and Directories from Windows Command Prompt Step 16
Create and Delete Files and Directories from Windows Command Prompt Step 16

Step 2. Navigate to the directory that contains the file you want to delete

By default, the "Command Prompt" working folder is "C: / Users / Username". If the file is stored in another folder, type the command cd directory_path and press the key Enter. In this case, replace the directory_path parameter with the full path of the folder in question.

  • For example, if you want to delete a file stored on your computer desktop, you will have to type the cd desktop command and press the key Enter.
  • If the folder you are looking for is not present in your working folder (for example C: / Users / Username), you will need to specify the entire path in the command (for example C: / Users / Username_Diverse / Desktop / File).
Create and Delete Files and Directories from Windows Command Prompt Step 17
Create and Delete Files and Directories from Windows Command Prompt Step 17

Step 3. Type the command dir and press the Enter key

A list of all files in the current working folder will be displayed. The file you want to delete should also be in the list.

By using the "Command Prompt" to delete a file, it will be permanently deleted from the system without first being moved to the recycle bin. For this reason try to be attentive and focused when using the "Command Prompt"

Create and Delete Files and Directories from Windows Command Prompt Step 18
Create and Delete Files and Directories from Windows Command Prompt Step 18

Step 4. Type the filename command and press the Enter key

Replace the filename parameter with the name of the file you want to delete complete with extension (for example *.txt, *.jpg). This will delete the file from your computer.

  • For example, if you need to delete a text file named "hello", you will need to run the following hello.txt command within the "Command Prompt".
  • If the filename contains blanks (for example "hi there"), you will need to enclose it in quotes like "hi there".
  • If you get an error message stating that the file cannot be deleted, try running the following command on / f filename. This command is used to force the deletion of files in "read only" mode.

Part 3 of 4: Create a Folder

Create and Delete Files and Directories from Windows Command Prompt Step 1
Create and Delete Files and Directories from Windows Command Prompt Step 1

Step 1. Open a "Command Prompt" window

The easiest way to do this is to press the key combination Win + S to open the Windows search bar, type the keyword cmd and click on the app icon Command Prompt when it appears in the results list.

Create and Delete Files and Directories from Windows Command Prompt Step 2
Create and Delete Files and Directories from Windows Command Prompt Step 2

Step 2. Navigate to the folder where you want to create the new directory

By default the "Command Prompt" working folder is "C: / Users / Username". If you do not need to create the new folder within the default "Command Prompt" path, execute the cd command directory_path and press the key Enter. In this case, replace the directory_path parameter with the full path to the destination folder.

  • For example, if you want to create a new folder inside your computer desktop, you will have to type the cd desktop command and press the key Enter to set the desktop as the "Command Prompt" working directory.
  • If the directory you need to access is not present in your user folder (for example "C: / Users / Username"), you will need to specify its full path (for example C: / Users / Other_Path / Desktop / File).
Create and Delete Files and Directories from Windows Command Prompt Step 3
Create and Delete Files and Directories from Windows Command Prompt Step 3

Step 3. Type the mkdir command Directory_Name into the "Command Prompt"

Replace the Directory_Name parameter with the name of the folder you want to create.

For example, if you want to create the directory named "Tasks", you will need to run the following command mkdir Tasks

Create and Delete Files and Directories from Windows Command Prompt Step 4
Create and Delete Files and Directories from Windows Command Prompt Step 4

Step 4. Press the Enter key

This will run the command that will create the new folder with the specified name.

Part 4 of 4: Deleting a Folder

Create and Delete Files and Directories from Windows Command Prompt Step 5
Create and Delete Files and Directories from Windows Command Prompt Step 5

Step 1. Open a "Command Prompt" window

The easiest way to do this is to press the key combination Win + S to open the Windows search bar, type the keyword cmd and click on the app icon Command Prompt when it appears in the results list.

Create and Delete Files and Directories from Windows Command Prompt Step 6
Create and Delete Files and Directories from Windows Command Prompt Step 6

Step 2. Navigate to the folder containing the directory you want to delete

By default the "Command Prompt" working folder is "C: / Users / Username". If the folder in question is located elsewhere on the disk, you will need to execute the following command cd directory_path and press the key Enter. In this case, replace the directory_path parameter with the full path to the destination folder.

  • For example, if the folder you want to delete is stored on the PC desktop, you will need to execute the cd desktop command.
  • If the directory you need to access is not in your user folder (for example "C: / Users / Username"), you will need to specify its full path (for example C: / Users / Other_Path / Desktop / File).
Create and Delete Files and Directories from Windows Command Prompt Step 7
Create and Delete Files and Directories from Windows Command Prompt Step 7

Step 3. Type the command rmdir / s Directory_Name

Replace the Directory_Name parameter with the name of the folder to delete.

  • For example, if you need to delete the "Tasks" folder, you will need to run the command rmdir / s Tasks.
  • If the folder name contains blank spaces (for example "Assigned Tasks"), you will need to enclose it in quotes like this: rmdir / s "Assigned Tasks".
Create and Delete Files and Directories from Windows Command Prompt Step 8
Create and Delete Files and Directories from Windows Command Prompt Step 8

Step 4. Press the Enter key to execute the command

  • If you are trying to delete a folder that contains hidden files or directories, you will get the following error message "The directory is not empty". In this case you will need to change the access attributes of these items by transforming them from hidden or system files or folders into normal items. Follow these instructions:

    • Use the cd command to access the folder you want to delete;
    • Execute the command dir / a to display the list of all the files in the directory together with their access attributes;
    • If deleting all the files in the folder does not cause any problems, run the attrib -hs * command. In this way, all special access attributes will be removed from all files, effectively allowing them to be deleted.
    • Type the command cd.. and press the Enter key 'to return to the previous folder;
    • Now run the rmdir / s command to delete the directory in question.
    Create and Delete Files and Directories from Windows Command Prompt Step 9
    Create and Delete Files and Directories from Windows Command Prompt Step 9

    Step 5. Press the y button to confirm your action

    The folder in question will be permanently deleted.

Recommended: