How to delete a file using a batch file (.bat)

Table of contents:

How to delete a file using a batch file (.bat)
How to delete a file using a batch file (.bat)
Anonim

Batch files are batch processing programs that consist of a series of instructions that can be understood by the command interpreter of an operating system. These scripts are useful when it is necessary to perform repetitive operations on a system, such as daily or weekly erasing of certain text files that accumulate in a directory.

Steps

Part 1 of 5: get the information about the file to be deleted

Delete a File in Microsoft Windows Using Batch Files Step 1

Step 1. Find the file you want to erase

Navigate to the location of the document you want to delete with a batch file.

Delete a File in Microsoft Windows Using Batch Files Step 2

Step 2. Right click on the file to be deleted

This will display a pop-up drop-down menu.

Delete a File in Microsoft Windows Using Batch Files Step 3

Step 3. Click the option labeled Properties

You'll find it at the bottom of the drop-down menu. You will see a menu displayed.

Delete a File in Microsoft Windows Using Batch Files Step 4

Step 4. Determine the file extension

Look in the section called File type. You may see a three- or four-letter word displayed in parentheses. This is the extension that you will need to add to the filename later.

  • If this is a text file, you will see displayed Text document (.txt) to the right of the title File type.
  • you see Folder as the file type displayed, you will have to close the properties window, click on the document you want to check, right-click on it again and click on Properties.
Delete a File in Microsoft Windows Using Batch Files Step 5

Step 5. Memorize the path to the file

Press the left mouse button and browse for the address displayed in the properties display section. Press Ctrl + C on your keyboard. This will save the path to your file in your operating system notepad. At this point, you are ready to begin writing your batch file.

Part 2 of 5: Write the batch file

Delete a File in Microsoft Windows Using Batch Files Step 6

Step 1. Open the start menu

Windowsstart

Windows.

Click on the Windows logo icon that you will find in the lower left corner of your screen.

Delete a File in Microsoft Windows Using Batch Files Step 7

Step 2. Open Notepad

Enter notepad in the system search bar then click on Notepad at the top of the start menu window. The Notepad window will appear.

Delete a File in Microsoft Windows Using Batch Files Step 8

Step 3. Add the change directory command

Enter the cd instruction followed by a space, then paste the address of the file to be deleted by simultaneously pressing the Ctrl + V keys on your keyboard. Press the Enter key. This line will have the effect of indicating to the following erase command to search for the file to be deleted in the folder given as an argument to the instruction CD.

  • If your file is in your desktop folder, the change directory command will be:

    cd C: \ Users \ your_name \ Office.

  • If you do not take this step, the erase instruction will search for the file to be erased in one of the folders reserved for the operating system.
Delete a File in Microsoft Windows Using Batch Files Step 9

Step 4. Now add the delete instruction

Enter del on the line immediately below the one on which you typed the change directory command, then press the space bar on your keyboard.

Delete a File in Microsoft Windows Using Batch Files Step 10

Step 5. Enter the name and extension of the file to be deleted in quotes

Type a double quote character followed by the name of the file to be deleted exactly as it appears, including spaces, uppercase letters and special characters. Enter a period followed by the extension of your file. Now type the double quote character to close the entry of the name of your file.

  • If you want to erase a text document named Bananas, you will need to type "Bananas.txt" as the filename with its extension.
  • The second line of your batch file will be:

    del "filename.extension" where you will replace filename with the actual name of the document and.extension with its actual extension.

Delete a File in Microsoft Windows Using Batch Files Step 11

Step 6. Add more file names

If you want to delete several files from the same directory, press the Enter key on your keyboard to start a new line. Type del followed by a space, then the name of the second file and its extension. Repeat this process as needed and until you have listed line by line all the files you want to remove from the directory.

Part 3 of 5: Save and run the batch file

Delete a File in Microsoft Windows Using Batch Files Step 12

Step 1. Click the File button

