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

Author Topic: Batch or script to check for blank files - Please help  (Read 17846 times)

0 Members and 1 Guest are viewing this topic.

antonhoward

    Topic Starter


    Greenhorn

    Batch or script to check for blank files - Please help
    « on: January 11, 2010, 10:03:29 AM »
    Hi,
    I need a script that I can run as a scheduled task to show me any blank (empty) files in a particular folder.
    Ideally the script can check a folder for the empty files and if there are any it will list them in an e-mail to me or on-screen if that's not possible.
    Can anyone help, I'm a newbie with this.

    Thanks
    Anton

    todouble22



      Rookie

      Thanked: 2
      Re: Batch or script to check for blank files - Please help
      « Reply #1 on: January 11, 2010, 11:30:01 AM »
      i know how frustrating it can be to not get any response . I am testing a few things out . have you reference the home page at all?  http://www.computerhope.com/forhlp.htm  this link has some helpful info.  there has to be something with creating a list with files that have zero file size.  I know how to do it for varying file extensions now I just need to figure out how to filter through to list the ones that have zero bytes. 
      -todd

      BillRichardson



        Intermediate

        Thanked: 15
        Re: Batch or script to check for blank files - Please help
        « Reply #2 on: January 11, 2010, 11:46:21 AM »
        C:\batch>type empty2.bat
        @echo off

        Code:
        dir  /x *.*  |  find " 0 "  >  zerotxt.txt

        for /f "tokens=5 delims= " %%i in (zerotxt.txt) do ECHO  "%%i"

        rem for /f "tokens=5 delims= " %%i in (zerotxt.txt) do del /F  "%%i"

        rem remove the rem from above  when satisfied  list is correct
        C:\batch>

        Warning:  Be very careful with search and delete  from root down:  /s
        cd \
        dir  /X / s *.*  |  find " 0 "  >  zerotxt.txt
        Bill Richardson

        todouble22



          Rookie

          Thanked: 2
          Re: Batch or script to check for blank files - Please help
          « Reply #3 on: January 11, 2010, 12:22:49 PM »
          C:\batch>type empty2.bat
          @echo off

          Code:
          dir  /x *.*  |  find " 0 "  >  zerotxt.txt

          for /f "tokens=5 delims= " %%i in (zerotxt.txt) do ECHO  "%%i"

          rem for /f "tokens=5 delims= " %%i in (zerotxt.txt) do del /F  "%%i"

          rem remove the rem from above  when satisfied  list is correct
          C:\batch>

          Warning:  Be very careful with search and delete  from root down:  /s
          cd \
          dir  /X / s *.*  |  find " 0 "  >  zerotxt.txt

          could you possible explain this a lil bit?
          -todd

          BillRichardson



            Intermediate

            Thanked: 15
            Re: Batch or script to check for blank files - Please help
            « Reply #4 on: January 11, 2010, 12:54:11 PM »
            "Would you  explain the code"?

            C:\batch>type empty2.bat
            Code: [Select]
            @echo off


            dir  /x *.*  |  find " 0 "  >  zerotxt.txt

            rem dir *.*  will list all files in current directory
            rem The output is piped ( sent ) to find to list only lines with zero bytes
            rem the output of find is is redirected ( > ) to a file ( zerotx.txt )

            for /f "tokens=5 delims= " %%i in (zerotxt.txt) do ECHO  "%%i"

            rem the above for is a test to verify list is correct

            rem for /f "tokens=5 delims= " %%i in (zerotxt.txt) do del /F  "%%i"

            rem the above for will delete files with zero bytes ( your must remove the leading "rem"

            rem remove the rem from above  when satisfied  list is correct
            rem C:\batch>

            rem Warning:  Be very careful with search and delete  from root down:  /s
            rem cd \
            rem dir  /X / s *.*  |  find " 0 "  >  zerotxt.txt
            Bill Richardson

            ghostdog74



              Specialist

              Thanked: 27
              Re: Batch or script to check for blank files - Please help
              « Reply #5 on: January 12, 2010, 12:03:27 AM »
              you can find the size of files using %~zI and checking against 0. although piping dir output to find with " 0 " is one way, files with names like "file 0 name.txt" will five you false positive.

              Salmon Trout

              • Guest
              Re: Batch or script to check for blank files - Please help
              « Reply #6 on: January 12, 2010, 12:27:12 AM »
              although piping dir output to find with " 0 " is one way, files with names like "file 0 name.txt" will five you false positive.

              Using Dir /x *.* | find "0", as suggested by Bill Richardson, will actually find every file with a zero anywhere in the date, time, size or name! That means every file created since Jan 1st 2000, as well as many before that.

              14/04/20004:01           250,048              ntldr

              I need hardly point out that this is not a very good way of selecting files to delete.








              ghostdog74



                Specialist

                Thanked: 27
                Re: Batch or script to check for blank files - Please help
                « Reply #7 on: January 12, 2010, 12:35:10 AM »
                actually, the find pattern he posted has space, " 0 " and not "0".

                Salmon Trout

                • Guest
                Re: Batch or script to check for blank files - Please help
                « Reply #8 on: January 12, 2010, 12:40:38 AM »
                actually, the find pattern he posted has space, " 0 " and not "0".

                So it does. Er... sorry, Bill!


                antonhoward

                  Topic Starter


                  Greenhorn

                  Re: Batch or script to check for blank files - Please help
                  « Reply #9 on: January 12, 2010, 04:49:01 AM »
                  Blimey, I think I'm in over my head.
                  I'm more familiar with Vb Script if anyone can give me an example of some code that can locate the files.
                  I do need the script to e-mail me if a blank file is found though, rather than delete it.
                  The reason is that if there is a blank file, it means something is broken and I need to fix it.
                  I need something automatic that can check this for me as a manual process will soon be forgotten.
                  Thanks
                  Anton

                  BillRichardson



                    Intermediate

                    Thanked: 15
                    Re: Batch or script to check for blank files - Please help
                    « Reply #10 on: January 12, 2010, 09:07:46 AM »
                    I do need the script to e-mail me if a blank file is found though, rather than delete it.

                    I have not tested or used the following (bill ):

                    http://www.computerhope.com/forum/index.php?topic=62553.0

                    http://www.blat.net/


                    http://www.robvanderwoude.com/email.php


                    To use this type of command in batch files we need to:

                    •precede the string with the START command
                    •replace every single percent sign ( % ) by double percent signs ( %% )
                    •"escape" ampersands ( & ) with carets ( ˆ )
                    •limit the length of the mailto string to the maximum allowable command line length minus 6 (leaving a maximum of 121 characters for MS-DOS, or 249 characters for Windows NT 4/Windows 2000, or 2035 for Windows XP)
                     

                    Examples:
                     


                    START mailto:[email protected]?subject=Test%%20messageˆ&[email protected]ˆ&body=Hi,%%0D%%0A%%0D%%0AThis%%20is%%20an%%20automatically%%20created%%20message.%%0D%%0A%%0D%%0ABye

                    This command will create a message to [email protected], with a carbon copy to [email protected], with the words "Test message" in the subject field.
                    The message itself will consist of the following text:

                    Hi,

                    This is an automatically created  message.

                    Bye
                     
                    « Last Edit: January 13, 2010, 10:22:58 AM by BillRichardson »
                    Bill Richardson

                    antonhoward

                      Topic Starter


                      Greenhorn

                      Re: Batch or script to check for blank files - Please help
                      « Reply #11 on: January 14, 2010, 06:50:37 AM »
                      Ok great,
                      I have now got a batch script that creates a text file with the files listed that are empty.
                      And i have an example of how to send an e-mail with a batch script.
                      Can someone help me to get my batch script to e-mail the body of my text file as the subject.

                      It's linking the two ideas together where I'm getting stumped.

                      Thanks
                      Anton

                      todouble22



                        Rookie

                        Thanked: 2
                        Re: Batch or script to check for blank files - Please help
                        « Reply #12 on: January 14, 2010, 08:51:14 AM »
                        i tried this and i still cant get it to find files with zero bytes?   ???
                        -todd

                        antonhoward

                          Topic Starter


                          Greenhorn

                          Re: Batch or script to check for blank files - Please help
                          « Reply #13 on: January 14, 2010, 09:18:27 AM »
                          i tried this and i still cant get it to find files with zero bytes?   ???

                          todouble22

                           I hope this is not too obvious but you need to make sure the batch script is in the folder you are searching.
                          Regards

                          todouble22



                            Rookie

                            Thanked: 2
                            Re: Batch or script to check for blank files - Please help
                            « Reply #14 on: January 14, 2010, 10:08:30 AM »
                            todouble22

                             I hope this is not too obvious but you need to make sure the batch script is in the folder you are searching.
                            Regards
                            I was attempting it just through dos and not creating a batch for it just yet.  I wanted to see how it functioned before I created it.  but yes I am aware that the batch file needs to be in the folder that it is to be in the desired folder.  thank you.  i'll keep playing with it
                            -todd