Page 1 of 1

delete files in Folder with version-stamp

Posted: 27 Jan 2019 10:53
by Chris1985
Hi

I need a batch file for delete of many files with a version-stamp. I have found a Code for the newest file in a folder, but i dont now rebuild it.

@echo off & setlocal
set "MySearchString=_"
for /r %%a in (*) do for /f "delims=" %%i in ('echo("%%~na" ^| findstr /i "%MySearchString%"') do del "%%~fa"


this are my files in my folder for example
6578_01.pdf
6578_02.pdf
6578_03.pdf
8844_12.pdf
8844_13.pdf
8844_14.pdf
0815_12.pdf
0815_20.pdf

and this i need as result:
6578_03.pdf
8844_14.pdf
0815_20.pdf

I hope someone can help me

thanks
Chris

Re: delete files in Folder with version-stamp

Posted: 27 Jan 2019 12:43
by Aacini
I am afraid I don't understand your requirement... Do you want to remove all files with the same string before the underscore, excepting the last one? If so, then you may try this solution:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "group="
for /F "tokens=1* delims=_" %%a in ('dir /A-D /B *.pdf') do (
   if "%%a" neq "!group!" (
      set "group=%%a"
      set "file=%%b"
   ) else (
      ECHO del "!group!_!file!"
      set "file=%%b"
   )
)
If the displayed files are the right ones for deletion, remove the ECHO part before the DEL command.

Antonio

Re: delete files in Folder with version-stamp

Posted: 28 Jan 2019 01:40
by Chris1985
Hi

thanks
it works with the correct "date created", but i must copy all files to another location an there changed the created time.
Would it be possible than the batch can the "date modified" time used? This time are not changed on copy......


Thanks for help
Chris

Re: delete files in Folder with version-stamp

Posted: 06 Feb 2019 10:35
by amzoun95
The reason you can't delete those and they are read-only is because most
files there are in the "read only memory" of the device (the ROM). Which
files do you want to delete from the Windows directory?

Some things, like the Start Menu folder, and be modified by you are an
application but the system files that are needed by the OS are secure.

Re: delete files in Folder with version-stamp

Posted: 13 Feb 2019 13:35
by pieh-ejdsch
Hello Chris1985,

and why exactly do you want to change this creation time?
Either you make a wrong file versioning, which always creates an even younger date of creation of a file, because this is newly created.
Or they can properly version by backing up changed files leaving the original creation date.
At the moment, it looks like they have no strategy for backing up the data.
What are you planning to do?

Phil