keep Higher number file but Delete rest all files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
mohdfraz
Posts: 69
Joined: 29 Jun 2011 11:16

keep Higher number file but Delete rest all files

#1 Post by mohdfraz » 20 May 2012 11:48

Hi,

I have a folder which receive thousands of random number Txt files like this;

-20000.txt
-100.txt
998300.txt
84345.txt

I need a batch file which can keep higher number file and delete all lower number files.

Thanks

aGerman
Expert
Posts: 4740
Joined: 22 Jan 2010 18:01
Location: Germany

Re: keep Higher number file but Delete rest all files

#2 Post by aGerman » 20 May 2012 13:45

This should work. It keeps 998300.txt and deletes all other txt files with numeric file name.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%i in ('dir /a-d /b *.txt^|findstr /ix "\-*[0-9][0-9]*\.txt"') do (
  if not defined oldfile set "oldfile=%%~ni"
  if !oldfile! lss %%~ni (
    del !oldfile!.txt
    set "oldfile=%%~ni"
  ) else (
    if !oldfile! gtr %%~ni del %%i
  )
)

Be aware of the numeric limits for numeric comparisons.

Regards
aGerman

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: keep Higher number file but Delete rest all files

#3 Post by Aacini » 20 May 2012 18:11

The Batch file below work OK if there are no other .TXT files in current folder:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set previousName=
for %%f in (*.txt) do (
   if defined previousName (
      ECHO del "!previousName!"
   )
   set "previousName=%%f"
)

This Batch file process all files excepting the last one. The natural DIR and FOR (*.txt) order show higher number file at last.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: keep Higher number file but Delete rest all files

#4 Post by Squashman » 20 May 2012 20:23

Aacini wrote:The Batch file below work OK if there are no other .TXT files in current folder:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set previousName=
for %%f in (*.txt) do (
   if defined previousName (
      ECHO del "!previousName!"
   )
   set "previousName=%%f"
)

This Batch file process all files excepting the last one. The natural DIR and FOR (*.txt) order show higher number file at last.

Are you sure about that. I am pretty sure the cmd prompt does not sort in numerical order like explorer does.
1.txt
11.txt
2.txt

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: keep Higher number file but Delete rest all files

#5 Post by foxidrive » 20 May 2012 21:36

Building on aGerman's code: this is a different method and it assumes that all the text files are involved in the test and have numerical names.


Code: Select all

@echo off
setlocal EnableDelayedExpansion
set old=0
set name=""
for /f "delims=" %%a in ('dir /a-d /b *.txt ') do (
  if %%~na GTR !old! set old=%%~na&set "name=%%a"
)
attrib +h "%name%"
del *?.txt
attrib -h "%name%"

mohdfraz
Posts: 69
Joined: 29 Jun 2011 11:16

Re: keep Higher number file but Delete rest all files

#6 Post by mohdfraz » 21 May 2012 00:52

aGerman wrote:This should work. It keeps 998300.txt and deletes all other txt files with numeric file name.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%i in ('dir /a-d /b *.txt^|findstr /ix "\-*[0-9][0-9]*\.txt"') do (
  if not defined oldfile set "oldfile=%%~ni"
  if !oldfile! lss %%~ni (
    del !oldfile!.txt
    set "oldfile=%%~ni"
  ) else (
    if !oldfile! gtr %%~ni del %%i
  )
)

Be aware of the numeric limits for numeric comparisons.

Regards
aGerman


aGerman, U r awesome. It worked like a charm. Many many thanks.





foxidrive wrote:Building on aGerman's code: this is a different method and it assumes that all the text files are involved in the test and have numerical names.


Code: Select all

@echo off
setlocal EnableDelayedExpansion
set old=0
set name=""
for /f "delims=" %%a in ('dir /a-d /b *.txt ') do (
  if %%~na GTR !old! set old=%%~na&set "name=%%a"
)
attrib +h "%name%"
del *?.txt
attrib -h "%name%"


Foxidrive, ur edited code also work great, Thanks a lot.






Aacini wrote:The Batch file below work OK if there are no other .TXT files in current folder:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set previousName=
for %%f in (*.txt) do (
   if defined previousName (
      ECHO del "!previousName!"
   )
   set "previousName=%%f"
)

