Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file to find and delete files or empty directories?  (Read 12689 times)

0 Members and 1 Guest are viewing this topic.

Interleave

    Topic Starter


    Rookie
  • Bringing hope to the hopeless
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 7
Batch file to find and delete files or empty directories?
« on: July 09, 2012, 05:33:30 PM »
So, the first one is files:

dir %1 %2 /S /B > findfile.txt

It worked but it listed everything in the directory, not just the %2 which I specified as *.nfo
The %1 is the drive and directory where I wanted to do the search - worked great.

So, I want to list ONLY the types of files I specify like *nfo
I'd also like to delete only those files if I find them.

Finally, I'd like to find empty directories and output that to a .txt file so I can go in and delete them or delete them automatically. With almost 5 TB of info on my computer, it would be really helpful to do this stuff. Come to think of it, getting rid of 0 byte files would help too.
Pc/laptop repair and upgrade expert. Love the new Ubuntu. Not so much Windows 8.

gpl



    Apprentice
  • Thanked: 27
    Re: Batch file to find and delete files or empty directories?
    « Reply #1 on: July 10, 2012, 01:38:54 AM »
    To fix your first problem, add the separator in :
    Code: [Select]
    dir %1\%2 /S /B > findfile.txt

    as for deleting the files, something like this should do it
    Code: [Select]
    for /f "delims=" %%A in (findfile.txt) do del "%%A"

    Interleave

      Topic Starter


      Rookie
    • Bringing hope to the hopeless
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 7
    Re: Batch file to find and delete files or empty directories?
    « Reply #2 on: July 10, 2012, 08:54:40 AM »
    thanks.

    What is it that the separator does that makes this work the way I wanted that not having it doesn't do?

    Also, if you could inform me as to the "for" line and how it is working, I would really appreciate it. I think it's time I started re-learning this stuff.
    « Last Edit: July 10, 2012, 09:12:46 AM by Interleave »
    Pc/laptop repair and upgrade expert. Love the new Ubuntu. Not so much Windows 8.

    gpl



      Apprentice
    • Thanked: 27
      Re: Batch file to find and delete files or empty directories?
      « Reply #3 on: July 10, 2012, 11:29:13 AM »
      Adding in the separator maker your line read

      dir c:\fred\myproject\*.nfo /s/b

      which does what you need, looks inside c:\fred\myproject for all *.nfo files - and then searches all subdirectories below it

      The For line very simply reads each line from the given file and stored it as %%A, which is passed to the del command.

      I confess I did a search for the syntax as I can never remember how to do it.. the important thing is that I knew it can be done.

      Interleave

        Topic Starter


        Rookie
      • Bringing hope to the hopeless
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 7
      Re: Batch file to find and delete files or empty directories?
      « Reply #4 on: July 10, 2012, 05:08:30 PM »
      Well, I really appreciate your help. I saved the for line in a separate batch program because I'm certain I won't always want to delete what I find. :-)

       I actually found a nice little utility to find and delete empty folders which really helps, too
      Pc/laptop repair and upgrade expert. Love the new Ubuntu. Not so much Windows 8.

      Interleave

        Topic Starter


        Rookie
      • Bringing hope to the hopeless
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 7
      Re: Batch file to find and delete files or empty directories?
      « Reply #5 on: July 12, 2012, 06:34:48 PM »
      P.S. - I finally had an opportunity to put this to the test on something that if everything got wiped out I wasn't going to care. Worked like a charm. And now that I see it in action, I understand why the \ had to be in there. Makes complete sense.

      Thanks again
      Pc/laptop repair and upgrade expert. Love the new Ubuntu. Not so much Windows 8.

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Batch file to find and delete files or empty directories?
      « Reply #6 on: February 12, 2016, 07:44:55 PM »
      For what it us worth...
      An empty directory takes up very little disk space.
      When you use XCOPY to copy directories and files, empty directories are skipped.
      http://www.computerhope.com/xcopyhlp.htm
      So if you integrate this behavior into you work habits, there is little need to delete empty directories.
      Just saying...

      patio

      • Moderator


      • Genius
      • Maud' Dib
      • Thanked: 1769
        • Yes
      • Experience: Beginner
      • OS: Windows 7
      Re: Batch file to find and delete files or empty directories?
      « Reply #7 on: February 12, 2016, 10:10:47 PM »
      For what it's worth...why humor someone who revived a 5 year old thread ? ?
      " Anyone who goes to a psychiatrist should have his head examined. "

      Salmon Trout

      • Guest
      Re: Batch file to find and delete files or empty directories?
      « Reply #8 on: February 13, 2016, 01:06:05 AM »
      why humor someone who revived a 5 year old thread ? ?
      And who revived it in order to post a spam link in their signature?