موسوعة الإلكترونيات الحرة وتعليم البرمجيات.

مواضيع جديدة

الثلاثاء، 14 يوليو 2020

Delete Files using Batch script


How to delete file in Batch?

To make and create batch file that delete files, you have to do the following
1- Open the notepad or any other text editor.

2- Turn the echo command off by typing @echo off

3- Use the command line Del followed by the file name you want to delete it.

4- Save the command lines as .bat file.

5- Run the batch file.


After running the batch, the file that you want to delete will no longer be exited any more. However, if the corresponding file was mistyped or not existed, and you run the delete batch file, the command window will show the following line:
Could Not Find C:\file_location\file_name

Example1: Delete the files data.txt, data.mp3, data.com

To delete the followings files using batch script we do write the following batch codes
@echo off
Del data.txt
Del data.mp3
Del data.com
Pause

How about deleting all the three upper files sharing the same file name at once?

We can write the following batch code
@echo off
Del data.*
Pause
Here, the asterisk (star) sign indicate that any extension name are viable and applicable to the deletion process.

Create batch file that delete itself automatically after running

Now, we are going to present the simple code methode that allow you to run any batch file and get rid of it by deleting automatically after running. This feature will maintain the drive clean without batch files residue, also it keeps code hidden after process completion. Hereby, the following code will do the job efficiently
@echo off
Del BatchFileName.Bat
Pause
Assume we want to run the batch file JustRun.bat and to delete itself directly after running. Therefore, we may embed the following batch line script at the end of the codes
Del JustRun.bat
In this case, the batch file will delete itself automatically just right after implementation.

Delete files with specified name or conditions using the batch script

Assume we have the following file names and their extension names as follows:
Apple.txt, app.apk, ads.txt, adsense.txt, celine.mp3, celine.mpeg
We may use the question mark sign to present each single letter, while the star or the asterisk sign will represent a group of letters. The following table will convey the main and the whole idea
Batch Script
Action (deleted files / files)
Del apple.txt
Apple.txt
Del a*.txt
Apple.txt, ads.txt, adsense.txt
Del a*.a*
app.apk
Del a*s.*
ads.txt
Del a*.*
Apple.txt, app.apk, ads.txt, adsense.txt
Del *.txt
Apple.txt, ads.txt, adsense.txt
Del celine.*
celine.mp3, celine.mpeg
Del celine.???
celine.mp3
Del celine.????
celine.mpeg
Del ad?????.*
adsense.txt
Del ad*.*
ads.txt, adsense.txt

So, if we intend to delete files that begin with a specific letter, we just follow the space after del command with that letter, followed by * sign to represent any letter after the specified letter no matter the count of these letters. On the other hand, the ? sign represent just and only single letter, number, or symbol. i.e just one character.

How to delete all files in folder?

To delete each file in specific folder, we write the following batch code for the batch file inside that folder
Del *.*
This simple batch command will remove and delete every single file inside that folder, except folders inside it. That’s why the del command is not used to delete folders.
After applying the delete batch to specific folder, a command prompt will appear presenting the statement, showing the folder content location, followed by the sentence: Are you sure (Y/N)?

Delete all files inside the Test folder, located at the drive C:\

Just run the following batch command
Del c:\Test
In this case, all files located inside the Test folder will be deleted except folders inside it.

ليست هناك تعليقات:

إرسال تعليق

اترك لنا أي سؤال أو تعليق ونحن نسعد بخدمتكم وإجابة استفساراتكم

???????