DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: AHK Script to create and delete a file until it is closed  (Read 9,725 times)

Zero3K

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 316
AHK Script to create and delete a file until it is closed
« on: August 07, 2019, 11:45 PM »
It would be nice if someone could make one that fills a file (in the root directory) with the word Dummy and deletes it then repeats the process until it is closed.

highend01

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 188
Re: AHK Script to create and delete a file until it is closed
« Reply #1 on: August 08, 2019, 07:31 AM »
Code
#NoEnv
#SingleInstance force
SetWorkingDir %A_ScriptDir%
FileEncoding UTF-8
SetBatchLines, -1

drive := "C:\"
file  := "Dummy.txt"
path  := drive . file
text  := "Dummy"


if FileExist(drive) {
    Loop {
        if FileExist(path)
            FileDelete, % path
        FileAppend, % text, % path
    }
}

Zero3K

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 316
Re: AHK Script to create and delete a file until it is closed
« Reply #2 on: August 08, 2019, 07:39 PM »
Thanks. And would removing the delete line from it make it overwrite the file after it is made or already exists?

highend01

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 188
Re: AHK Script to create and delete a file until it is closed
« Reply #3 on: August 08, 2019, 08:36 PM »
Nope

Zero3K

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 316
Re: AHK Script to create and delete a file until it is closed
« Reply #4 on: August 10, 2019, 03:32 PM »
Can you please make one that overwrites the file that is made?

highend01

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 188
Re: AHK Script to create and delete a file until it is closed
« Reply #5 on: August 11, 2019, 02:42 PM »
Code
#NoEnv
#SingleInstance force
SetWorkingDir %A_ScriptDir%
FileEncoding UTF-8
SetBatchLines, -1

drive := "C:\"
file  := "Dummy.txt"
path  := drive . file
text  := "Dummy"


if FileExist(drive) {
    Loop {
        EmptyFile(path)
        FileAppend, % text, % path
    }
}

EmptyFile(file) {
    hFile := DllCall("CreateFile", Str, file, UInt, 0x40000000, UInt, 3, UInt, 0, UInt, 5, UInt, 0, UInt, 0)
    DllCall("CloseHandle", UInt, hFile)
    return hFile > 0 ? 1 : hFile
}

But ofc this empties the file before it gets overwritten...

OpenFile() would impose too many problems (no unicode, no > 128 file path length...)

Original address: https://www.donationcoder.com/forum/index.php?topic=48388.0