Solved: Need Help with Batch, !!

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

g_01001

Thread Starter
Joined
Sep 22, 2008
Messages
11
Hi, I got a problem and I don’t seem to find the answer anywhere.
The question that I want to ask is how to make a batch that identify the bigger file located in certain directory and then erase all others files that are smaller than the larger file.

Here is the full description: In a directory (for example) D:\BATCH, there are a lot of files all with the same file extension (like “*.xls”) what I need to do is to erase all files whit that extension except for the one file that has the bigger size.

For this I was wondering if exist any way to identify the bigger file by the size, and then erase all the other files that are smaller.
So that in the end it just remain one single file (the bigger one) in the directory.

Thanks a lot for the help!
 

g_01001

Thread Starter
Joined
Sep 22, 2008
Messages
11
HI, thanks for respond.
Yes, I know that it will be easier , but it has to be in batch.

Thanks.
Regards.
 
Joined
Dec 7, 2005
Messages
146
if you have PHP
Code:
<?php

$size=0;
$directory = "c:\test";
foreach ( glob("*.xls") as $files ){ 
 $SIZE=filesize($files);
 if ( (int)$SIZE >= (int)$size) {
  $size = $SIZE;
 }else{
    echo "unlinking $files\n";
    unlink($files);
 }
}
?>
 
Joined
Aug 7, 2007
Messages
9,028
Hmm, I think a one line batch statement is much easier:
For /F "tokens=* skip=1" %%I In ('dir /B /A-D /O-S *.xls') Do Del /F %%I

If you have two or more files that are the exact same size you'll need to specify additional sort orders for the /O switch like name or time. If not specified, it will compare the Last Modified times keeping the oldest; if those times are equal, it will sort by name, keeping the first in alphabetical order.
Be sure to add them after the S
name - alphabetical (/O-SN) will keep the first; Reverse alphabetical (/O-S-N) will keep the last

Note that for filenames with numbers, CMD may see them differently than Windows Explorer or My Computer. CMD will sort names character by character, so 1001.txt comes before 101.txt. In Windows Explorer or My Computer, it will sort them as numbers, so 101.txt comes before 1001.txt, unless you've applied this tweak.

time (/O-SD) Oldest will be kept, (/O-S-D) Newest will be kept
You can specify Date Created, Last Written, or Last Accessed for the time field used to compare with the /T switch.

HTH

Jerry
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top