Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Autohotkey 是否使用自动热键清空文本文件?_Autohotkey - Fatal编程技术网

Autohotkey 是否使用自动热键清空文本文件?

Autohotkey 是否使用自动热键清空文本文件?,autohotkey,Autohotkey,我正在设法用自动热键清空文本文件。不幸的是,我找不到任何内置函数可以让你这么做。我能想到的最佳解决方案是: FileRead, TheText, file.txt StringReplace, NewText, TheText, 'text to search for', 'replacement text', All FileDelete, file.txt FileAppend, %NewText%, file.txt 但是,这个解决方案对我不起作用,因为我正在编写一个工具,可以从Wind

我正在设法用自动热键清空文本文件。不幸的是,我找不到任何内置函数可以让你这么做。我能想到的最佳解决方案是:

FileRead, TheText, file.txt
StringReplace, NewText, TheText, 'text to search for', 'replacement text', All
FileDelete, file.txt
FileAppend, %NewText%, file.txt
但是,这个解决方案对我不起作用,因为我正在编写一个工具,可以从Windows主机文件中添加或删除内容。当然,删除主机文件是不可能的

因此,我将把hosts文件内容加载到一个变量中,用该变量中的空字符串替换程序所做的任何添加(以恢复原始hosts文件内容),并将其写回hosts文件。但为了做到这一点,我需要先清空文件,否则它只会将变量附加到现有内容中

对于那些对我的节目感兴趣的人:


该程序将启用/禁用outlook.com或hotmail.com上的Skype自动登录功能。当然,邮件功能或Skype客户端不会出现任何问题。

下面是一种使用fileobject()的方法

只要把这个例子放在一个新的脚本文件中并运行它,看看它是如何工作的

/* String1
 * String2
 * String3
 * String4
 * String5
 * String6
 * String7
 * String8
 * String10
*/

msgbox % CutString(A_ScriptFullPath, "String7")

CutString(filePath, Needle)
{
    fileObj := FileOpen(filePath, "rw"), pointerPos := fileObj.Pos, RegExMatch(fileObj.read(), "O)" needle, match)

    fileObj.Seek(0), newText .= fileObj.read(match.pos-1), fileObj.Seek(match.pos + match.Len), newText .= fileObj.read()

    fileObj.Seek(pointerPos), fileObj.Write(newText), fileObj.length := StrLen(newText), fileObj.Close()
    return match.value
}
此函数使用正则表达式查找文件中“针”字符串的位置和大小,然后重写文件内容并调整其大小

您还可以重写函数,只需从文件中剪切行


我希望这符合您的需要

重命名它怎么样?我以前做过这个

hosts := "c:\windows\system32\etc\hosts"

fileread, hostsText, %hosts%
stringreplace, newText, %hostsText%, %searchtext%, %replacementtext%, all
fileappend, %hostsText%, %hosts%.bak   ;make backup
filedelete, %hosts%.temp               ;delete the temp file if it exists
fileappend, %newtext%, %hosts%.temp    ;write new text to a blank file
filemove, %hosts%.temp, %hosts%, 1     ;rename the new file to hosts - set flag to 1 to overwrite
那么你需要刷新DNS,不是吗

Run cmd /c ipconfig /flushdns

请按照以下步骤操作:

  • 运行时,notepad.exe无效\u response.txt
  • 睡吧,200
  • 发送,^a
  • 发送,{Del}
  • 睡吧,200
  • 发送!{F4}
  • 发送,{HOME}
  • 森德劳#
  • 发送,^s
  • 睡吧,200
  • 发送!{F4}
  • 出口

  • 虽然这可能有效,但它并不是我问题的答案。不过我还是觉得做一个备份文件以防万一是个很好的主意。因此,我将投票表决:)顺便说一句,我认为在WindowsVista+上,主机文件会立即生效。不需要刷新dns。我以前这样做是为了尝试在多个主机文件之间切换。您可以有多个主机文件,为了在它们之间切换,您可以将其中一个重命名为临时名称,然后只需“附加”到一个新的、带有主机名称的空白文件。它既简单又快速。而且它不会锁定您的主机文件。如果你进入文件读取模式,文件将被锁定(无法编辑),直到你释放它。这似乎是一个完美的解决方案,我的情况。我现在无法测试,但我会尽快接受你的答案。谢谢!哎呀,我忘了把这个问题标为已回答。无论如何,谢谢!