Thread overview
Check whether a file is empty.
Dec 08
Vino
Dec 08
Kagamin
Dec 08
FreeSlave
Dec 08
vino
Dec 09
FreeSlave
December 08
Hi All,

  Request your help on how to check whether a given file is empty, I tried the getSize from std.file but no luck as in windows 7 is the file is empty the size of the file is 0 bytes but in Windows 2003 if the file is empty the size of the file show as 2 bytes.


From,
Vino.B
December 08
Other functions that can be used for this are GetFileInformationByHandle, GetFileSizeEx, SetFilePointerEx or File.size in phobos.
December 08
On Friday, 8 December 2017 at 09:40:18 UTC, Vino wrote:
> Hi All,
>
>   Request your help on how to check whether a given file is empty, I tried the getSize from std.file but no luck as in windows 7 is the file is empty the size of the file is 0 bytes but in Windows 2003 if the file is empty the size of the file show as 2 bytes.
>
>
> From,
> Vino.B

Was it the same file on Windows 7 and Windows 2003?
Maybe the file on Windows 2003 had a byte order mark or carriage return + newline characters.
December 08
On Friday, 8 December 2017 at 12:25:19 UTC, FreeSlave wrote:
> On Friday, 8 December 2017 at 09:40:18 UTC, Vino wrote:
>> Hi All,
>>
>>   Request your help on how to check whether a given file is empty, I tried the getSize from std.file but no luck as in windows 7 is the file is empty the size of the file is 0 bytes but in Windows 2003 if the file is empty the size of the file show as 2 bytes.
>>
>>
>> From,
>> Vino.B
>
> Was it the same file on Windows 7 and Windows 2003?
> Maybe the file on Windows 2003 had a byte order mark or carriage return + newline characters.

Hi,

 The code is same just copy pasted the code form Windows 7 into Windows 2003 and executed, in Windows 7 the log file is of size 0 where as in windows 2003 the log file is of size 2 byte where the log file in both the server is empty.

From,
Vino.B
December 09
On Friday, 8 December 2017 at 19:13:20 UTC, vino wrote:
> Hi,
>
>  The code is same just copy pasted the code form Windows 7 into Windows 2003 and executed, in Windows 7 the log file is of size 0 where as in windows 2003 the log file is of size 2 byte where the log file in both the server is empty.
>
> From,
> Vino.B

It certainly sounds like some kind of encoding issue.

I guess you don't have much choice other than to read the contents of the file, and determine what it actually contains.

Assuming these log files of yours are 'ascii text' files, then if the file is less than say .. 5 bytes.. you could check if it *only* contained *non-printable* characters, in which case it's 'likely' an empty file.

something very silly, like this, might do it ;-)

// -------

bool isFileLikelyEmpty(string filename)
{

    import std.exception, std.file, std.ascii, std.conv;

    File f = filename;

    enforce(f.size < 5,
        "This file is not likely to be empty." ~
        " No point in continuing."); // 5 seems reasonable cutoff ;-)

    bool result = false;
    int charCount = 0;
    auto str = readText(filename);

    foreach(c; str)
    {
        // https://dlang.org/phobos/std_ascii.html#isPrintable
        if( !isPrintable(c) )
            charCount++;
    }

    if(charCount == str.length)
        result = true; // file is likely empty, as all characters are non-printable.

    return result;
}

// ---------

December 09
On Friday, 8 December 2017 at 19:13:20 UTC, vino wrote:
>
> Hi,
>
>  The code is same just copy pasted the code form Windows 7 into Windows 2003 and executed, in Windows 7 the log file is of size 0 where as in windows 2003 the log file is of size 2 byte where the log file in both the server is empty.
>
> From,
> Vino.B

What do you mean exactly by empty log file? If the file looks blank in notepad it does not mean it's empty. What are those 2 bytes?
Which program does create this file? It may work differently depending on the system or different versions are installed.