You will find it in the upper left corner of the Notepad window. A drop-down menu will appear.

Delete a File in Microsoft Windows Using Batch Files Step 13

Step 2. Click Save As

This option, which you will find in the displayed drop-down menu, will present you with a save files dialog.

Delete a File in Microsoft Windows Using Batch Files Step 14

Step 3. Enter the name of the file with its batch type extension

Enter the name of the file in the input box titled File name then follow it with the.bat extension.

To name a Fruits batch file, you will need to enter fruit.bat in the text entry box

Delete a File in Microsoft Windows Using Batch Files Step 15

Step 4. Click the selection box titled File of type

It is located in the lower part of the window. This will display a new drop-down menu.

Delete a File in Microsoft Windows Using Batch Files Step 16

Step 5. Click All Files from the drop-down menu

Delete a File in Microsoft Windows Using Batch Files Step 17

Step 6. Choose a folder to save your file to

In the left part of the window, click on the name of a directory such as Desktop to select it as the location to save your batch file.

Delete a File in Microsoft Windows Using Batch Files Step 18

Step 7. Click Save

Your batch file will be saved to the location you chose when you click on this button, which you will find in the lower right corner of the window.

Delete a File in Microsoft Windows Using Batch Files Step 19

Step 8. Your batch file will permanently erase the targeted documents

When you run it, the del command in it will permanently delete the targeted document (s) without placing them in the system recycle bin. You will therefore no longer be able to recover them in the event of an error. This is why you should be very careful when adding the file (s) to delete to it.

Delete a File in Microsoft Windows Using Batch Files Step 20

Step 9. Run your batch file

When you are ready to delete the selected documents, just double-click on the icon representing your script to execute it. You should briefly see the command console listing the names of the documents as you delete them.

Part 4 of 5: Delete a file type from a folder

Delete a File in Microsoft Windows Using Batch Files Step 21

Step 1. Understand the purpose of this method

You can use a batch script if you want to delete all documents of a given type, such as text files, from a folder. This is particularly useful if your system accumulates various files of the same type in one folder over the course of its use.

Delete a File in Microsoft Windows Using Batch Files Step 22

Step 2. Open the folder whose files you want to erase

Double-click on the folder you want to open to clean it up.

Delete a File in Microsoft Windows Using Batch Files Step 23

Step 3. Determine the extension common to the files to be deleted

Proceed as follows:

  • right click on the type of files to be deleted;
  • click on Properties;
  • look for the desired extension in the section File type.
Delete a File in Microsoft Windows Using Batch Files Step 24

Step 4. Memorize the address of the folder

Select the address bar that you will see displayed at the top of the folder by right-clicking on it, then simultaneously press Ctrl + C on your keyboard to copy it to your clipboard.

  • If clicking on the address bar does not select its content, press and hold your left mouse button while browsing the path text to your folder, then right click and select it. 'option To copy in the displayed context menu.
Delete a File in Microsoft Windows Using Batch Files Step 25

Step 5. Open Notepad

Click on the icon

Windowsstart

boot, type notepad, then click Notepad when you see it appear at the top of the Windows menu.

Delete a File in Microsoft Windows Using Batch Files Step 26

Step 6. Enter the change directory instruction

Enter the command cd followed by a space, then simultaneously press the Ctrl + V keys on your keyboard to place the stored address of the folder after the instruction.

Delete a File in Microsoft Windows Using Batch Files Step 27

Step 7. Enter the erase command

Press the Enter key on your keyboard to add a new line after the change directory command. Enter the command del followed by a space.

Delete a File in Microsoft Windows Using Batch Files Step 28

Step 8. Give your script the type of file to delete

Instead of entering a full file name, you will need to enter, between double quotes, an asterisk followed by the extension corresponding to the type of files to be deleted. This will have the effect of forcing the erasure of all files whose extension matches the one you entered after the asterisk.

  • If you want to clean a directory of all its files with the extension *.txt, you will need to enter del "*.txt"
