GetFileAttributes: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Forgot about the category.)
No edit summary
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{|align=right
{{PageAuthor|Instructor}}
|<small>Author: [[{{ns:2}}:Instructor|Instructor]] ([[{{ns:3}}:Instructor|talk]], [[{{ns:-1}}:Contributions/Instructor|contrib]])</small>
 
|}
{{User:Instructor/Headers/Template}}
<br style="clear:both;">
 
== The Function ==
== Function Description ==
<highlight-nsis>/*
 
<highlight-nsis>
____________________________________________________________________________
____________________________________________________________________________


                             GetFileAttributes v1.0
                             GetFileAttributes v1.2
____________________________________________________________________________
____________________________________________________________________________


Line 15: Line 16:


Syntax:
Syntax:
${GetFileAttributes} "[file]" "[attributes]" $var
${GetFileAttributes} "[File]" "[Attributes]" $var


"[file]"          ; File or directory
"[File]"          ; File or directory
                   ;
                   ;
"[attributes]"    ; "ALL"  (default)
"[Attributes]"    ; "ALL"  (default)
                   ;  -all attributes of file combined with "|" to output
                   ;  -all attributes of file combined with "|" to output
                   ;
                   ;
Line 31: Line 32:
                   ;    $var=1  file has specified attributes
                   ;    $var=1  file has specified attributes
                   ;    $var=0  file has no specified attributes
                   ;    $var=0  file has no specified attributes
                  ;    $var=-1  error (file isn't exist)




Example1:
Note:
Section
-Error flag if file isn't exist
</highlight-nsis>
 
 
 
<b>Example1:</b>
<highlight-nsis>Section
${GetFileAttributes} "C:\MSDOS.SYS" "ALL" $R0
${GetFileAttributes} "C:\MSDOS.SYS" "ALL" $R0
; $R0=READONLY|HIDDEN|SYSTEM|ARCHIVE
; $R0=READONLY|HIDDEN|SYSTEM|ARCHIVE
SectionEnd
SectionEnd
</highlight-nsis>


Example2:
<b>Example2:</b>
Section
<highlight-nsis>Section
${GetFileAttributes} "C:\MSDOS.SYS" "SYSTEM|HIDDEN" $R0
${GetFileAttributes} "C:\MSDOS.SYS" "SYSTEM|HIDDEN" $R0
; $R0=1
; $R0=1
SectionEnd
SectionEnd
</highlight-nsis>


Example3:
<b>Example3:</b>
Section
<highlight-nsis>Section
${GetFileAttributes} "C:\MSDOS.SYS" "NORMAL" $R0
${GetFileAttributes} "C:\MSDOS.SYS" "NORMAL" $R0
; $R0=0
; $R0=0
SectionEnd*/
SectionEnd
</highlight-nsis>


== Function Code ==


;---------------------------------------------------------------------------
<highlight-nsis>
 
Function GetFileAttributes
Function GetFileAttributes
!define GetFileAttributes `!insertmacro GetFileAttributesCall`
!define GetFileAttributes `!insertmacro GetFileAttributesCall`
Line 75: Line 84:


System::Call 'kernel32::GetFileAttributes(t r0)i .r2'
System::Call 'kernel32::GetFileAttributes(t r0)i .r2'
StrCmp $2 -1 error
StrCmp $2 -1 error
StrCpy $3 ''
StrCpy $3 ''


IntOp $0 $2 - 16384
IntOp $0 $2 & 0x4000
IntCmp $0 0 0 +4
IntCmp $0 0 +2
StrCpy $3 'ENCRYPTED|'
StrCpy $3 'ENCRYPTED|'
StrCpy $2 $0
StrCmp $2 0 all


IntOp $0 $2 - 8192
IntOp $0 $2 & 0x2000
IntCmp $0 0 0 +4
IntCmp $0 0 +2
StrCpy $3 'NOT_CONTENT_INDEXED|$3'
StrCpy $3 'NOT_CONTENT_INDEXED|$3'
StrCpy $2 $0
StrCmp $2 0 all


IntOp $0 $2 - 4096
IntOp $0 $2 & 0x1000
IntCmp $0 0 0 +4
IntCmp $0 0 +2
StrCpy $3 'OFFLINE|$3'
StrCpy $3 'OFFLINE|$3'
StrCpy $2 $0
StrCmp $2 0 all


