Finding filesizes

Yesterday, I was tasked w/ finding and identify the filesize of some files located in some directory. SQL doesn’t really have a function to do this and neither does a straight up DOS’ dir command.

Forunately, the Internet is a big place and someone wrote a one line syntax to do what I needed.

@echo off
for %%a in (E:\path\to\files\*.*) do (if %%~za GEQ 0 (echo %%~za %%a) ) )

I took this code, put it into a DOS .bat file, output the call to a .txt file, and then finally bulk inserting it into a table for SQL to crunch thru. With over 300GB of files in my directory, it took roughly 18seconds to complete the entire process.

I made some changes to what files were being stored in my directory and cut the process down to less than a second.

set @cmdListing = '\\path\to\batch\file\filesize_listing.bat > \\path\to\output_listing.txt'

exec xp_cmdshell @cmdListing

bulk insert dbname..fileSizesTable from 'E:\path\to\output_file\output_listing.txt'

After this, it was basically a select statement to find out if my kb column in the fileSizesTable column contains any 0.

Recent Entries

One Response to “Finding filesizes”

  1. Obe Says:

    well … apparently I was just shown today that you can use a maybe about 5 lines of code to find the 0kb file using “select * from @tbl where columnA like ‘% 0 %’ .”

    you would first have to run ‘dir /c \\path\to\files’, store this into a temp table (@tbl).

    I was going to initially use this but I thought that the select statement will find all leading ‘0′.

Leave a Reply

You must be logged in to post a comment.