[SOLVED] Checking if a folder is read only in a TShellTreeView lazarus / 31-LCL ---------------------------------------------------------------------- petevick · 2024-01-17 17:53 I've cobbeled the following code from google searches, but it doesnt catch known read only folders, it always returns False. Can anyone see what might be wrong..... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function CheckIfFolderIsReadOnly(SelectedNode: TTreeNode): Boolean;var  FolderPath: String;  FolderInfo: TSearchRec;begin  Result:= True;  // Get the selected node//  SelectedNode := ShellTreeView1.Selected;  if Assigned(SelectedNode) then  begin    // Get the folder path from the selected node    FolderPath := Browser.myTreeView.Path;    // Check if the folder exists    if DirectoryExists(FolderPath) then    begin    // Check if the folder is read-only      if FindFirst(FolderPath, faDirectory, FolderInfo) = 0 then      begin        if (FolderInfo.Attr and faReadOnly) = faReadOnly then         Result:= True        else         Result:= False;;        FindClose(FolderInfo);      end;    end;  end;end; ---------------------------------------------------------------------- KodeZwerg · 2024-01-17 18:48 Your check is wrong. Here is a correct version: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses SysUtils; function IsDirectoryReadOnly(const DirName: string): Boolean;var SR: TSearchRec;begin if FindFirst(DirName, faAnyFile, SR) = 0 then begin Result := (SR.Attr and faReadOnly) <> 0; FindClose(SR); end else Result := False;end; This here might help but I do not recommend that approach. It checks if its readonly by creating a test file, on success it aint readonly. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses SysUtils; function IsDirectoryReadOnly(const ADirName: string): Boolean;var TempFile: string;begin TempFile := IncludeTrailingPathDelimiter(ADirName) + 'tempfile'; try   AssignFile(TempFile);   Rewrite(TempFile);   CloseFile(TempFile);   DeleteFile(TempFile);   Result := False; except   Result := True; end;end; ---------------------------------------------------------------------- petevick · 2024-01-18 07:32 Quote from: KodeZwerg on January 17, 2024, 07:48:33 pmYour check is wrong. Here is a correct version: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses SysUtils; function IsDirectoryReadOnly(const DirName: string): Boolean;var SR: TSearchRec;begin if FindFirst(DirName, faAnyFile, SR) = 0 then begin Result := (SR.Attr and faReadOnly) <> 0; FindClose(SR); end else Result := False;end;Thanks for the help KodeZwerg. I'm getting a little bit closer I think. A problem with my original code was that Browser.myTreeView.Path has a delimiter at the end. The function is now passed Browser.myTreeView.Path and the last delimiter is removed. But it's still not giving consistent correct results, known read only folders are still returning false. The only folder that returns True is the root folder (in Linux) where obviosly the Node.Text value = ''. The FolderInfo.Attr data seems to be ok except I don't know what the Attr = 48; value indicates for a read only folder. Revised function below based on your code above.... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function CheckIfFolderIsReadOnly(FolderPath: String): Boolean;var  FolderInfo: TSearchRec;  SelectedNode: TTreeNode;begin  // Get the selected node  SelectedNode := Browser.myTreeView.Selected;  if Assigned(SelectedNode) then  begin    // remove last delimiter     LeftStr(FolderPath, (length(FolderPath)-1));    // Check if the folder exists    if DirectoryExists(FolderPath) then    begin      // Check if the folder is read-only      if FindFirst(FolderPath, faAnyFile, FolderInfo) = 0 then      begin        Result:= (FolderInfo.Attr and faReadOnly) <> 0;        FindClose(FolderInfo);      end else        Result:= False;    end;  end;end; ---------------------------------------------------------------------- Zvoni · 2024-01-18 09:11 Quote from: petevick on January 18, 2024, 08:32:24 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function CheckIfFolderIsReadOnly(FolderPath: String): Boolean;var  FolderInfo: TSearchRec;  SelectedNode: TTreeNode;begin  // Get the selected node  SelectedNode := Browser.myTreeView.Selected;  if Assigned(SelectedNode) then  begin    // remove last delimiter     LeftStr(FolderPath, (length(FolderPath)-1));    // Check if the folder exists    if DirectoryExists(FolderPath) then    begin      // Check if the folder is read-only      if FindFirst(FolderPath, faAnyFile, FolderInfo) = 0 then      begin        Result:= (FolderInfo.Attr and faReadOnly) <> 0;        FindClose(FolderInfo);      end else        Result:= False;    end;  end;end; This looks weird to me Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Result:= (FolderInfo.Attr and faReadOnly) <> 0;  Would have expected Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Result:= (FolderInfo.Attr and faReadOnly) = faReadOnly; ---------------------------------------------------------------------- petevick · 2024-01-18 09:23 Well after all that I came across one of my own posts querying the DirectoryIsWritable() function which as leads to the my revised and simpler function that works (although I do have to try it in Windows yet) I switch the boolean result so it matches TShellTreeView.ReadOnly property...... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function CheckIfFolderIsReadOnly(FolderPath: String): Boolean;var  SelectedNode: TTreeNode;begin  // Get the selected node  SelectedNode := Browser.myTreeView.Selected;  if Assigned(SelectedNode) then  begin    if DirectoryExists(FolderPath) then    begin      // Check if the folder is read-only      Result:= DirectoryIsWritable(FolderPath);      Result:= not Result;    end;  end;end; ---------------------------------------------------------------------- wp · 2024-01-18 09:25 Quote from: Zvoni on January 18, 2024, 10:11:29 amThis looks weird to me Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Result:= (FolderInfo.Attr and faReadOnly) <> 0;  Would have expected Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Result:= (FolderInfo.Attr and faReadOnly) = faReadOnly;Why? faReadOnly represents a single bit only (faReadOnly   = $00000001). Therefore both statements are correct. ---------------------------------------------------------------------- petevick · 2024-01-18 09:26 Quote from: Zvoni on January 18, 2024, 10:11:29 am.......... Would have expected Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Result:= (FolderInfo.Attr and faReadOnly) = faReadOnly;That's what I had orginally which also didn't work. ---------------------------------------------------------------------- Zvoni · 2024-01-18 09:40 OK, noticed something "weird" (on Windows): I declared a Test-Folder to be readonly. OK. BUT..... This Folder inherits Attributes from its Parent-Folder, and that one removed the ReadOnly-Flag when i was reading it again..... ---------------------------------------------------------------------- KodeZwerg · 2024-01-18 10:01 I am unsure about the problems you encounter but specific on windows i would do it in a similar way Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var attr: Integer;begin attr := FileGetAttr('C:\SomeDirectory'); if (attr and faReadOnly) <> 0 thenAnd I always just test if a bit is included (<>0) ---------------------------------------------------------------------- petevick · 2024-01-18 11:36 Tested in Windows and works correctly. Thanks to all for the help