1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Need Help with Batch, !!

Discussion in 'Software Development' started by g_01001, Sep 26, 2008.

Thread Status:
Not open for further replies.
  1. g_01001

    g_01001 Thread Starter

    Sep 22, 2008
    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!
  2. -Fabez-


    Jul 28, 2008
    Try doing this using a higher level language, it will be much easier.
  3. g_01001

    g_01001 Thread Starter

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

  4. ghostdog74


    Dec 7, 2005
    if you have PHP
    $directory = "c:\test";
    foreach ( glob("*.xls") as $files ){ 
     if ( (int)$SIZE >= (int)$size) {
      $size = $SIZE;
        echo "unlinking $files\n";
  5. TheOutcaste


    Aug 7, 2007
    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.


  6. g_01001

    g_01001 Thread Starter

    Sep 22, 2008
    Thaks Again. Your help is great!

    I's done.

    Regards. - Me
  7. Sponsor

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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/753523

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice