How to delete files from a folder recursively based on an expression?

5

I'm using a project where there are several temporary files that for some reason have not been deleted over time and are taking up a lot of space.

I need to delete all images from that directory, recursively, when those files begin with the letters "LX".

How can I do this using PowerShell or CMD?

For example, in the structure below, each of the subfolders has these files started by "LX".

DIRETORIO
    FOTOS_1
       1.jpg
       2.jpg
       LXa45axdgg.jpg
    FOTOS_2
       1.jpg
       2.jpg
       LXa4bbbbxdgg.jpg
    FOTOS_3
       1.jpg
       2.jpg
       LXa4555g.jpg
    
asked by anonymous 03.07.2017 / 14:01

2 answers

3

Using Powershell is easy.

get-childitem . -include LX*.* -recurse | foreach ($_) {remove-item $_.fullname}

The Get-Children command returns a collection of files.

  • The dot ( . ) represents the starting place of departure (the folder where script is running).

  • The -include LX*.* , serves to "tell" the command to return only files that satisfy this condition.
    You can add more conditions by separating them with commas: -include LX*.*, lx*.* .

  • The recurse causes the command to be recursive. That is, look inside the daughter folders of the current folder and within daughters daughters and so on.

After Get-Children is made a foreach that passes through all elements of the collection and calls the remove-item command to delete it.

    
03.07.2017 / 14:09
5

Using cmd , you can do the following:

del /s *.{sua extensão}

or if you want the file name, you can do the following:

del /s LX*

Remembering that you must be in the "root directory", that is, in DIRECTORY .

If you'd like a confirmation before deleting each file, use the /p option.

    
03.07.2017 / 14:16