Page 1 of 1

How to delete subfolders and files from parent folders found in the variable of a bat script?

Posted: 28 Mar 2020 20:47
by caiopacheco02
What I'm trying to do is find all the folders named "temp" on local disk C and then delete all files and subfolders that are inside it, but I don't know how to delete it, I only managed to get to the part of finding all folders and put them in the variable, but I don't know the correct code to delete. Help.

Code: Select all

@echo off
setlocal
setlocal enabledelayedexpansion
@echo off
for /d /r "c:\" %%i in (temp) do (
@if exist "%%i" (
@set _variable=%%i
@echo !_variable!
)
)
endlocal

Re: How to delete subfolders and files from parent folders found in the variable of a bat script?

Posted: 29 Mar 2020 13:46
by Squashman
Open up a command prompt and type: help. I bet you will find a command or two that will help you. Probably easier to Google search for deleting a file or folder using a batch file.

You do not need to assign the FOR variable to an environmental variable, which also means you will not need delayed expansion.