1

I have a batch code which cleans files which are older than 15 days. But I want to only delete files that end with '_C' and they have no extension.

Here is my code:

SET mypath=%cd%/downloads 

ForFiles /p %mypath% /d -15 /c "cmd /c For /R %%A in (*_C.*) Do (del /q @fname)

Here are my files:

File Name: ---------Date:

A_C_123_C ------ 18.02.2019

A_C_456_C ------ 01.01.2018

A_C_789_C ------ 01.01.2018

Testfile------------- 01.01.2018

tmp.txt ------------- 01.01.2018

When I execute the code, it cleans Testfile also. I just want to delete extensionless files that end with '_C' and older than 15 days.

What should I do?

Palle Due
  • 5,929
  • 4
  • 17
  • 32
sercano07
  • 31
  • 1
  • 6

1 Answers1

3

You do not require incorporating the for loop into the forfiles command. forfiles has it's own search switch /s

You specifically mention you want to only do files ending with _C with no extension, but you added *_C.* which in fact means anything before _C with a dot and anything after the dot. You really just need:

@forfiles /p %mypath% /d -15 /s /m *_C /c "cmd /c If @IsDir==FALSE del @path"
Gerhard
  • 22,678
  • 7
  • 27
  • 43