- 1 year ago
How to write a batch file to delete multiple specific files from different paths?
Comment
1 / 1
To write a batch file that deletes multiple specific files from different paths, you can use the del
command in combination with the file paths. Here's a sample batch script to delete multiple files:
batch@echo off
REM Specify the files to delete along with their paths
set "file1=C:\path\to\file1.txt"
set "file2=D:\another\path\file2.doc"
set "file3=E:\some\other\path\file3.jpg"
REM Check if each file exists and delete it if found
if exist "%file1%" (
echo Deleting "%file1%"
del "%file1%"
) else (
echo "%file1%" not found, skipping...
)
if exist "%file2%" (
echo Deleting "%file2%"
del "%file2%"
) else (
echo "%file2%" not found, skipping...
)
if exist "%file3%" (
echo Deleting "%file3%"
del "%file3%"
) else (
echo "%file3%" not found, skipping...
)
pause
Replace file1
, file2
, and file3
with the paths of the specific files you want to delete. The script will check if each file exists and then delete it if found. If a file is not found, it will skip the deletion for that file.
You can add more set
statements and if
blocks to delete more files from different paths as needed. Save the script with a .bat
extension, and then run it to delete the specified files. Note that deleting files is permanent, so use this script with caution and ensure you specify the correct file paths to be deleted.