BATCH FILE TO DELETE FROM SUBFOLDERS WITH DIFFERENT NAMES DYNAMICALLY USING WILDCARD FOR SUBFOLDER NAMES
Batch file to delete files from wildcard folder
Here's an easy and simple batch script example that I use to complete this type of task all the time, I plugged in the variable folder paths to suit your needs as you describe:
SAMPLE BATCH SCRIPT
(Set your variable root folder and subfolder up top, and the
FOR /D and
FOR loops iterate accordingly to do the rest of the magic traversing the directory as the logic specifies and completes the
DEL /Q /F command for the
:: --// note the asterisk wildcard after SourceDir in the first FOR /D loop using X as the variable
:: --// note the X variable appended to the beginning of the second FOR (no /D switch here) loop in the SET part using Y as the variable
FOR /D %%X IN ("%SourceDir%\*") DO FOR %%Y IN ("%%~X\%SourceSubDir%\*.txt") DO DEL /Q /F "%%~Y"
NOTE: If you plan to run this with a copy and paste manually in the command prompt, then the variables in the
FOR loops need to have one of the percent signs removed in all parts so use the below for that part if you're running this manually with a copy and paste rather than in a batch script and executing that which is how the above example will work.
FOR /D %X IN ("%SourceDir%\*") DO FOR %Y IN ("%~X\%SourceSubDir%\*.txt") DO DEL /Q /F "%~Y"
FURTHER DETAIL AND RESEACH
FOR /? in Windows Command Prompt to see this detail)
FOR (with no switch)
Runs a specified command for each file in a set of files.
FOR %variable IN (set) DO command [command-parameters]
%variable Specifies a single letter replaceable parameter.
(set) Specifies a set of one or more files. Wildcards may be used.
command Specifies the command to carry out for each file.
Specifies parameters or switches for the specified command.
To use the FOR command in a batch program, specify %%variable instead
of %variable. Variable names are case sensitive, so %i is different
If Command Extensions are enabled, the following additional
forms of the FOR command are supported:
FOR /D %variable IN (set) DO command [command-parameters]
If set contains wildcards, then specifies to match against directory
names instead of file names.