1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

[DOS] Batch Delete

Discussion in 'BBS Hangout' started by yuantian, Aug 29, 2011.

  1. yuantian

    yuantian Contributing Member

    Joined:
    Sep 14, 2005
    Messages:
    2,849
    Likes Received:
    8
    Anyone know how to delete all files in a folder that does NOT contain a specific string?

    For example, if I have a bunch of files that have _YM in the file name, and other files that have _HO, _MM, or _OT. I only want to keep _YM and get rid of everything else in DOS command.

    Thanks.
     
  2. bejezuz

    bejezuz Contributing Member

    Joined:
    Jun 26, 2002
    Messages:
    2,772
    Likes Received:
    69
    Move the files that contain the string you want, delete the rest, move the kept files back.

    The DOS shell is very limited when it comes to scripting. I've used cygwin before, which is the GNU tools ported to Windows, and had much better luck. In most UNIX shells, what you want could be easily accomplished with a find script.
     
  3. CrazyDave

    CrazyDave Contributing Member

    Joined:
    Nov 21, 2002
    Messages:
    6,027
    Likes Received:
    439
    I knew my guess was wrong.
     
  4. SwoLy-D

    SwoLy-D Contributing Member

    Joined:
    Jul 20, 2001
    Messages:
    37,617
    Likes Received:
    1,448
    There are many ways to do this, but you need to think OUTSIDE the box. :cool:

    IT and scripting is all about being lazy and saving time... but showing your boss you're doing an extensive job. ;)

    Don't think of "deleting everything but something else," think of it as "only keeping something else."

    Therefore: output the listing of the directory containing "_YM" to a file. Using this file, do a for look that moves or copies those out to another directory temporarily, then delete everything in the directory where you had them first. You've deleted everything and salvaged the "YM" ones. Now bring back everything from the temporary directory back into the original directory.

    You're done. :eek:
     
  5. SwoLy-D

    SwoLy-D Contributing Member

    Joined:
    Jul 20, 2001
    Messages:
    37,617
    Likes Received:
    1,448
    DANG IT. :( Double post FAIL.

    *EDIT: AND, I hadn't read bejezuz's post. DOUBLE fail.
     
  6. rockbox

    rockbox Around before clutchcity.com

    Joined:
    Jul 28, 2000
    Messages:
    21,668
    Likes Received:
    10,588
    That's exactly what I was going to suggest.
     
  7. Dr of Dunk

    Dr of Dunk Clutch Crew

    Joined:
    Aug 27, 1999
    Messages:
    45,276
    Likes Received:
    31,319
    Change the file attribute of the files you want to keep to read only (+R), then issue a command to delete everything. The delete will fail on files with the read-only attribute set.

    So, for example, if you want to keep a_YM.txt and b_YM.txt, but want to delete a_MM.txt and b_OT.txt :

    attrib +R *_YM*
    del *

    You don't really need a batch file. I'm doing this off the top of my head, and am not sure which Windows you're using or how it behaves differently in various versions of it, but it's worked for me in the past. Try it in a dummy directory first. :)
     
  8. SwoLy-D

    SwoLy-D Contributing Member

    Joined:
    Jul 20, 2001
    Messages:
    37,617
    Likes Received:
    1,448
    I forgot to add that this would have been so easy to do on LINUX... :eek:
     
  9. SunsRocketsfan

    Joined:
    Jul 1, 2002
    Messages:
    6,234
    Likes Received:
    453
  10. SwoLy-D

    SwoLy-D Contributing Member

    Joined:
    Jul 20, 2001
    Messages:
    37,617
    Likes Received:
    1,448
    del *SunsChampionships*.*

    del error - file not found

    :p
     
  11. ScriboErgoSum

    ScriboErgoSum Contributing Member

    Joined:
    Aug 5, 2002
    Messages:
    3,138
    Likes Received:
    355
    I'd do a variation of this.

    First turn off the archive attribute:
    ATTRIB -A *.*

    Then turn on the archive attribute for the _YM files:
    ATTRIB +A *_YM*

    Then delete all files with the archive attribute off:
    DEL *.* /A:-A
     
  12. yuantian

    yuantian Contributing Member

    Joined:
    Sep 14, 2005
    Messages:
    2,849
    Likes Received:
    8
    Thanks guys. I am using DOS commands within SAS. I will give it a try tomorrow with both of the suggestions you guys had. It's easy to remove files with specific text, but wasn't as easy to remove everything but. BTW, I was just using player initials as examples.:grin:
     

Share This Page

  • About ClutchFans

    Since 1996, ClutchFans has been loud and proud covering the Houston Rockets, helping set an industry standard for team fan sites. The forums have been a home for Houston sports fans as well as basketball fanatics around the globe.

  • Support ClutchFans!

    If you find that ClutchFans is a valuable resource for you, please consider becoming a Supporting Member. Supporting Members can upload photos and attachments directly to their posts, customize their user title and more. Gold Supporters see zero ads!


    Upgrade Now