Deleting only certain files from specified location and its sub-folders [SOLVED]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Deleting only certain files from specified location and its sub-folders [SOLVED]

#1 Post by DOSadnie » 28 Apr 2023 12:25

I want to automatically get rid of e.g. temporary files of Excel which sometimes are left over after its crash. Such file can look like this
~$OriginalNameOfMyFile.xlsm
Lets say they are in a folder like

S:\Data

and its sub-folders and their sub-folders


I tried using

Code: Select all

forfiles /s /m "~$*.xlsm" /c "cmd /c del @path" /p "S:\Data"
and

Code: Select all

for /r "S:\Data" %x in (~$*.xlsm) do del "%x"
and some variants of them but I was not able to write a working one


Was it because such temporary files have the hidden attribute applied to them?
Last edited by DOSadnie on 15 Jul 2023 05:06, edited 3 times in total.

bxx
Posts: 5
Joined: 09 Mar 2023 03:49

Re: Deleting only certain files from specified location and its sub-folders

#2 Post by bxx » 29 Apr 2023 06:08

Use del /A to delete hidden files, like

Code: Select all

del /A *.xlsm

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Deleting only certain files from specified location and its sub-folders

#3 Post by DOSadnie » 25 Jun 2023 08:34

This seems to work

Code: Select all

cd /D S:\Data
del /AH *.xlsm
So thank you for your help


The only caveat is that there is no fail-safe in form of files going to the Recycle Bin, as they are just deleted permanently
Last edited by DOSadnie on 01 Jul 2023 04:17, edited 1 time in total.

miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Re: Deleting only certain files from specified location and its sub-folders

#4 Post by miskox » 25 Jun 2023 09:05

Maybe you add /s to delete if required.

Saso

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Deleting only certain files from specified location and its sub-folders

#5 Post by DOSadnie » 01 Jul 2023 04:19

Add for gaining that fail-safe of sending files to the Recycle Bin?

If yes- then it does not work

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Deleting only certain files from specified location and its sub-folders

#6 Post by DOSadnie » 15 Jul 2023 05:06

As the initial question had been answered, I have just created a separate topic for the newer one: viewtopic.php?f=3&t=10858

Post Reply