How to Delete Files and Folders in Windows Using a Batch File

Table of contents:

How to Delete Files and Folders in Windows Using a Batch File
How to Delete Files and Folders in Windows Using a Batch File
Anonim

Would you like to be able to delete all files in a specific directory with a single mouse click? Are you a programmer looking for a quick and easy way to get your program to delete a file at a certain point in execution? Well, this article is just for you. Read on to find out how.

Initial Steps

  1. Open the "Run" window by pressing the key combination "Windows + R", type the command "explorer" in the "Open" field and press the "Enter" key. A new "File Explorer" window will open.
  2. Press the "Alt" key on your keyboard while the "File Explorer" window is active.
  3. Access the "Tools" menu and choose the "Folder Options" item.
  4. Go to the "View" tab of the "Folder Options" window.
  5. Uncheck the "Hide extensions for known file types" checkbox (but only if it is checked).
  6. Press the "OK" button.

    Steps

    Delete a File in Microsoft Windows Using Batch Files Step 1
    Delete a File in Microsoft Windows Using Batch Files Step 1

    Step 1. Start the "Notepad" program

    Access the "Start" menu, type in the keyword "Notepad" and choose its icon from the list of results that appears. If you are using an older version of Windows, go to the "Start" menu, select the "All Programs" item, click the "Accessories" icon and finally select the "Notepad" option.

    Delete a File in Microsoft Windows Using Batch Files Step 2
    Delete a File in Microsoft Windows Using Batch Files Step 2

    Step 2. Type the command "cd" in the first line of the "Notepad" program window (without including the quotes)

    Delete a File in Microsoft Windows Using Batch Files Step 3
    Delete a File in Microsoft Windows Using Batch Files Step 3

    Step 3. Locate the file or folder you want to delete, then right click on it and choose the "Properties" option from the context menu that will appear

    Delete a File in Microsoft Windows Using Batch Files Step 4
    Delete a File in Microsoft Windows Using Batch Files Step 4

    Step 4. Copy the text string visible in the "Path" field of the "Properties" window

    Delete a File in Microsoft Windows Using Batch Files Step 5
    Delete a File in Microsoft Windows Using Batch Files Step 5

    Step 5. Return to the "Notepad" program window, press the space bar once to insert a blank space after the "cd" command, paste the path you just copied and finally enclose it in quotes

    For example, if the path in question is C: / users / Luca, in the document it should appear as follows "C: / users / Luca".

    If you need to delete the files inside a specific folder that is currently empty, in step number 3 of the article take note of the complete path of the directory in question followed by the special character / and the name of the latter

    Delete a File in Microsoft Windows Using Batch Files Step 6
    Delete a File in Microsoft Windows Using Batch Files Step 6

    Step 6. Now press the "Enter" key on your keyboard to create a new line of text

    Delete a File in Microsoft Windows Using Batch Files Step 7
    Delete a File in Microsoft Windows Using Batch Files Step 7

    Step 7. Type the "del" command into the "Notepad" program (without quotes) using the newly created line of text

    Delete a File in Microsoft Windows Using Batch Files Step 8
    Delete a File in Microsoft Windows Using Batch Files Step 8

    Step 8. Add a blank space and type the name of the folder or file (in this case also include the extension) you want to delete

    Remember to enclose it in quotation marks. For example to delete the directory named "test", you will have to type the following text of the "test". If you want to delete a file called "musica.wav" instead, you will have to use this string of "music.wav".

    Delete a File in Microsoft Windows Using Batch Files Step 9
    Delete a File in Microsoft Windows Using Batch Files Step 9

    Step 9. Go to the "File" menu of the "Notepad" program and choose the "Save As" option

    Delete a File in Microsoft Windows Using Batch Files Step 10
    Delete a File in Microsoft Windows Using Batch Files Step 10

    Step 10. Now access the "Save as" drop-down menu and choose the "All files (*

    *)".

    Delete a File in Microsoft Windows Using Batch Files Step 11
    Delete a File in Microsoft Windows Using Batch Files Step 11

    Step 11. Name the file using the format "[filename].bat" (without quotes)

    Replace the [filename] parameter with the name of your choice.

    Delete a File in Microsoft Windows Using Batch Files Step 12
    Delete a File in Microsoft Windows Using Batch Files Step 12

    Step 12. Press the "Save" button

    Delete a File in Microsoft Windows Using Batch Files Step 13
    Delete a File in Microsoft Windows Using Batch Files Step 13

    Step 13. Locate the file you just created and double-click it to section

    If you have followed all the steps described so far correctly, the specified folder or file will have been successfully deleted.

    If a notification message appears prompting you to confirm that you want to delete the item under consideration, simply press the "Y" key on your keyboard followed by the "Enter" key

    Advice

    • Using this method you can also use metacharacters. For example, if you need to delete all the contents of a folder, you can replace the file name and extension with the "*" symbol. If you want to delete all files with the ".txt" extension in a directory instead of typing the individual names of the elements to be removed, you can use the "*.txt" parameter.
    • To delete multiple files at the same time repeat the steps described in the article using the same text document.

Recommended: