Wesley Smith
Wesley Smith

Reputation: 19571

modify hosts file with autohotkey without running as administrator

In C:\Windows\System32\drivers\etc, I have the standard hosts file

In C:\Windows\System32\drivers\etc, I have a file named hosts-backup.txt

Using autohotkey, when I press `abc1, I'd like to replace the hosts file with the contents of the file named hosts-backup.txt

Here's what I have so far:

`::    
    Check := true
    SetTimer, CheckOff, 1000 ; 1 second to type in 001
return

:*:abc1::
    If Check


        ; Read the contents of the backup file 
    FileRead, Contents, C:\Windows\System32\drivers\etc\hosts-backup.txt
    if not ErrorLevel  ; Successfully loaded.
    {
        ListVars
        Pause

        ; delete the old hots file
            FileDelete, C:\Windows\System32\drivers\etc\hosts.txt               
            ; also tried this with C:\Windows\System32\drivers\etc\hosts

        ; save a new copy of the hosts file with the content from the backup file
            FileAppend, %Contents%, C:\Windows\System32\drivers\etc\hosts.txt 
            ; also tried this with C:\Windows\System32\drivers\etc\hosts

            Contents =  ; Free the memory.
    }
return


CheckOff:
    Check := false
return

ListVars returns the following:

Global Variables (alphabetical)
--------------------------------------------------
0[1 of 3]: 0
Check[1 of 3]: 0
Contents[0 of 0]:  
ErrorLevel[1 of 3]: 0

When the above script is run, the file at C:\Windows\System32\drivers\etc\hosts is not updated.

How can I fix this?


Update per Blauhirn's suggestions (still not working)

`::    
    Check := true
    SetTimer, CheckOff, -1000 ; 1 second to type in abc1
return

:*:abc1::
    If Check{


    ; Read the contents of the backup file 
    FileRead, Contents, C:\Windows\System32\drivers\etc\hosts-backup.txt
    if not ErrorLevel  ; Successfully loaded.
    {
        ListVars

        ; delete the old hots file
            FileDelete, C:\Windows\System32\drivers\etc\hosts.txt    

        ; save a new copy of the hosts file with the content from the backup file
            FileAppend, %Contents%, C:\Windows\System32\drivers\etc\hosts.txt 

            Contents =  ; Free the memory.
    }

     }
return


CheckOff:
    Check := false
return

Update

This appears to be a permissions issue, if I compile the script to an exe and run it as an administrator, it works as expected.

Is there a simple way around this using autohotkey?

Upvotes: 1

Views: 757

Answers (1)

phil294
phil294

Reputation: 10852

This piece of code of yours:

`::    
    Check := true
    SetTimer, CheckOff, 1000 ; 1 second to type in 001
return

CheckOff:
    Check := false
return

Will set check to false every 1000 ms repeatedly. "Specify a negative Period to indicate that the timer should run only once". Since it doesn't get set back to true anywhere in your code automatically, this whole timer thingy is kind of useless. What is it you want to achieve with it? Check will only be true within the first 1 second. Thus,

    If Check
        FileRead, Contents, C:\Windows\System32\drivers\hosts-backup.txt

Will not do anything after the second is over. Also be aware that the if-clause only works for the next line, because you're not using any braces { }.


edit

I might be wrong, but seems that you can spare the timer and simply make a hotstring out of it: (two `` because backtick is the escape character in ahk)

:*:``abc1::
        ; Read the contents of the backup file 
    FileRead, Contents, C:\Windows\System32\drivers\etc\hosts-backup.txt
    if not ErrorLevel  ; Successfully loaded.
    {
        ListVars
        Pause

        ; delete the old hots file
            FileDelete, C:\Windows\System32\drivers\etc\hosts.txt               
            ; also tried this with C:\Windows\System32\drivers\etc\hosts

        ; save a new copy of the hosts file with the content from the backup file
            FileAppend, %Contents%, C:\Windows\System32\drivers\etc\hosts.txt 
            ; also tried this with C:\Windows\System32\drivers\etc\hosts

            Contents =  ; Free the memory.
    }
return

Seems logical to me that only Administrators may edit the system32 folder.. have you tried changing the AutoHotkey.exe to "run ALWAYS as Administrator"? http://technet.microsoft.com/en-us/magazine/ff431742.aspx

Upvotes: 1

Related Questions