0

I want to delete extensionless files older than 7 days whose names end with _C.

Example files:

B_C_A1_C
B_C_A2_C
B_C_A3_A
test.txt 

My code:

SET mypath=%cd%\downloads
ForFiles /p %mypath% /d -7 /c "cmd /c del /q %mypath%\*_C

When I execute the code, it deletes all files which contains _C, but I want to delete only files which end with _C and are older than 7 days.

How can I fix this?

Gerhard
  • 22,678
  • 7
  • 27
  • 43
sercano07
  • 31
  • 1
  • 6
  • for ideas on how to assess files of a certain age, take a look here: `https://pastebin.com/F4yUs8Bq` As for deleting files ending with _C, for the options in the for Loop, Make use of the Wildcard to define the set. `For /R %%A in (*_C.*) Do (` – T3RR0R Feb 17 '20 at 16:53
  • Open up a Command Prompt window and enter `forfiles /?` to read its usage information. You'll note from that information that you can provide your searchmask `*_C`, using the `/M` option you have missed from your provided example! When you're there also please take a look at the various `@` variables, so that you can correct your `/C` command. _Please also learn to doublequote your paths, as standard practice._ – Compo Feb 17 '20 at 16:56
  • Thank you for help, but i imply your code like this: SET mypath=%cd% ForFiles /p %mypath% /d -7 /c "cmd /c For /R %%A in (*_c.*) Do (del /q @FILE)" it keeps file whose names end with '_c' but it delete all files which are older than 7 days. How can i prevent this? I just wanna delete files whose name end with '_C' and older then 7 days. – sercano07 Feb 17 '20 at 20:22

1 Answers1

0

For more information about the forfiles documention, see Batch file to delete files older than N days.

The wildcard expression *_C selects any files which end with _C, regardless of whether it has an extension.

The final code is insanely complicated:

forfiles /p "%cd%" /m *_C /C "cmd /c \"echo @file^|find \".\" ^|^| del @path\""

Using *_C will select the following:

anything without periods_C

It will NOT select the following:

any.thing_C
anything.txt._C
anything containing periods_C
Compo
  • 36,585
  • 5
  • 27
  • 39
ScriptKidd
  • 803
  • 1
  • 5
  • 19
  • @Compo Your edit was not what I meant. Using `*_C` **WILL** select files like `anything.txt._C` – ScriptKidd Feb 18 '20 at 13:04
  • HackingAddict0302, if I have modified anything you said, in a way which detracts from your intent, please feel free to correct it, it's your answer. However please note that your answer stated that "This code", "`_C`", "will delete the following files: `anything without periods_C`". That seemed wrong to me, as my interpretation is that, `_C` would select only files and directories named exactly `_C` or `_c`, nothing else. – Compo Feb 18 '20 at 13:15