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?