Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Filename Too Long Example

Part of StandardLibraryCategory

Description

This program generates a comma-separated-values (CSV) file with files with filenames that are too long for me to burn them onto a DVD. Apparently, there's some kind of 108-character limit for filenames on a DVD. There's also a limit on CD filenames, but I don't know if it's the same.

When I try to create a DVD on my computer, the DVD-creating software complains if filenames are too long. The program gives me list of the files the offend it, but it's in a little window that I can't resize and it won't let me copy-and-paste the data from it. So now, I can have a friendlier list of files to shorten.

Example

import std.file; /* for listdir */
import std.path; /* for getBaseName */
import std.stdio; /* for writef */
import std.stream; /* for File */
import std.string; /* for toString */


const char[] pgmName = "LinesOfCodeExample";
const int max_number_of_characters = 108;

char[] outputData;


int main(char[][] args)
{
    char[] pathStr;
    char[] outputFileStr;

    if(args.length < 2)
    {
        writefln("Usage: " ~ pgmName ~ " input_path output_file");
        return 0;
    }
    else
    {
        pathStr = args[1];
        outputFileStr = args[2];
    }

    outputData = `"Path","Filename","PathAndFilename",` ~
      `"Length"` ~ \r\n;

    int n = listdirfn(pathStr);

    writef("Number checked: %s\n", n);
    std.file.write(outputFileStr, cast(byte[])outputData);

    return 0;
}


int listdirfn(char[] pathname)
{
    int n;
    bool callback(DirEntry* de)
    {
        if (de.isdir)
        {
            writefln("Checking %s", de.name);
            std.file.listdir(de.name, & callback);
        }
        else
        {   
            if (getBaseName(de.name).length > max_number_of_characters)
            {
                n++;

                outputData ~=  `"` ~ getDirName(de.name) ~ `","` ~ 
                  getBaseName(de.name) ~ `","` ~ 
                  de.name ~ `","` ~ 
                  toString(de.name.length) ~ `"` ~ \r\n;
            }
        }
        return true; // continue
    }

    std.file.listdir(pathname, & callback);
    return n;
}

Sample batch file

@echo off
dmd FilenameTooLongExample.d
FilenameTooLongExample.exe D:\dwnlds D:\examples\fn_too_long.csv
pause

Tested Version

Tested by compiling with DMD 0.174 and running on Windows 2000.

Source

Partially based on digitalmars.D/33287 (listdir example).