1

How do I create a batch file that deletes files from multiple directories?

D:\L1\asdasda\L3\*.txt
D:\L1\dfghfghfh\L3\*.txt
D:\L1\tyutyugj\L3\*.txt
D:\L1\ytrtyrty\L3\*.txt

Like:

D:
del "d:\L1\*\L3\*.txt"

Note by barlop- questioner adds-

I have about a hundred of those directories. I do not want to delete the Folders, only the files. All of them have a L3 Folder, and they all contain some files with the same extension. Those are just temporary files, but it does not delete automatically.

  • you haven't stated the error you get – barlop Dec 10 '15 at 9:23
  • you can say del a.a b.b – barlop Dec 10 '15 at 9:24
  • Are the "*" really the file name (deleting that works here) or is that only some unnecessary HTML markup (then please write just the real file name)? – Werner Henze Dec 10 '15 at 9:51
  • Sorry, previously it couldn't display the * properly, therefore I used a HTML code to display (*). But I'm not sure why it works now. – Andrew Lay Dec 11 '15 at 9:53
  • oh you want like an rmdir with a wildcard. I guess what i'd do is not that.. I'd get a list of the files that you want to delete, into a file. Then i'd get the word "del " before them, and make that file a batch file and run it. That's quite safe too 'cos then you see exactly what would be getting deleted. You can open the list of files in notepad++ and find the beginning of each line with ^ and replace it with "del " – barlop Dec 11 '15 at 14:55
0

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 *.txt files)

@ECHO ON

SET SourceDir=D:\L1
SET SourceSubDir=L3

:: --// 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"
GOTO EOF

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

(Type in 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.
  command-parameters
             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
from %I.

If Command Extensions are enabled, the following additional
forms of the FOR command are supported:

FOR /D

FOR /D %variable IN (set) DO command [command-parameters]

    If set contains wildcards, then specifies to match against directory
    names instead of file names.
0

Interpreting your line \L1\*\L3\*. As: For a given directory L1 find all the subdirectories that have a subdirectory L3 and delete all the files in it.

A script has to be written to walk the directory structure. It not easily doable as a batch file you may have to use Perl or powershell which have ways of getting list of directories or files and then doing a repeated action on them.

In pseudo code

Get list directories
For each dir 
  Cd into it
  Check if The dir L3 exists
  If it does then del .\L3\*.* 
  Go back up a level
Next dir

I will think about this and did this in batch file code although I am thinking of redoing it in powershell just to see the difference.

Ok I thought about it

This batch file will walk directory structure from where is is run from and delete the content of any directory that has the name "L3".

@echo off
for /r %%d in (*) do call :process "%%d"
goto :eof

:process
pushd "%~dp1"
for /f "delims=" %%A in ('cd') do (
     set foldername=%%~nxA
    )
if "%foldername%"=="L3" del/q *.*
popd
goto :eof

It may be possible to make is simpler but the action of getting just the current directory name is messy (the for loop in process section)

If a directory starts out like this

└───L1
    │   1.dat
    │
    ├───Alice
    │   │   9.dat
    │   │
    │   └───L3
    │           c.dat
    │           d.dat
    │
    ├───Bob
    │       6.dat
    │
    ├───Carole
    │   │   2.dat
    │   │
    │   └───L3
    │           a.dat
    │           b.dat
    │
    └───Ted
            6.dat

run cleantree should make look like this

└───L1
    │   1.dat
    │
    ├───Alice
    │   │   9.dat
    │   │
    │   └───L3
    ├───Bob
    │       6.dat
    │
    ├───Carole
    │   │   2.dat
    │   │
    │   └───L3
    └───Ted
            6.dat
0
for /f %G in ('dir /ad /b D:\L1\') do del D:\L1\%G\L3\*.txt

for /f runs following do command on all files in set

%G in ('dir /ad /b D:\L1\') variable set as the bare format for each folder within given directory

delete command using set %G value & wildcard for the directory


Test Run First

run with echo to print to the console the commands that will run to test the command without actually running it:

for /f %G in ('dir /ad /b D:\L1\') do echo del D:\L1\%G\L3\*.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.