Sign in to follow this  
Followers 0
JohnBailey

FileSetAttrib not working

14 posts in this topic

I'm using FileSetAttrib to change the Attributes of a file on a NetWorkDrive, which is where the script is located. Any reasons why the following wouldn't change the attribute or at least say it doesn't? It's not even giving me the error msg that it didn't change it.

Global $dbFile = @ScriptDir&"\FullListForDownloading.txt"
;Make file Writeable
If Not FileSetAttrib($dbFile, "+R") Then 
    MsgBox(0,'Error', $dbFile&' could not be made writeable. Save feature will not work.')
    Exit
EndIf

$attrib = FileGetAttrib($dbFile)
If @error Then
    MsgBox(0,"Error", "Could not obtain attributes.")
    Exit
Else
    If StringInStr($attrib, "R") Then
        MsgBox(0,"", "File is read-only.")
    Else
        MsgBox(0,"", "File is NOT read-only.")
    EndIf
EndIf

A decision is a powerful thing

Share this post


Link to post
Share on other sites



You're setting the file to read-only, which contradicts with your error message...


"be smart, drink your wine"

Share this post


Link to post
Share on other sites

You're setting the file to read-only, which contradicts with your error message...

thanks, it still isn't working though :rolleyes:

Global $dbFile = @ScriptDir&"\FullListForDownloading.txt"
;Make file Writeable
If Not FileSetAttrib($dbFile, "-R") Then 
    MsgBox(0,'Error', $dbFile&' could not be made writeable. Save feature will not work.')
    Exit
EndIf

$attrib = FileGetAttrib($dbFile)
If @error Then
    MsgBox(0,"Error", "Could not obtain attributes.")
    Exit
Else
    If StringInStr($attrib, "R") Then
        MsgBox(0,"", "File is read-only.")
    Else
        MsgBox(0,"", "File is NOT read-only.")
    EndIf
EndIf

If Not FileSetAttrib($dbFile, "+R") Then 
    MsgBox(0,'Error', $dbFile&' could not be made read-only.')
    Exit
EndIf

$attrib = FileGetAttrib($dbFile)
If @error Then
    MsgBox(0,"Error", "Could not obtain attributes.")
    Exit
Else
    If StringInStr($attrib, "R") Then
        MsgBox(0,"", "File is read-only.")
    Else
        MsgBox(0,"", "File is NOT read-only.")
    EndIf
EndIf

A decision is a powerful thing

Share this post


Link to post
Share on other sites

Any ideas someone?


A decision is a powerful thing

Share this post


Link to post
Share on other sites

Try this test script. The extra If's may force testing in sequence before continuing on with the script.

Global $dbFile = @ScriptDir&"\FullListForDownloading.txt"
FileWriteLine($dbFile, '')

If Not FileSetAttrib($dbFile, "-R") Then
    MsgBox(0,'Error1', $dbFile&' could not be made writeable. Save feature will not work.')
    Exit
EndIf

If Not FileGetAttrib($dbFile) And @error Then
    MsgBox(0,"Error2", "Could not obtain attributes.")
    Exit
EndIf

If Not FileSetAttrib($dbFile, "+R") Then
    MsgBox(0,'Error3', $dbFile&' could not be made read-only.')
    Exit
EndIf

If Not FileGetAttrib($dbFile) And @error Then
    MsgBox(0,"Error4", "Could not obtain attributes.")
    Exit
EndIf

If FileSetAttrib($dbFile, "-R") Then
    FileDelete($dbFile)
EndIf

:rolleyes:

Share this post


Link to post
Share on other sites

Try this test script. The extra If's may force testing in sequence before continuing on with the script.

:rolleyes:

Thanks, but that didn't do the trick. :rambo: Could it be because it's a network drive? I can change it manually. I can do a FileExist and a FileWriteLine without problems, but FileSetAttrib won't set the attribs.


A decision is a powerful thing

Share this post


Link to post
Share on other sites

FileSetAttrib will remove Attributes but will not add them. Odd!


