[Solved] Getting and setting a File's all attributes
-
I made an app (Windows only) with which you can create a backup of a folder/file locally or to a server. I'm done with the copy/restore process, but when it comes to restoring I have a small technical problem. If the file in originally hidden, then this is easy as you can call
Files.setAttribute(file.toPath(), "dos:hidden", true);
which will make the file hidden on client side after it is created/restored, but there are files likedesktop.ini
which aren't simply hidden but are system protected hidden and the above code will only give it the hidden status, and not system protected hidden. There is alsoFiles.getAttribute()
but as the name suggests it only returns the attribute value for a single attribute, and not all of them to easily be able to doFiles.setAttributes(file.toPath(), Files.getAttributes(file.toPath()));
. So my question is, is there a way to get a list will all the attributes a file has and then setting them in a more dynamically way and not having to callFiles.getAttribute();
/Files.setAttribute();
for each of them line by line? If unfortunately there isn't one, and you do have to set them line by line, can someone give me a list with all the attributes for Windows OS like there is here for example with DateTimeFormatter[^] for pattern symbols? -
I made an app (Windows only) with which you can create a backup of a folder/file locally or to a server. I'm done with the copy/restore process, but when it comes to restoring I have a small technical problem. If the file in originally hidden, then this is easy as you can call
Files.setAttribute(file.toPath(), "dos:hidden", true);
which will make the file hidden on client side after it is created/restored, but there are files likedesktop.ini
which aren't simply hidden but are system protected hidden and the above code will only give it the hidden status, and not system protected hidden. There is alsoFiles.getAttribute()
but as the name suggests it only returns the attribute value for a single attribute, and not all of them to easily be able to doFiles.setAttributes(file.toPath(), Files.getAttributes(file.toPath()));
. So my question is, is there a way to get a list will all the attributes a file has and then setting them in a more dynamically way and not having to callFiles.getAttribute();
/Files.setAttribute();
for each of them line by line? If unfortunately there isn't one, and you do have to set them line by line, can someone give me a list with all the attributes for Windows OS like there is here for example with DateTimeFormatter[^] for pattern symbols?I don't know how it was implemented in Java, however there is the API functions: [GetFileAttributesA function (fileapi.h) - Win32 apps | Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfileattributesa) [SetFileAttributesA function (fileapi.h) - Win32 apps | Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfileattributesa) And here is the list of attributes: [File Attribute Constants (WinNT.h) - Win32 apps | Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants)
-
I don't know how it was implemented in Java, however there is the API functions: [GetFileAttributesA function (fileapi.h) - Win32 apps | Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfileattributesa) [SetFileAttributesA function (fileapi.h) - Win32 apps | Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfileattributesa) And here is the list of attributes: [File Attribute Constants (WinNT.h) - Win32 apps | Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants)
This doesn't look all that hard to do in C++, which makes me believe that there might be a way to do it in Java as well. If it was locally only (on the same machine) then I could make a
dll
and copy the attribues that way as both the backup and restored files are on the same machine, but the app also has the Socket part and sending the C++ attributes might give a a headache when trying to crate the code, compared to just sending java objects. Even so, this is good info, thanks! -
I made an app (Windows only) with which you can create a backup of a folder/file locally or to a server. I'm done with the copy/restore process, but when it comes to restoring I have a small technical problem. If the file in originally hidden, then this is easy as you can call
Files.setAttribute(file.toPath(), "dos:hidden", true);
which will make the file hidden on client side after it is created/restored, but there are files likedesktop.ini
which aren't simply hidden but are system protected hidden and the above code will only give it the hidden status, and not system protected hidden. There is alsoFiles.getAttribute()
but as the name suggests it only returns the attribute value for a single attribute, and not all of them to easily be able to doFiles.setAttributes(file.toPath(), Files.getAttributes(file.toPath()));
. So my question is, is there a way to get a list will all the attributes a file has and then setting them in a more dynamically way and not having to callFiles.getAttribute();
/Files.setAttribute();
for each of them line by line? If unfortunately there isn't one, and you do have to set them line by line, can someone give me a list with all the attributes for Windows OS like there is here for example with DateTimeFormatter[^] for pattern symbols?I found a solution for the information when you open the Properties of a file, from the General tab, as for Details tab, that info it is copied with the file. Example for General tab:
DosFileAttributes dos = Files.readAttributes(original.toPath(), DosFileAttributes.class);
DosFileAttributeView dosView = Files.getFileAttributeView(copy.toPath(), DosFileAttributeView.class);
dosView.setArchive(dos.isArchive());
dosView.setHidden(dos.isHidden());
dosView.setReadOnly(dos.isReadOnly());
dosView.setSystem(dos.isSystem());
dosView.setTimes(dos.lastModifiedTime(), dos.lastAccessTime(), dos.creationTime());