keep Higher number file but Delete rest all files
Moderator: DosItHelp
keep Higher number file but Delete rest all files
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
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
Re: keep Higher number file but Delete rest all files
This should work. It keeps 998300.txt and deletes all other txt files with numeric file name.
Be aware of the numeric limits for numeric comparisons.
Regards
aGerman
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
Re: keep Higher number file but Delete rest all files
The Batch file below work OK if there are no other .TXT files in current folder:
This Batch file process all files excepting the last one. The natural DIR and FOR (*.txt) order show higher number file at last.
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.
Re: keep Higher number file but Delete rest all files
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
Re: keep Higher number file but Delete rest all files
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%"
Re: keep Higher number file but Delete rest all files
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.
Re: keep Higher number file but Delete rest all files
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.

Re: keep Higher number file but Delete rest all files
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
Re: keep Higher number file but Delete rest all files
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:\
Re: keep Higher number file but Delete rest all files
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.
Re: keep Higher number file but Delete rest all files
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?
Re: keep Higher number file but Delete rest all files
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.
Re: keep Higher number file but Delete rest all files
Aacini
Rename 998300.txt to 1998300.txt and try again.
It lists alphanumerically
Regards
aGerman
Edit: You already fixed it.
Rename 998300.txt to 1998300.txt and try again.
It lists alphanumerically

Regards
aGerman
Edit: You already fixed it.
Re: keep Higher number file but Delete rest all files
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.
Re: keep Higher number file but Delete rest all files
You missed the last little detail: change ...0000%%f by ...0000%%~Nf.