0

Can anybody help me in Creating a batch file which should delete files which names are preceded by a keyword.

For example : I have a file names ABC_%Date%.CSV , and it gets created twice a day. I want to delete the this files older than 10days.

Please help me in creating a batch file.

I'm new to Batch scripting,also you can suggest me any useful sites for my learning.

Thanks in advance, Thiru

npocmaka
  • 55,367
  • 18
  • 148
  • 187
Thiru
  • 1
  • 1
  • 1
    Please note that https://stackoverflow.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – DavidPostill Jan 01 '18 at 14:27
  • You can use [FORFILES](https://ss64.com/nt/forfiles.html) to delete files based on how old the file is and use a search mask as well. – Squashman Jan 01 '18 at 18:24
  • 1
    I think that it's worth noting that `ForFiles` uses the last modified time not the created time, this may be an important difference for the OP. – Compo Jan 01 '18 at 19:56

2 Answers2

1

Read Bat file to delete files only when younger files are present for understanding the strategy used in the batch file below to delete all ABC_*.CSV files in current directory except the latest 20 modified (or created) files.

@for /F "skip=20 delims=" %%I in ('dir "ABC_*.CSV" /A-D /B /O-D /TW 2^>nul') do @del "%%I"

So this simple one line batch file keeps the 20 newest ABC_*.CSV files in current directory and delete all other (older) files matching this pattern. There must be more than %Date% in file name of each ABC_*.CSV when being created twice per day. Or is an already existing ABC_%Date%.CSV file overwritten on existing already and so there are only 10 files for 10 different dates?

The value after FOR option skip= determines how many of the newest files should be kept. On creating two ABC_*.csv files per day and keeping the newest 20 files, all files older than 10 days are deleted by this single line batch file. Well, as written in referenced answer, the age of the files does not really matter, just their file sizes and the remaining free disk space. This solution makes sure that at least the latest 20 ABC_*.csv files are stored on storage media independent on how often they are created and if the creation of the files is really successful every day.

To use the creation date of the file in the directory use:

@for /F "skip=20 delims=" %%I in ('dir "ABC_*.CSV" /A-D /B /O-D /TC 2^>nul') do @del "%%I"

/TW (write time) is replaced here by /TC (creation time) in comparison to above command line.

Please note that the creation date does not store when the file itself was created the first time. It stores the time stamp when this file was created first time in this directory. So for example a file with last modification date 2017-12-30 18:24:30 copied from folder X to another folder Y today has the file creation date 2018-01-01 20:59:38 in folder Y while the last modification date is unmodified 2017-12-30 18:24:30 and file creation date of same file in folder X is for example also 2017-12-30 18:24:30. In other words the file in folder Y was created after its contents being last modified.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • del /?
  • dir /?
  • for /?

Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.

Mofi
  • 46,139
  • 17
  • 80
  • 143
1

Use the below code (with caution, cause there is no turning back).

Define the correct path on first line and substitute Date, on lines 1 and 2, according to your wishes. Best luck.

if exist "C:\Users\-\myfolder\ABC_*Date*.CSV" (
   del ABC_*Date*.CSV
   echo "Files found and deleted."
) else (
   echo "Nothing done."
)
statosdotcom
  • 3,109
  • 2
  • 17
  • 40
  • The user wants to delete files **older** than **10** days that match a pattern. This script deletes all files that match the pattern regardless of date. – Squashman Jan 01 '18 at 18:22
  • I agree with you. My script focuses **filename pattern**, not **datefile**. But, from the OP: `I have a file names ABC_%Date%.CSV , and it gets created twice a day. I want to delete the this files older than 10days`. Considering `Date` a var as the OP proposed, if today `Date` is 01012018 and he runs my script with `Date` equals `12202017` he gets what he wants: deleted all files ten days earlier than today, for example. Clean and simple. Just a point of view question, different approach. Thanks. – statosdotcom Jan 01 '18 at 18:32