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: File Type Distribution analysis: Batch File.

Discussion in 'Software Development' started by Ent, Aug 29, 2009.

Thread Status:
Not open for further replies.
Advertisement
  1. Ent

    Ent Trusted Advisor Thread Starter

    Joined:
    Apr 11, 2009
    Messages:
    5,467
    First Name:
    Josiah
    More as an alternative to twiddling my thumbs than anything else, I tried to produce a batch file that would count up the number and size of files in a folder, as well as the number and size of files with each extension. Hence it would say how many word documents I have and how much space they're taking up, etc.

    I've managed to get a vaguely working model, but the output formatting is terrible (I just wanted it to say X bytes in Y "Z" Files, none of that ugly dotted variable names) and I have failed to get it to equate .htm and .html, .jpg and .jpeg, etc.

    Code:
    @ECHO OFF
    REM THIS BATCH FILE ITTERATES THROUGH ALL THE SUBFOLDERS AND FILES IN A FOLDER (IDEALLY A DRIVE)
    REM IT TOTALS THE NUMBER OF FILES CONTAINED IN THAT FOLDER, AND THE SIZE OF THOSE FILES.
    REM IT ALSO TOTALS THE NUMBER AND SIZE OF FILES WITH EACH EXTENSION IT ENCOUNTERS.
    REM ALL INFORMATION IS THEN PRINTED TO THE SCREEN AND FTYPEDST.TXT
    
    IF EXIST FTypeDst.txt ERASE FTypeDst.txt
    ECHO. >FTypeDst.txt
    ATTRIB +A FTypeDst.txt
    
    ECHO SCANNING FILE TREE FOR FILE EXTENSION DISTRIBUTION
    ECHO Please Wait Paitently. . .
    
    (SET /A ALLNUMBER = 0) & (SET /A ALLSIZE = 0)
    
    REM cycle through the current directory and subs, Increment Allnumber, Allsize, define and increment Number of * files, size of * files.
    FOR /R %%F in (*) DO (
    SET /A ALLNUMBER += 1
    SET /A ALLSIZE += %%~ZF
    IF NOT DEFINED Number.of.%%~XF.files (SET /A Number.of.%%~XF.files = 0)
    IF NOT DEFINED Size.of.%%~XF.files (SET /A Size.of.%%~XF.files = 0)
    SET /A Number.of.%%~XF.files += 1
    SET /A .Size..of.%%~XF.files += %%~ZF
    ) 
    
    REM END CYCLE
    ECHO Scan FINISHED
    ECHO.
    ECHO.
    ECHO FILE EXTENSION DISTRIBUTION REPORT FOR %CD% & ECHO FILE TYPE DISTRIBUTION REPORT FOR %CD% >> FTypeDst.txt
    ECHO. & ECHO. >> FTypeDst.txt
    ECHO. & ECHO. >> FTypeDst.txt
    ECHO %ALLSIZE% BYTES IN %ALLNUMBER% FILES (*.*) & ECHO %ALLSIZE% BYTES IN %ALLNUMBER% FILES (*.*) >> FTypeDst.txt
    ECHO. & ECHO. >> FTypeDst.txt
    
    SET Number.of > EXTTEMP.tmp
    SET .Size..of >> EXTTEMP.tmp
    REM Sort SET based on extension name.
    SORT EXTTEMP.tmp /+10
    SORT EXTTEMP.tmp >> FTypeDst.txt /+10
    ERASE EXTTEMP.tmp
    
    ECHO. & ECHO. >> FTypeDst.txt
    PAUSE
    :EOF
    Has anyone any ideas about what I should have done better?
     
  2. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,783
    Posted via Mobile Device
    You would be better off using third party software that already does this. Plenty of them out there.
    I personally would check the extension of each file using the cmd extensions of a for loop. then do an if statement for each file type you want to keep track of. then use a call statement to branch to a function that adds up the file sizes and number of files.
     
  3. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    You have a typo in line 22:
    Code:
    IF NOT DEFINED Size.of.%%~XF.files (SET /A Size.of.%%~XF.files = 0)
    This should be
    Code:
    IF NOT DEFINED [COLOR=Red][B].[/B][/COLOR]Size.of.%%~XF.files (SET /A [COLOR=Red][B].[/B][/COLOR]Size.of.%%~XF.files = 0)
    These lines aren't really needed, as the variable will be created with the first += line.

    You are creating the FTypeDst.txt file in the same directory before the loop executes, so it will be included in the total for number of text files, but it's size will always be shown as three.
    This line Echo. >FTypeDst.txt creates the file with a single space, followed by a CR/LF, giving a size of 3 bytes for the file when the loop executes.
    The space is there because you have a space between the . and >.
    Anytime you use a redirection symbol, if there is a space in front of it, it will be output to the file.
    For a blank line you need to remove the space:
    Echo.>FTypeDst.txt
    This file will have a size of 2 for the CR/LF
    and you can remove the space on these lines as well:
    ECHO.& ECHO.>> FTypeDst.txt

    If you don't want the file included, create it after the loop is finished.

    If you run the file a 2nd time without closing the Command Prompt, the the previous totals for Number and Size will be the starting point.
    Example, if you have a folder with just one file that is 10 bytes, the first run will show 1 and 10, the 2nd 2 and 20, and so on.

    Add a SetLocal command at the start of the file to eliminate that.

    Files without an extension will be sorted as if the extension is files

    Because of the way Sort works, some extensions will have Size listed before Number, and some will have Number before Size.
    Example, 1 file with size of 1346 is listed like this:
    Code:
    Number.of..cmd.files=1
    .Size..of..cmd.files=1346
    If there are 2 files with a size of 1346 they will be listed like this:
    Code:
    .Size..of..cmd.files=1346
    Number.of..cmd.files=2
    Rather than echoing the same line to the screen then to the file, just echo it to the file, then use the Type command to display the file on the screen. This will also let you sort directly to an output file, which is faster than redirecting the output of the sort command, and you only need to do the sort once instead of twice.


    With a slight change of variable name, you can eliminate the use of Sort altogether, and just let Set do the sorting.
    Then a simple For loop can format the data any way you want, though since the number of digits for number of files and size will vary, the output won't be lined up very well without padding the numbers with spaces. Differences in the length of the extension will throw off the formatting unless you put the extension last:
    XXX bytes in YYY Files of type ".???"
    Also, a batch file uses 32 bit signed numbers, so numbers range from -2,147,483,648 to 2,147,483,647; If you add one to 2,147,483,647, it "wraps around" to -2147483648, so size (and possibly counts) could be off.

    If you want to lump .htm and .html or other types together, you would have to add them up after the loop finishes and clear the variable for the one you don't want displayed, or clear both and create a new variable for the combined extensions to indicate they are combined.

    Give this a whirl and see how it works for you. It doesn't include the output file in the totals, doesn't use Sort or any temp files, combines .htm and .html together (which takes 11 lines of code), and pads all numbers to a width of 10 characters. Files without an extension are listed first.
    Note that files that have an = sign in the extension will not be listed:
    Code:
    @ECHO OFF
    SetLocal EnableDelayedExpansion
    REM THIS BATCH FILE ITTERATES THROUGH ALL THE SUBFOLDERS AND FILES IN A FOLDER (IDEALLY A DRIVE)
    REM IT TOTALS THE NUMBER OF FILES CONTAINED IN THAT FOLDER, AND THE SIZE OF THOSE FILES.
    REM IT ALSO TOTALS THE NUMBER AND SIZE OF FILES WITH EACH EXTENSION IT ENCOUNTERS.
    REM ALL INFORMATION IS THEN PRINTED TO THE SCREEN AND FTYPEDST.TXT
    ECHO SCANNING FILE TREE FOR FILE EXTENSION DISTRIBUTION
    ECHO Please Wait Patiently. . .
    
    (SET /A ALLNUMBER = 0) & (SET /A ALLSIZE = 0)
    
    REM cycle through the current directory and subs, Increment Allnumber, Allsize, define and increment Number of * files, size of * files.
    FOR /R %%F in (*) DO (
    SET /A ALLNUMBER += 1
    SET /A ALLSIZE += %%~ZF
    SET /A .Type_."%%~XF"_Number += 1
    SET /A .Type_."%%~XF"_Size += %%~ZF
    ) 
    
    REM END CYCLE
    ECHO Scan FINISHED
    ECHO.
    ECHO.
    If NOT Defined .Type_..htm_Number Set .Type_..htm_Number=0 & Set .Type_..htm_Size=0
    If NOT Defined .Type_..html_Number Set .Type_..html_Number=0 & Set .Type_..html_Size=0
    Set /A _tmp_Number = .Type_..htm_Number + .Type_..html_Number
    Set /A _tmp_Size = .Type_..htm_Size + .Type_..html_Size
    If %_tmp_Number%==0 (Set _tmp_Number=)&(Set _tmp_Size=)
    Set .Type_..htm and html_Number=%_tmp_Number%
    Set .Type_..htm and html_Size=%_tmp_Size%
    Set .Type_..htm_Number=
    Set .Type_..htm_Size=
    Set .Type_..html_Number=
    Set .Type_..html_Size=
    >FTypeDst.txt ECHO.FILE TYPE DISTRIBUTION REPORT FOR %CD%
    ATTRIB +A FTypeDst.txt
    >>FTypeDst.txt ECHO.
    >>FTypeDst.txt ECHO.
    >>FTypeDst.txt ECHO.%ALLSIZE% BYTES IN %ALLNUMBER% FILES (*.*)
    >>FTypeDst.txt ECHO.
    For /F "Tokens=2-4 Delims=_=" %%I In ('SET .Type_') Do (
    Set _Type=%%I
    Set _Type=!_Type:..=.!
    Set _Pad=          %%K
    If /I %%J==Number Set _Ext%%I=!_Pad:~-10! Files with an extension of "!_Type!"
    If /I %%J==Size Set _Ext%%I=!_Pad:~-10! bytes in !_Ext%%I!
    )
    For /F "Tokens=2 Delims==" %%I In ('Set _Ext') Do Echo %%I>>FTypeDst.txt
    >>FTypeDst.txt ECHO.
    Type FTypeDst.txt
    PAUSE
     
  4. Ent

    Ent Trusted Advisor Thread Starter

    Joined:
    Apr 11, 2009
    Messages:
    5,467
    First Name:
    Josiah
    Wow. Thank you. That's exactly what I had in mind, but it was my first attempt with Batches and I certainly couldn't have pulled it off at that level. You certainly are good with these things aren't you?!?
     
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!

Loading...
Thread Status:
Not open for further replies.

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

  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