IntOp $0 $2 - 2048
IntOp $0 $2 & 0x0800
IntCmp $0 0 0 +4
IntCmp $0 0 +2
StrCpy $3 'COMPRESSED|$3'
StrCpy $3 'COMPRESSED|$3'
StrCpy $2 $0
StrCmp $2 0 all


IntOp $0 $2 - 1024
IntOp $0 $2 & 0x0400
IntCmp $0 0 0 +4
IntCmp $0 0 +2
StrCpy $3 'REPARSE_POINT|$3'
StrCpy $3 'REPARSE_POINT|$3'
StrCpy $2 $0
StrCmp $2 0 all


IntOp $0 $2 - 512
IntOp $0 $2 & 0x0200
IntCmp $0 0 0 +4
IntCmp $0 0 +2
StrCpy $3 'SPARSE_FILE|$3'
StrCpy $3 'SPARSE_FILE|$3'
StrCpy $2 $0
StrCmp $2 0 all


IntOp $0 $2 - 256
IntOp $0 $2 & 0x0100
IntCmp $0 0 0 +4
IntCmp $0 0 +2
StrCpy $3 'TEMPORARY|$3'
StrCpy $3 'TEMPORARY|$3'
StrCpy $2 $0
StrCmp $2 0 all


IntOp $0 $2 - 128
IntOp $0 $2 & 0x0080
IntCmp $0 0 0 +4
IntCmp $0 0 +2
StrCpy $3 'NORMAL|$3'
StrCpy $3 'NORMAL|$3'
StrCpy $2 $0
StrCmp $2 0 all


IntOp $0 $2 - 64
IntOp $0 $2 & 0x0040
IntCmp $0 0 0 +4
IntCmp $0 0 +2
StrCpy $3 'DEVICE|$3'
StrCpy $3 'DEVICE|$3'
StrCpy $2 $0
StrCmp $2 0 all


IntOp $0 $2 - 32
IntOp $0 $2 & 0x0020
IntCmp $0 0 0 +4
IntCmp $0 0 +2
StrCpy $3 'ARCHIVE|$3'
StrCpy $3 'ARCHIVE|$3'
StrCpy $2 $0
StrCmp $2 0 all


IntOp $0 $2 - 16
IntOp $0 $2 & 0x0010
IntCmp $0 0 0 +4
IntCmp $0 0 +2
StrCpy $3 'DIRECTORY|$3'
StrCpy $3 'DIRECTORY|$3'
StrCpy $2 $0
StrCmp $2 0 all


IntOp $0 $2 - 4
IntOp $0 $2 & 0x0004
IntCmp $0 0 0 +4
IntCmp $0 0 +2
StrCpy $3 'SYSTEM|$3'
StrCpy $3 'SYSTEM|$3'
StrCpy $2 $0
StrCmp $2 0 all


IntOp $0 $2 - 2
IntOp $0 $2 & 0x0002
IntCmp $0 0 0 +4
IntCmp $0 0 +2
StrCpy $3 'HIDDEN|$3'
StrCpy $3 'HIDDEN|$3'
StrCpy $2 $0
StrCmp $2 0 all


IntOp $0 $2 - 1
IntOp $0 $2 & 0x0001
IntCmp $0 0 +2
StrCpy $3 'READONLY|$3'
StrCpy $3 'READONLY|$3'


all:
StrCpy $0 $3 -1
StrCpy $0 $3 -1
StrCmp $1 '' end
StrCmp $1 '' end
Line 189: Line 171:


error:
error:
StrCpy $0 -1
SetErrors
StrCpy $0 ''


end:
end:
Line 201: Line 184:
</highlight-nsis>
</highlight-nsis>


[[{{ns:14}}:Disk, Path & File Functions]]
[[Category:Disk, Path & File Functions]]

Latest revision as of 17:51, 8 June 2006

Author: Instructor (talk, contrib)


Page for NSIS 2.07 and below users

You can use the latest version of headers (recommended) or the following function code (put the function code in your script before calling it)

Function Description

____________________________________________________________________________
 
                            GetFileAttributes v1.2
____________________________________________________________________________
 
 
Get attributes of file or directory.
 
 
Syntax:
${GetFileAttributes} "[File]" "[Attributes]" $var
 
"[File]"          ; File or directory
                  ;
"[Attributes]"    ; "ALL"  (default)
                  ;  -all attributes of file combined with "|" to output
                  ;
                  ; "READONLY|HIDDEN|SYSTEM|DIRECTORY|ARCHIVE|
                  ; DEVICE|NORMAL|TEMPORARY|SPARSE_FILE|REPARSE_POINT|
                  ; COMPRESSED|OFFLINE|NOT_CONTENT_INDEXED|ENCRYPTED"
                  ;  -file must have specified attributes
                  ;
