Sunday, June 26, 2011

To create a Directory Path (X++)

Hi,

To create a particular Dicetory path using X++, follow below code steps:

protected boolean createDirectoryPath(FilePath _path)
{
    int ptr;
    ;
    try
    {
        //First check for the passed path exits or not
        if (winAPI::folderExists(_path))
        {
            return true;
        }

        _path = strLRTrim(_path);

        if (substr(_path,strlen(_path),1) != '\\') //Adding backslash gives a more simple while-loop!
        {
            _path += '\\';
        }

        //MKU: enables creation of UNC paths, by detecting which folder already exists (backwards).
        ptr = strLen(_path);
        ptr = strfind(_path, '\\', ptr, -ptr);
        while(!WinAPI::folderExists(substr(_path,1,ptr)))
        {
            ptr = strfind(_path, '\\', ptr -1 , -ptr);
        }
        // ptr now points to last existing folder in the path.
        //MKU: end

        while (ptr)
        {
            WinAPI::createDirectory(substr(_path, 1, ptr));
            if (!WinAPI::folderExists(substr(_path, 1, ptr)))
            {
                break;
            }
            ptr = strfind(_path, '\\', ptr + 1, strlen(_path));
        }

        return WinAPI::folderExists(_path);
    }
    catch
    {
        info('@WML75'); //Item image is not created.
        return false;
    }
}

Thanks,
/Ashlesh

No comments:

Post a Comment