none
Batch Script to retrieve zero byte files

    Question

  • Hi

    How to retrieve zero byte files and copy into one folder using Batch Script.

    Friday, May 14, 2010 11:41 AM

Answers

  • Are you running it at a command prompt and providing an input location as a command line input?  Without the input (say when double clicked in explorer), it will just search the folder in which the procedure is located (and any subfolders, if present).  To search the whole drive, either place the procedure at the root of the drive or run it from the command prompt with "C:\" (or D:\ or X:\, etc.) as the command line argument. 

    Wait - I think I know the problem!  The search of a whole drive using the approach in the batch can take a very, very long time.  So it may appear that nothing is happening.  Here is another approach that provides output as it goes, so that it doesn't appear to be hung-up.

     @echo off
      set out="C:\somefolder\somewhere\zerofiles.txt"
      for /r "%~1." %%A in (*.*) do if %%~zA EQU 0 echo %%~fA >> %out%

    Called with a command line argument of "\" will cause the search to be referenced to the root of the current drive.  Otherwise, it still searches from the current active folder down.  It still takes as long.  However, if the procedure is stopped short, the output file will contain the results up to the point the procedure was aborted.


    Tom Lavedas
    Friday, May 14, 2010 3:43 PM
    Moderator

All replies

  • That's not much of a description of what you hope to accomplish.  For example, do you want to search a particular folder or a whole drive?  What about subfolders?  If subfolders are to be searched, what is to be done with duplicate named files, if any are present?

    A more complete description of the intended outcome might be useful in formulating a response (see "How to ask questions in a technical forum" http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/22207f93-8e60-4b4b-8ccf-cf107e5fb3e7).  But, going on the fundamentals of your question, here is a starting point ...

     @echo off
      set destination="C:\somefolder\somewhere"
      for /f "tokens=4*" %%A in (
       'dir "%~1" /s ^| find " 0 "') do (
          copy "%%~fB" %destination% > nul
       )

    An optional starting location and/or wildcard mask for the search can be provided as a command line input.  As configured, subfolders are searched and duplicate file names are overwritten.  Note that the original location of the file cannot be determined from the resultant copy, if it is not located in the root folder of the search.  But, without a better understanding of what is to be accomplished ...


    Tom Lavedas
    Friday, May 14, 2010 12:27 PM
    Moderator
  • You can use robocopy to do this very easy: you can specify the max filesize for the files to be copied in the command line (/max=0). Robocopy replaces the deprecated xcopy command in Vista/2008 and later. For previous OS versions, robocopy is part of the resource kit since nt4.

    When using xcopy or copy  to accomplisch this for a a folder structure, please be aware that folder objects report a size of 0 bytes... you should be aware of this and test you scripts in a safe location first!

     


    MCSA/MCTS/MCP
    Friday, May 14, 2010 12:49 PM
  • Hi Tom Lavedas

    Thanks for the reply. this script is useful.

    I want the script like to list it out the zero byte files path and copy into a new text file the output. is it possible?

    Friday, May 14, 2010 1:05 PM
  • Yes, that makes more sense.  Here is one that will do that ...

     @echo off
      set out="C:\somefolder\somewhere\zerofiles.txt"
      (for /f "tokens=*" %%A in ('dir "%~1" /a-d /b /s') do (
         if %%~zA EQU 0 echo %%A))  > %out%

    It searches subfolders and lists the full path for the zero length files found.


    Tom Lavedas
    Friday, May 14, 2010 1:59 PM
    Moderator
  • I don't understand your assertion that this works "only [with the] root folder".  It is designed to perform a recursive search through the subfolders.  The /S in the DIR part of the FOR statement does just that.  Why do you say that it does not recurse?  Is that based on a trial?  It worked perfectly well when I tested it with zero byte files in subfolders.

    Tom Lavedas
    Friday, May 14, 2010 2:19 PM
    Moderator
  • When i run the script, its not going to search subfolders and not displaying in the file output.

    Friday, May 14, 2010 2:22 PM
  • Tom's program worked great for me. However it fails if you do not have permissions in any folder. For example, if you run this from C: on a Vista or newer computer, you do not have permission in all folders.

    Richard Mueller


    MVP ADSI
    Friday, May 14, 2010 2:41 PM
    Moderator
  • Thanks all for your replies.

    I run the script which Tom has Provided. i have full access to all folders. i dont know why it is failing for me. search is not working for subfolders.

    Operating system is WindowsXP

    Friday, May 14, 2010 2:48 PM
  • I just tried on an XP computer and it worked fine. However, it took a long time when I ran the program from the root of the C: drive.

    Richard Mueller


    MVP ADSI
    Friday, May 14, 2010 3:15 PM
    Moderator
  • Are you running it at a command prompt and providing an input location as a command line input?  Without the input (say when double clicked in explorer), it will just search the folder in which the procedure is located (and any subfolders, if present).  To search the whole drive, either place the procedure at the root of the drive or run it from the command prompt with "C:\" (or D:\ or X:\, etc.) as the command line argument. 

    Wait - I think I know the problem!  The search of a whole drive using the approach in the batch can take a very, very long time.  So it may appear that nothing is happening.  Here is another approach that provides output as it goes, so that it doesn't appear to be hung-up.

     @echo off
      set out="C:\somefolder\somewhere\zerofiles.txt"
      for /r "%~1." %%A in (*.*) do if %%~zA EQU 0 echo %%~fA >> %out%

    Called with a command line argument of "\" will cause the search to be referenced to the root of the current drive.  Otherwise, it still searches from the current active folder down.  It still takes as long.  However, if the procedure is stopped short, the output file will contain the results up to the point the procedure was aborted.


    Tom Lavedas
    Friday, May 14, 2010 3:43 PM
    Moderator
  • Great, its worked.  Thanks alot for your help Tom.

    Sunday, May 16, 2010 7:01 AM
  • same thread I am reopening again.

    Is there any possibilities to exclude certain file extensions while retrieving Zero byte size files. I want to achieve this using Batch script.

    Already in above thread, Tom provided one script. its worked for me. now In the same script I would like to include this "Exclude certain file extensions type". Please help me out.

    Thursday, May 27, 2010 2:16 PM
  • It might be helpful if you were to explain what you're trying to accomplish and why.

    Bill

    Thursday, May 27, 2010 2:31 PM
    Moderator
  • Not withstanding your good question, my mind immediately jumped to an answer that seems to meet the questioner's criteria (without understanding the why).  That is, something like this ...

    @echo off
      set out="C:\somefolder\somewhere\zerofiles.txt"
      set exclude=".ext$ .abc$ .xyz$ .etc$"
      for /r "%~1." %%A in (*.*) do (
        if %%~zA EQU 0 echo %%~fA|findstr /i /v %exclude% >> %out%
      )


    Tom Lavedas
    Thursday, May 27, 2010 2:55 PM
    Moderator
  • Hi Tom

    thanks for your help.

    I ran this script in command prompt. but the file type didnt excluded while retrieving zerobyte size files. In Output file still i can see the file type.

    I given value as: set exclude=".vsscc$"

    @echo off
      set out="E:\inetpub\zerofiles.txt"
      set exclude=".vsscc$"
      for /r "%~1." %%A in (*.*) do (
        if %%~zA EQU 0 echo %%~fA|findstr /i /v %exclude% >> %out%
      )

    Is something i am missing? Please help me.

    Friday, May 28, 2010 3:40 PM
  • If the file named in the SET OUT line already exists when you run the procedure, say from a previous test, then the later results are appended to the bottom of the file.  That might be causing a confusion, because your code worked perfectly for me just now.

    Try this modified version to see if it fixes the problem by emptying the "out" file before the search ...

    @echo off
      set out="E:\inetpub\zerofiles.txt"
      set exclude=".vsscc$"> %out%
      for /r "%~1." %%A in (*.*) do (
        if %%~zA EQU 0 echo %%~fA|findstr /i /v %exclude% >> %out%
      )


    Tom Lavedas
    Friday, May 28, 2010 4:18 PM
    Moderator
  • Hi Tom

    I really appreciate your help. Thank you very much.

    Monday, May 31, 2010 9:43 AM
  • Tom,

     

    I am very new at this so here is my problem:

    I need to identify empty (zero byte) csv files that are imported daily.  the above scripting, I am guessing is used in a bat file to locate the zero byte files in a folder.

    Once they are identified, how can they be renamed (example, *.zero  so I can delete them automatically?

    If these files are in c:/import/csvs,,,, the script above is:

     @echo off
      set out="C:\import\csvs\*.csv"   ### this tells script to look for empty csv files??
      for /r "%~1." %%A in (*.*) do if %%~zA EQU 0 echo %%~fA >> %out%

     

    thanks in advance for any help!

     

     

     

     

     

     

     

     

     

    /

    Monday, June 06, 2011 11:55 PM
  • Attaching a new question to an old thread that is marked as "answered" might delay or prevent you from getting a response.  It is better to start a new thread that references the old one as background for your new question.

    Concerning deleting zero byte files, it can all be done by replacing the ECHO statement with the one you want, as in ...

    @echo off
      for /r "%~1." %%A in (*.*) do if %%~zA EQU 0 del "%%~fA" > nul

    where the folder to search is provided as the single command line input.  This means that the folder to be searched can be dragged and dropped onto the batch file in explorer or onto a shortcut to the batch file on the desktop.

    If you really want to rename the file, rather than delete it, use this ...

    @echo off
      for /r "%~1." %%A in (*.*) do if %%~zA EQU 0 ren "%%~fA" "%%~nA.zero"


    Tom Lavedas
    Tuesday, June 07, 2011 12:46 PM
    Moderator