This Batch file process all files excepting the last one. The natural DIR and FOR (*.txt) order show higher number file at last.

Aacini, I tried but some how i was not able to make it work. But I m great full for ur reply.



Thank you guys all of you. U r great. Many thanks.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: keep Higher number file but Delete rest all files

#7 Post by Aacini » 22 May 2012 11:46

mohdfraz wrote:Aacini, I tried but some how i was not able to make it work.

My Batch file is pure and simple, but when I wrote a program that may delete files or achieve something harmful, I used to add an ECHO command that just SHOW the real command. When you are satisfied with the results (by review the commands that would really execute), just remove the ECHO part of the DEL command to really delete the files. 8)

mohdfraz
Posts: 69
Joined: 29 Jun 2011 11:16

Re: keep Higher number file but Delete rest all files

#8 Post by mohdfraz » 22 May 2012 12:19

foxidrive wrote:Building on aGerman's code: this is a different method and it assumes that all the text files are involved in the test and have numerical names.


Code: Select all

@echo off
setlocal EnableDelayedExpansion
set old=0
set name=""
for /f "delims=" %%a in ('dir /a-d /b *.txt ') do (
  if %%~na GTR !old! set old=%%~na&set "name=%%a"
)
attrib +h "%name%"
del *?.txt
attrib -h "%name%"



I need a little Alteration in the code, when files r deleted the original running software which is creating files gives error some time,, of file not found and stop the process. So what I want that instead of deleting all files, It should xcopy ONLY the highest number file to folder c:\ and leave other files as its.

Thanks

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: keep Higher number file but Delete rest all files

#9 Post by Aacini » 22 May 2012 13:00

mohdfraz wrote:... what I want that instead of deleting all files, It should xcopy ONLY the highest number file to folder c:\ and leave other files as its.

Thanks

Code: Select all

@echo off
for %%f in (*.txt) do set lastName=%%f
xcopy %lastName% c:\

mohdfraz
Posts: 69
Joined: 29 Jun 2011 11:16

Re: keep Higher number file but Delete rest all files

#10 Post by mohdfraz » 22 May 2012 13:31

Aacini wrote:

Code: Select all

@echo off
for %%f in (*.txt) do set lastName=%%f
xcopy %lastName% c:\


I tried this code it did movied a file but not the highest number.txt file.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: keep Higher number file but Delete rest all files

#11 Post by Aacini » 22 May 2012 13:45

Code: Select all

>dir /b
-100.txt
-20000.txt
1.txt
11.txt
111.txt
2.txt
84345.txt
998300.txt

>for %f in (*.txt) do set lastName=%f

>echo %lastName%
998300.txt


What exactly happens in your case?

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: keep Higher number file but Delete rest all files

#12 Post by Aacini » 22 May 2012 13:59

OK. I fixed the error:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
for %%f in (*.txt) do (
   set name=00000000000000000%%~Nf
   set a[!name:~-18!]=%%f
)
for /F "tokens=2 delims==" %%f in ('set a[') do set lastName=%%f
xcopy %lastName% c:\
Last edited by Aacini on 22 May 2012 14:07, edited 1 time in total.

aGerman
Expert
Posts: 4740
Joined: 22 Jan 2010 18:01
Location: Germany

Re: keep Higher number file but Delete rest all files

#13 Post by aGerman » 22 May 2012 14:02

Aacini

Rename 998300.txt to 1998300.txt and try again.
It lists alphanumerically :wink:

Regards
aGerman

Edit: You already fixed it.

mohdfraz
Posts: 69
Joined: 29 Jun 2011 11:16

Re: keep Higher number file but Delete rest all files

#14 Post by mohdfraz » 22 May 2012 14:05

Aacini wrote:OK. I fixed the error:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
for %%f in (*.txt) do (
   set name=00000000000000000%%f
   set a[!name:~-18!]=%%f
)
for /F "tokens=2 delims==" %%f in ('set a[') do set lastName=%%f
xcopy %lastName% c:\



Aacini, Wahooo, awesome, great, it works like a charm. Many many thanks.
Don't know how u guys do all that but that is great. Thank you.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: keep Higher number file but Delete rest all files

#15 Post by Aacini » 22 May 2012 14:09

You missed the last little detail: change ...0000%%f by ...0000%%~Nf.

Post Reply