- 1 year ago
How to delete any folder matching a name in specific location using batch script?
To delete any folder matching a name in a specific location using a batch script, you can use the for
command to loop through the directories in the location and the if
command to check if each directory matches the desired name. Here's an example:
batch@echo off
set "target_folder=myfolder"
set "target_location=C:\path\to\location"
for /d %%i in ("%target_location%\*") do (
if /i "%%~nxi" == "%target_folder%" (
echo Deleting folder "%%i"
rd /s /q "%%i"
)
)
In this example, the script sets the target_folder
and target_location
variables to the desired folder name and location, respectively. The for /d
command loops through each directory in the specified location, and the if /i
command checks if the directory name matches the target folder name, ignoring case.
If a match is found, the script echoes a message indicating that the folder is being deleted, and then uses the rd
command to delete the folder and its contents recursively (/s
) without prompting for confirmation (/q
).
Note that this script will permanently delete the folder and its contents without prompting for confirmation, so be sure to double-check the folder name and location before running the script.