A decision is a powerful thing

Share this post


Link to post
Share on other sites

Thanks, but that didn't do the trick. :rambo: Could it be because it's a network drive?...

If your using UNC paths then it maybe ok. If you are referring to mapping a network drive then your script may need to do the mapping before using further script functions else they may fail. Others have mentioned some issues with network drives using a cache for changes rather then updating immediately. Unfortunately, I cannot be further help to you as I have no network drive here to play with this.

:rolleyes:

Share this post


Link to post
Share on other sites

If your using UNC paths then it maybe ok. If you are referring to mapping a network drive then your script may need to do the mapping before using further script functions else they may fail. Others have mentioned some issues with network drives using a cache for changes rather then updating immediately. Unfortunately, I cannot be further help to you as I have no network drive here to play with this.

:rolleyes:

MHz thank you though! This is indeed an odd issue. I have tried UNC paths and @ScriptDir and the mapping name, which none have made a difference. The odd thing is that the script will remove R but not add it. odd isn't it. -R works but +R doesn't


A decision is a powerful thing

Share this post


Link to post
Share on other sites

Actually, any time FileSetAttrib runs it will set the file to A only no matter what! that's soooooooooo weird!


A decision is a powerful thing

Share this post


Link to post
Share on other sites

MHz or anyone, do you know how FileSetAttrib works? Is the code somewhere in the base files for AutoIt. My guess is that it's using a DLL of some kind.


A decision is a powerful thing

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

MHz or anyone, do you know how FileSetAttrib works? Is the code somewhere in the base files for AutoIt. My guess is that it's using a DLL of some kind.

Digging this up as it's a good match to my question/needs.

I'm working on a little script that will reset the HOSTS file on an infected system. I want it to work even if its security has been messed with.

For testing puroses, I start by setting my Hosts to "Deny" Everyone, and to +RSH.

When I run the script, resetting the permissions works fine but resetting the attributes fails consistenly. I've even got an extra command in for redundancy but neither work! Argh.

RunWait(@ComSpec & ' /c ' & 'echo,Y|cacls "%WinDir%\system32\drivers\etc\hosts" /G everyone:f') ;Works
    RunWait(@ComSpec & ' /c ' & 'attrib -s -h -r "%WinDir%\system32\drivers\etc\hosts"') ;
    FileSetAttrib(@SystemDir & "\drivers\etc\hosts","-shr") ;should do the same as 2nd line but both seem to fail

    FileCopy(@SystemDir & "\drivers\etc\hosts", "C:\old_hosts.txt", 1) ;when working, change to a LOG folder on USB
    FileDelete(@SystemDir & "\drivers\etc\hosts")
    FileInstall("y:\disinfect\Default_Hosts", @SystemDir & "\drivers\etc\hosts", 1) ;reset to an MVPS Hosts file to limit reinfection
I've even tried it with a plain txt file in the same folder. It's the attributes that keep tripping it up.

Edit: I changed the location of the file to c:\test.txt and it will work on that. So, it seems to be the ETC folder and it's permissions. The script runs with #requireAdmin but that doesn't seem to make a difference. I'm open to suggestions.

Edited by Xander

Share this post


Link to post
Share on other sites

#requireAdmin is not enough, add this SciTE directive to the script before compiling:

#AutoIt3Wrapper_Res_requestedExecutionLevel=requireAdministrator

Share this post


Link to post
Share on other sites

#requireAdmin is not enough, add this SciTE directive to the script before compiling:

#AutoIt3Wrapper_Res_requestedExecutionLevel=requireAdministrator

Thanks but, unfortunately, it didn't make a difference. I'm really sure it has to do with the folder's permissions. If I do the exact same test on a file elsewhere, it works.

Also noted, by running the command through CMD, that cacls has been put to pasture (I'm running Win7x64 Home Prem) but it still works.

Odd, though, even when I make a blank/default TXT and rename it to Hosts, the permissions seem really plain-jane and I can manually delete it but, through the script, I can neither change attributes or even delete it(!)

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