Delete a File in Microsoft Windows Using Batch Files Step 29

Step 9. You can add more file types if needed

For each document extension to be deleted, add a new line to your script by pressing the Enter key on your keyboard and copy the delete instruction described previously, taking care to replace the file extension with the one that should be apply.

Delete a File in Microsoft Windows Using Batch Files Step 30

Step 10. Save your script as a batch file

Proceed as follows:

  • click on File;
  • click on Save as…;
  • enter in the input box the name you want to give to your batch file followed by the.bat extension;
  • click on the drop-down menu titled File type and select the option All the files;
  • choose in the left column of the window a folder where to save your script then click on Save.
Delete a File in Microsoft Windows Using Batch Files Step 31

Step 11. Run your script when you need to clean up a folder

You just need to double-click on your batch file icon to delete files from the folder whose extensions match those you specified.

If you need to move the directory where the files to be deleted are located, be sure to modify your script to reflect the changes made

Part 5 of 5: Erase All Files from a Folder

Delete a File in Microsoft Windows Using Batch Files Step 32

Step 1. Understand how this method works

You can also use a batch file if you need to delete all the documents from a main directory and those contained in its folders without erasing these.

This method will not erase the folders in the target directory, but will delete all the files they contain

Delete a File in Microsoft Windows Using Batch Files Step 33

Step 2. Right click on the directory to clean

You will see a drop-down menu.

Delete a File in Microsoft Windows Using Batch Files Step 34

Step 3. Click the option labeled Properties

You'll find it at the bottom of the drop-down menu. This will open the folder and file properties window.

Delete a File in Microsoft Windows Using Batch Files Step 35

Step 4. Memorize the directory location

Press and hold the left mouse button while browsing the path text to your folder, then simultaneously press Ctrl + C on your keyboard to memorize its path.

Delete a File in Microsoft Windows Using Batch Files Step 36

Step 5. Open Notepad

Click on the icon

Windowsstart

boot, type notepad, then click Notepad when you see it appear at the top of the Windows menu.

Delete a File in Microsoft Windows Using Batch Files Step 37

Step 6. Enter the change directory instruction

Enter the cd instruction followed by a space, then simultaneously press the Ctrl + V keys on your keyboard to place the path to the folder you previously memorized after the instruction.

Delete a File in Microsoft Windows Using Batch Files Step 38

Step 7. Type the instruction to erase without confirmation of all files

Press the Enter key on your keyboard to add a line below the one containing the change directory instruction.

  • Enter the del erase statement followed by a space.
  • Enter the / S / Q switches that you will follow with a space. The switch / S will erase the files contained in the folders of the main directory. The switch / Q inhibits the display of a confirmation prompt after deleting each file.
  • Paste the address of the main folder that you memorized earlier.
  • Enter a backslash character \ (also called a backslash).
  • Enter the name of the main folder exactly as it appears on the address bar that appears when it is opened.
Delete a File in Microsoft Windows Using Batch Files Step 39

Step 8. Save your script as a batch file

Proceed as follows:

  • click on File;
  • click on Save as…;
  • enter in the input box the name you want to give to your script followed by the.bat extension;
  • click on the drop-down menu titled File type and select the option All the files;
  • choose in the left column of the window a folder where to save your script then click on Save.
Delete a File in Microsoft Windows Using Batch Files Step 40

Step 9. Run your file when you need to

Double-click on the icon representing your script when you need to clean up the main directory and the folders it contains.

Advice

  • If you want to edit a batch file, right click on its name and click the option Edit from the drop-down menu that appears. This will open the file in Notepad where you can make your changes and then save it by pressing Ctrl + S.
  • You may see a command console window open for a very short time when you initiate a batch file execution.

Warnings

  • Batch files used in this way will permanently delete the documents they are responsible for deleting without placing them in the system recycle bin. Therefore, extreme care should be taken when making the list of files to be deleted when writing the script.

Popular by topic