B4J Question [SOLVED] File Attributes...

OliverA

Expert
Licensed User
Longtime User
is there any b4j library

Was able to get this to work with some inline Java. I tried to do it with pure JavaObject, but I failed with a method not matched error. I left the JavaObject code there (I just commented it out) in case someone else figures it out.

Usage example:
B4X:
SetDOSAttribute("C:\temp\test.cmd","a",True)
SetDOSAttribute("C:\temp\test.cmd","r",False)
SetDOSAttribute("C:\temp\test.cmd","h",False)
SetDOSAttribute("C:\temp\test.cmd","s",False)

Code:
B4X:
Sub SetDOSAttribute(aFile As String, attribute As String, setTo As Boolean) As Boolean
   If File.Exists(File.GetFileParent(aFile),File.GetName(aFile)) = False Then
       Log($"File ${aFile} does not exist"$)
       Return False
   End If
   Dim retVal As Boolean = True
   Dim fileAttr As String
   Select attribute.ToLowerCase
       Case "h"
           fileAttr = "dos:hidden"
       Case "r"
           fileAttr = "dos:readonly"
       Case "s"
           fileAttr = "dos:system"
       Case "a"
           fileAttr = "dos:archive"
       Case Else
           retVal = False
   End Select
   If retVal = True Then
       Dim nativeMe As JavaObject
       nativeMe = Me
       nativeMe.RunMethod("setDOSFileAttribute", Array(aFile, fileAttr, setTo))
       'Pure JavaObject -> could not get it to work
       'Dim jo As JavaObject
       'Dim aURI As JavaObject
       'Dim aPath As JavaObject
       'aURI = jo.InitializeStatic("java.net.URI").RunMethod("create",Array(File.GetUri(File.GetFileParent(aFile),File.GetName(aFile))))
       'aPath = jo.InitializeStatic("java.nio.file.Paths").RunMethod("get", Array(aURI))
       'Dim aEnum As JavaObject
       'aEnum = jo.InitializeStatic("java.nio.file.LinkOption").RunMethod("valueOf",Array("NOFOLLOW_LINKS"))
       'Never could get the below to match
       'jo.InitializeStatic("java.nio.file.Files").RunMethod("setAttribute", Array(aPath, fileAttr, setTo, aEnum))
   End If
   Return retVal
End Sub

#If JAVA
import java.io.IOException;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

//Note: Remove the word static when used in a class module
public static void setDOSFileAttribute(String aFile, String attribute, boolean flag) throws IOException {
   Path aPath = Paths.get(aFile);
   Files.setAttribute(aPath, attribute, flag);
}
#End If
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
@Roycefer ...that will be the BEST ! ...

Any Attribute like these (for unix)... :

  • a: append only
  • c: compressed
  • d: no dump
  • e: extent format
  • i: immutable
  • j: data journalling
  • s: secure deletion
  • t: no tail-merging
  • u: undeletable
  • A: no atime updates
  • C: no copy on write
  • D: synchronous directory updates
  • S: synchronous updates
  • T: top of directory hierarchy
and any for windows (That needed for sure!)


+ IF you can do a special remove folder command/routine

found this but a low level will help more:

B4X:
Sub DeleteFolderRecursive(Folder As String)
   For Each f As String In File.ListFiles(Folder)
     If File.IsDirectory(Folder, f) Then
       DeleteFolderRecursive (File.Combine(Folder, f))
     End If
     File.Delete(Folder, f)
   Next
End Sub

Can you manipulate Permissions too ?

Am i asking too much ???

(SORRY)

*** Thanks in advance ***

ps: this is something like I WISH :)
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Yes, it looks like all those wishes are feasible. I'll try to get them done by the weekend. Keep an eye on the jFileWatcher thread because that is where I will post.
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
I didn't wait until the weekend. Check the jFileWatcher thread for the latest version (1.3). Test it. If it has any problems, post those problems in that thread and I'll work on them.
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
@Roycefer...

Congratulations (tried at windows works perfect - don;t know at other systems - will try tomorrow)

i have and idea...

can you add this: KillInFolderRecursive...
the difference will be:

remove any attribute -r -h -s -a / change to system owner... for any folder and any file too...
and Delete every folder and file in selected folder... except the selected folder...

Can you make them work like functions that will return error number... like those or something else

0 - success
1 - not found
2 - some files deleted / some not...
3 - files in use
4 - something else...
5...
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
I don't understand. You want to remove attributes from files and folders that you will delete in the next CPU cycle?
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
@Roycefer ... if delete bypass attributes... it's ok :) (i ve tested and and i see that your deletion not checking if it is readonly)

if it is possible....

1. delete everything in folder - but not the same folder...
2. and return error number if there is...

Thanks again..
 
Upvote 0
Top