How to delete files from a recursively expression-based folder?

Asked

Viewed 2,223 times

6

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

I need to delete all images from this particular directory recursively when these files start 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 initiated 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

3 answers

7

Using the cmd, you can do the following:

del /s *.{sua extensão}

or if you wish by file name, you can do the following:

del /s LX*

Remembering that for this you must be in the "root directory", ie in DIRECTORY.

If you want a confirmation before deleting each file, use the option /p.

5


Using the Powershell is easy.

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

The command Get-Children returns a collection of files.

  • The point (.) represents the place of departure (the folder where the script is spinning).

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

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

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

1

Del/f/s/q LX*.*

/f = Strength Read Only File Deletion

/s = includes ALL subdirectories, ie will delete ALL the files initiated in LX of all directories from which you execute the command.

/q = does not ask for confirmation. If you wish to confirm, one by one, use /p.

Remember that using the /s, the command will delete from ALL the folders then make sure that you can actually delete ALL these files.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.