1

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?

Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
  • Why not just do file renames? `A->C B->A` switcharoo? – Joe DF Jul 09 '15 at 17:00
  • @JoeDF mainly because this is just the first step, once I get this working I will be performing string replacements on the file contents depending on what keys are pressed. Getting the contents of the backup file each time allows me to reset the contents back to the original values. – Wesley Smith Jul 09 '15 at 17:04
  • ..." Since [`Check`] 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?" – phil294 Jul 09 '15 at 17:15
  • @Blauhirn, Check gets set to true everytime the user presses the backtick/tilde key – Wesley Smith Jul 09 '15 at 17:16
  • and after that, you give him 1 seond time to type `abc1` ? why not make a single hotstring :*:`abc1:: out of it? – phil294 Jul 09 '15 at 17:18
  • @Blauhirn honestly, I was just working from this example I found http://www.autohotkey.com/board/topic/64063-trigger-action-on-sequence-of-hotkeys/?p=403993 – Wesley Smith Jul 09 '15 at 17:22

1 Answers1

1

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

phil294
  • 10,038
  • 8
  • 65
  • 98
  • Thanks for the reply. To intent is for the user to press the backtick/tilde key , then if within the next second the user types "abc1" the function is run. I added the brackets you pointed out and changes and added the - to the timer. It turns out the issues is caused by permissions in the etc folder. If I compile the script to an exe and run it as admin (both your version and my original version) it works as expected – Wesley Smith Jul 09 '15 at 17:18
  • Yeah, definitely logical. I did change it to "run ALWAYS as Administrator" but doing that I get the UAC popup every time the program starts, I'm hoping to avoid that. – Wesley Smith Jul 09 '15 at 17:26
  • 1
    (I edited my post) Sorry, I can't help you any more then, not better than google does. Last thing that comes into my mind is the `runAs` command in ahk, maybe it will work..? – phil294 Jul 09 '15 at 17:34
  • Thanks for the help, I found this just now, trying to get it going now http://www.autohotkey.com/board/topic/70449-enable-interaction-with-administrative-programs/ – Wesley Smith Jul 09 '15 at 17:35
  • 1
    sorry my bad. runAs won't help, it only affects the `run` commands used after it, not AutoHotkey itself. Good luck xD – phil294 Jul 09 '15 at 17:36
  • Thanks again for trying to help! I found a way to do it via this method: http://www.sevenforums.com/tutorials/11949-elevated-program-shortcut-without-uac-prompt-create.html – Wesley Smith Jul 09 '15 at 18:18