Sign in to follow this  
Followers 0
joakim

BitAND for identifying file permissions

12 posts in this topic

#1 ·  Posted (edited)

I made som code that will produce the right output, but I feel it is unnecessary long with BitAND and a bunch of If statements. So I was wondering if the same result could be achieved by BitOR? Here's what I've got;

$input = 0x4001
$output = ""
$read_only = 0x0001
$hidden = 0x0002
$system = 0x0004
$archive = 0x0020
$device = 0x0040
$normal = 0x0080
$temporary = 0x0100
$sparse_file = 0x0200
$reparse_point = 0x0400
$compressed = 0x0800
$offline = 0x1000
$not_indexed = 0x2000
$encrypted = 0x4000

If BitAND($input,$read_only) Then
    $output &= 'read_only+'
EndIf
If BitAND($input,$hidden) Then
    $output &= 'hidden+'
EndIf
If BitAND($input,$system) Then
    $output &= 'system+'
EndIf
If BitAND($input,$archive) Then
    $output &= 'archive+'
EndIf
If BitAND($input,$device) Then
    $output &= 'device+'
EndIf
If BitAND($input,$normal) Then
    $output &= 'normal+'
EndIf
If BitAND($input,$temporary) Then
    $output &= 'temporary+'
EndIf
If BitAND($input,$sparse_file) Then
    $output &= 'sparse_file+'
EndIf
If BitAND($input,$reparse_point) Then
    $output &= 'reparse_point+'
EndIf
If BitAND($input,$compressed) Then
    $output &= 'compressed+'
EndIf
If BitAND($input,$offline) Then
    $output &= 'offline+'
EndIf
If BitAND($input,$not_indexed) Then
    $output &= 'not_indexed+'
EndIf
If BitAND($input,$encrypted) Then
    $output &= 'encrypted+'
EndIf

$output = StringMid($output,1,StringLen($output)-1)
ConsoleWrite("$output = " & $output & @crlf)

I want the result to be like; "read_only+encrypted".

Joakim

Edited by joakim

Share this post


Link to post
Share on other sites



Maybe like this.

Global $aFilePermissions[13][2] = [ _
        [0x0001, 'read_only'], _
        [0x0002, 'hidden'], _
        [0x0004, 'system'], _
        [0x0020, 'archive'], _
        [0x0040, 'device'], _
        [0x0080, 'normal'], _
        [0x0100, 'temporary'], _
        [0x0200, 'sparse_file'], _
        [0x0400, 'reparse_point'], _
        [0x0800, 'compressed'], _
        [0x1000, 'offline'], _
        [0x2000, 'not_indexed'], _
        [0x4000, 'encrypted']]
        
Global $input = 0x4001
Global $output = ""

For $i = 0 To UBound($aFilePermissions) - 1
    If BitAND($input, $aFilePermissions[$i][0]) Then $output &= $aFilePermissions[$i][1] & '+'
Next

$output = StringTrimRight($output, 1)
ConsoleWrite("$output = " & $output & @CRLF)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

How about running it in a loop? :mellow:

$input = 0x4001
$output = ""

Dim $a_rights[15][2] = [ _
[14, 0], _
[0x0001, "read_only"], _
[0x0002, "hidden"], _
[0x0004, "system"], _
[0x0020, "archive"], _
[0x0040, "device"], _
[0x080, "normal"], _
[0x0100, "temporary"], _
[0x0200, "sparse_file"], _
[0x0400, "reparse_point"], _
[0x0800, "compressed"], _
[0x1000, "offline"], _
[0x2000, "not_indexed"], _
[0x4000, "encrypted"]]


For $i = 1 To $a_rights[0][0]
    If BitAND($input, $a_rights[$i][0]) Then $output &= $a_rights[$i][1] & " + "
Next
$output = StringTrimRight($output, 3)
ConsoleWrite("$output = " & $output & @crlf)

Edit: Well, somewhat too slow, eh? :)

Edited by Hannes123

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

Thanks guys.

It was very helpful to get examples of alternativ usage on this case. Unfortunately, for some strange reason, it seems like my sample performs faster.. Since the code will run in a huge loop about a 100.000 times (filesystem analysis), I must keep the fastest. But once again, thank you very much for the examples.

Share this post


Link to post
Share on other sites

Well, running 1.000.000 iterations it comes to a difference of 27s (our version) to 17s (your version).

This is another example of "AutoIT is not fast in loops". :)

But if you#re running over 100.000 of files the difference of 10 sec in difference wouldn't play a big role. :mellow:


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

Yes the isolated diff on this particular code is not big at all, but in total with all calculations, the script will take about 10 minutes per 100 Mb of input, so I have to cut back on each part I can even though it's not much. Btw, it's an $MFT analyzer..

Share this post


Link to post
Share on other sites

Instead of using this style of statement:

If BitAND($input,$read_only) Then
    $output &= 'read_only+'
EndIf

Try it this way to see if the speed increases or not:

If BitAND($input,$read_only) Then $output &= 'read_only+'

It might be a slight bit faster this way, I seem to remember some timing tests that said an inline If statement was faster than a multi-line one. O it could have been the other way around, getting old has its drawbacks. :mellow:


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

If you want the fastest code, then you should stick to this: One-line Ifs are sligtly faster and each variable access needs time

$output = ""
If BitAND($input,1) Then $output &= 'read_only+'
If BitAND($input,2) Then $output &= 'hidden+'
If BitAND($input,4) Then $output &= 'system+'
If BitAND($input,0x20) Then $output &= 'archive+'
If BitAND($input,0x40) Then $output &= 'device+'
If BitAND($input,0x80) Then $output &= 'normal+'
If BitAND($input,0x100) Then $output &= 'temporary+'
If BitAND($input,0x200) Then $output &= 'sparse_file+'
If BitAND($input,0x400) Then $output &= 'reparse_point+'
If BitAND($input,0x800) Then $output &= 'compressed+'
If BitAND($input,0x1000) Then $output &= 'offline+'
If BitAND($input,0x2000) Then $output &= 'not_indexed+'
If BitAND($input,0x4000) Then $output &= 'encrypted+'
$output = StringTrimRight($output, 1)

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

You are right about 1-line Ifs. It is a little bit faster. Just tested. Thanks.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

If you want to save time change the line where you cut the last char from the string from

$output = StringMid($output,1,StringLen($output)-1)

to

$output = StringtrimRight($output,1)

Edit: corrected what I really wanted to say (D'oh!) :mellow:

Edited by Hannes123

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

I'm afraid I can't see any difference..

Share this post


Link to post
Share on other sites

You are right. StringTrimRight seems slightly faster too. Thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0