$var              ; Result:
                  ;    $var=attr1|attr2|... (if used "ALL")
                  ;    $var=1   file has specified attributes
                  ;    $var=0   file has no specified attributes
 
 
Note:
-Error flag if file isn't exist


Example1:

Section
	${GetFileAttributes} "C:\MSDOS.SYS" "ALL" $R0
	; $R0=READONLY|HIDDEN|SYSTEM|ARCHIVE
SectionEnd

Example2:

Section
	${GetFileAttributes} "C:\MSDOS.SYS" "SYSTEM|HIDDEN" $R0
	; $R0=1
SectionEnd

Example3:

Section
	${GetFileAttributes} "C:\MSDOS.SYS" "NORMAL" $R0
	; $R0=0
SectionEnd

Function Code

Function GetFileAttributes
	!define GetFileAttributes `!insertmacro GetFileAttributesCall`
 
	!macro GetFileAttributesCall _PATH _ATTR _RESULT
		Push `${_PATH}`
		Push `${_ATTR}`
		Call GetFileAttributes
		Pop ${_RESULT}
	!macroend
 
	Exch $1
	Exch
	Exch $0
	Exch
	Push $2
	Push $3
	Push $4
	Push $5
 
	System::Call 'kernel32::GetFileAttributes(t r0)i .r2'
	StrCmp $2 -1 error
	StrCpy $3 ''
 
	IntOp $0 $2 & 0x4000
	IntCmp $0 0 +2
	StrCpy $3 'ENCRYPTED|'
 
	IntOp $0 $2 & 0x2000
	IntCmp $0 0 +2
	StrCpy $3 'NOT_CONTENT_INDEXED|$3'
 
	IntOp $0 $2 & 0x1000
	IntCmp $0 0 +2
	StrCpy $3 'OFFLINE|$3'
 
	IntOp $0 $2 & 0x0800
	IntCmp $0 0 +2
	StrCpy $3 'COMPRESSED|$3'
 
	IntOp $0 $2 & 0x0400
	IntCmp $0 0 +2
	StrCpy $3 'REPARSE_POINT|$3'
 
	IntOp $0 $2 & 0x0200
	IntCmp $0 0 +2
	StrCpy $3 'SPARSE_FILE|$3'
 
	IntOp $0 $2 & 0x0100
	IntCmp $0 0 +2
	StrCpy $3 'TEMPORARY|$3'
 
	IntOp $0 $2 & 0x0080
	IntCmp $0 0 +2
	StrCpy $3 'NORMAL|$3'
 
	IntOp $0 $2 & 0x0040
	IntCmp $0 0 +2
	StrCpy $3 'DEVICE|$3'
 
	IntOp $0 $2 & 0x0020
	IntCmp $0 0 +2
	StrCpy $3 'ARCHIVE|$3'
 
	IntOp $0 $2 & 0x0010
	IntCmp $0 0 +2
	StrCpy $3 'DIRECTORY|$3'
 
	IntOp $0 $2 & 0x0004
	IntCmp $0 0 +2
	StrCpy $3 'SYSTEM|$3'
 
	IntOp $0 $2 & 0x0002
	IntCmp $0 0 +2
	StrCpy $3 'HIDDEN|$3'
 
	IntOp $0 $2 & 0x0001
	IntCmp $0 0 +2
	StrCpy $3 'READONLY|$3'
 
	StrCpy $0 $3 -1
	StrCmp $1 '' end
	StrCmp $1 'ALL' end
 
	attrcmp:
	StrCpy $5 0
	IntOp $5 $5 + 1
	StrCpy $4 $1 1 $5
	StrCmp $4 '' +2
	StrCmp $4 '|'  0 -3
	StrCpy $2 $1 $5
	IntOp $5 $5 + 1
	StrCpy $1 $1 '' $5
	StrLen $3 $2
	StrCpy $5 -1
	IntOp $5 $5 + 1
	StrCpy $4 $0 $3 $5
	StrCmp $4 '' notfound
	StrCmp $4 $2 0 -3
	StrCmp $1 '' 0 attrcmp
	StrCpy $0 1
	goto end
 
	notfound:
	StrCpy $0 0
	goto end
 
	error:
	SetErrors
	StrCpy $0 ''
 
	end:
	Pop $5
	Pop $4
	Pop $3
	Pop $2
	Pop $1
	Exch $0
FunctionEnd