当前位置:网站首页>Use file and directory properties and properties
Use file and directory properties and properties
2022-07-05 18:47:00 【User 7741497】
Use file and directory properties and properties
%Library.File Class also provides many class methods , You can use these methods to get information about files and directories , Or view or set their properties and properties .
Check whether the files and directories exist
To determine whether a given file exists , Please use Existes() Method and specify the file name as the parameter . for example :
DHC-APP>write ##class(%File).Exists("e:\temp\")
1 Again , To find out whether a given directory exists , Please use DirectoryExists() Method , And specify the directory as the parameter . for example :
DHC-APP>write ##class(%File).DirectoryExists("e:\temp")
1 As mentioned earlier , These methods are in Unix Treat file or directory names as case sensitive , But in Windows Upper case insensitive . Besides , If you specify part of the file name or directory name , Then the file or directory referenced by this method is relative to the directory of the default global database containing the namespace being used . for example :
DHC-APP>write ##class(%File).Exists("cache.dat")
1View and set file and directory permissions
%Library.File Class provides many class methods , You can use these methods to view or set the permissions of files or directories .
Check whether the file or directory is read-only or writable
Given the file or directory name , If the file or directory is read-only ,ReadOnly() Method returns 1, Otherwise return to 0:
DHC-APP>write ##class(%File).ReadOnly("export.xml")
1
DHC-APP>write ##class(%File).ReadOnly("E:\temp")
0 Again , Given a file or directory name , If the file or directory can be written , be Writeable() Method returns 1, Otherwise return to 0:
DHC-APP>write ##class(%File).Writeable("export.xml")
0
DHC-APP>write ##class(%File).Writeable("e:\temp")
1Make the file or directory read-only or writable ( window )
To make Windows Files or directories on become read-only , Please use SetReadOnly() Method , This method returns a Boolean value indicating success or failure . This method takes three parameters , The second parameter is in Windows Is omitted from . The first parameter is the name of the file or directory . The third parameter is the output parameter . If a negative , It contains the error code returned by the operating system , In case the method fails .
In the following example , Yes SetReadOnly() The call to successfully put the file "e:\temp\config.txt Change to read-only .
DHC-APP>write ##class(%File).ReadOnly("e:\temp\config.txt")
0
DHC-APP>write ##class(%File).SetReadOnly("e:\temp\config.txt",,.return)
1
DHC-APP>write ##class(%File).ReadOnly("e:\temp\config.txt") In the following example , Yes SetReadOnly() Call of failed ,Windows The system error code is 5, It means “ Access denied .”
DHC-APP>write ##class(%File).SetReadOnly("C:\",,.return)
0
DHC-APP>w return
-5 To make Windows Files or directories on can be written , Please use SetWriteable() Method . This method uses the same three parameters , The second parameter is in Windows Is omitted again .
DHC-APP>write ##class(%File).Writeable("e:\temp\config.txt")
0
DHC-APP>write ##class(%File).SetWriteable("e:\temp\config.txt",,.return)
1
DHC-APP>write ##class(%File).Writeable("e:\temp\config.txt")
1Make the file or directory read-only or writable (Unix)
stay Unix On , You can also use SetReadOnly() and SetWriteable() Method , But due to the existence of the second parameter , Their behavior is somewhat different .
however , stay Unix in , For the owner 、 Groups and users specify different permissions . To better control file and directory permissions , See the section viewing or setting file and directory properties .
View and set file and directory properties
To view or set the properties of a file or directory at a more detailed level , Please use %Library.File Of Attributes() and SetAttributes() Method . File attributes are represented by a sequence of bits collectively expressed as integers . The meaning of each bit depends on the underlying operating system .
View file and directory properties
%Library.File Of Attributes() Method requires a file name or directory name as a parameter , And return the attribute bit sequence represented by an integer .
The following example is in Windows Running on the system :
DHC-APP>write ##class(%File).Attributes("cache.dat")
32
DHC-APP>write ##class(%File).Attributes("e:\temp")
16
DHC-APP>write ##class(%File).Attributes("secret.zip")
35
DHC-APP>write ##class(%File).Attributes("e:\temp\config.txt")
32 In the first example ,32 Express cache.dat It's an archive file . In the second example ,16 Express C:\temp Is a directory . In the third example , More than one bit is set ,35 Express secret.zip It's hidden (2) And read only (1) The archive (32). Add 32 + 2 + 1 = 35.
The following example is in Unix Running on the system :
write ##class(%File).Attributes("/home")
16877 In this case ,16877 Express /home Is a directory (16384), The owner owns the read (256)、 write in (128) And execution (64) jurisdiction ; Read (32) And execution (8) Group permission ; And read for others (4) And execution (1) jurisdiction . add 16384+256+128+64+32+8+4+1 = 16877.
Set file and directory properties
contrary ,SetAttributes() Method to set the properties of a file or directory , And return a Boolean value to indicate success or failure . This method requires three parameters . The first parameter is the name of the file or directory . The second parameter is an integer , Indicates that the file or directory has the required attributes . The third parameter is the output parameter . If a negative , It contains the error code returned by the operating system , In case the method fails .
The following example is in Windows On , By setting 1 position , Make the document e:\temp\config.txt Become a read-only file :
DHC-APP>write ##class(%File).Attributes("e:\temp\config.txt")
32
DHC-APP>write ##class(%File).SetAttributes("e:\temp\config.txt",33,.return)
1
DHC-APP>write ##class(%File).Attributes("e:\temp\config.txt")
33 The following example is in Unix On , Set the files in the default directory myfile From 644 Change to full permission (777):
USER>write ##class(%File).Attributes("myfile")
33188
USER>write ##class(%File).SetAttributes("myfile",33279,.return)
1
USER>write ##class(%File).Attributes("myfile")
33279By placing the regular file (32768) Value of and owner (448)、 Group (56) And others (7) Add the masks of to calculate the expected attribute value .
View other file and directory properties
%Library.File Other class methods of allow you to check various other properties of files and directories .
GetFileDateCreated() Method returns with $H Format the date the file or directory was created :
DHC-APP>write $zdate(##class(%File).GetFileDateCreated("stream"))
04/11/2020
DHC-APP>write $zdate(##class(%File).GetFileDateCreated("e:\temp"))
03/27/2021Be careful :Windows It is currently the only platform to track the actual creation date . Other platforms store the date of the last file state change .
GetFileDateModified() Methods to $H Format returns the modified date of the file or directory :
DHC-APP>write $zdate(##class(%File).GetFileDateModified("cache.dat"))
07/11/2021Method returns the size of the file , In bytes :
DHC-APP>write ##class(%File).GetFileSize("e:\temp\config.txt")
220GetDirectorySpace() Method returns the amount of free space and total space in the drive or directory . According to the fourth parameter ( It can be 0、1 or 2) Value , Space can be in bytes 、MB( The default value is ) or GB Return... For units . In this case ,2 Represent space with GB Return... For units :
DHC-APP>set status = ##class(%File).GetDirectorySpace("C:", .FreeSpace, .TotalSpace, 2)
DHC-APP>write FreeSpace
128.83
DHC-APP>write TotalSpace
247.97stay Windows On , If you pass the directory name to this method , The amount of space returned is the amount of space on the entire drive .
about GetDirectorySpace() Method , Any error status returned is an operating system level error . In the following example ,Windows System error code 3 Express “ The system could not find the specified path .”
DHC-APP>set status = ##class(%File).GetDirectorySpace("Q:", .FreeSpace, .TotalSpace, 2)
DHC-APP>do $system.Status.DisplayError(status)
error #83: Error code =3边栏推荐
- Problems encountered in the project u-parse component rendering problems
- max31865模块RTD测温注意事项
- Case sharing | integrated construction of data operation and maintenance in the financial industry
- 7-1 linked list is also simple fina
- 2022最新中高级Android面试题目,【原理+实战+视频+源码】
- 5. 数据访问 - EntityFramework集成
- 7-1 链表也简单fina
- 尚硅谷尚优选项目教程发布
- Lombok @builder annotation
- How to obtain the coordinates of the aircraft passing through both ends of the radar
猜你喜欢

Shang Silicon Valley Shang preferred project tutorial release

ViewPager + RecyclerView的内存泄漏

RedHat7.4配置yum软件仓库(RHEL7.4)

Thoroughly understand why network i/o is blocked?

LeetCode 6109. 知道秘密的人数

案例分享|金融业数据运营运维一体化建设

Rse2020/ cloud detection: accurate cloud detection of high-resolution remote sensing images based on weak supervision and deep learning

Word查找红色文字 Word查找颜色字体 Word查找突出格式文本

Find in MySQL_ in_ Detailed explanation of set() function usage
![[HCIA cloud] [1] definition of cloud computing, what is cloud computing, architecture and technical description of cloud computing, Huawei cloud computing products, and description of Huawei memory DD](/img/b8/624799e4bf788e4476b155486f478b.png)
[HCIA cloud] [1] definition of cloud computing, what is cloud computing, architecture and technical description of cloud computing, Huawei cloud computing products, and description of Huawei memory DD
随机推荐
常见时间复杂度
Ant group open source trusted privacy computing framework "argot": open and universal
[QNX hypervisor 2.2 user manual]6.3.2 configuring VM
RPC protocol details
Low code practice of xtransfer, a cross-border payment platform: how to integrate with other medium-sized platforms is the core
Precautions for RTD temperature measurement of max31865 module
Reptile 01 basic principles of reptile
2022 latest Android interview written examination, an Android programmer's interview experience
Use JMeter to record scripts and debug
音视频包的pts,dts,duration的由来.
爬虫01-爬虫基本原理讲解
max31865模块RTD测温注意事项
[HCIA cloud] [1] definition of cloud computing, what is cloud computing, architecture and technical description of cloud computing, Huawei cloud computing products, and description of Huawei memory DD
Case sharing | integrated construction of data operation and maintenance in the financial industry
vs2017 qt的各种坑
Shang Silicon Valley Shang preferred project tutorial release
技术分享 | 常见接口协议解析
【HCIA-cloud】【1】云计算的定义、什么是云计算、云计算的架构与技术说明、华为云计算产品、华为内存DDR配置工具说明
集合处理的利器
Problems encountered in the project u-parse component rendering problems