Sleep command optimization help

Get help with using AutoHotkey and its commands and hotkeys
sttrebo
Posts: 66
Joined: 27 Jan 2014, 12:31

Sleep command optimization help

22 Jun 2018, 14:55

hi-
I have a fairly long script that does a number of file/directory create/reads/writes. for a number of them, i check if it exists then if it doesn't I create it. if it does exist, i just delete it's contents to start fresh. here is a sample of the code:

Code: Select all

IfNotExist, %AHK_Temp%\qtdadd\
	{
	FileCreateDir, %AHK_Temp%\qtdadd\
	sleep, 250
	}
FileDelete, %AHK_Temp%\qtdadd\*.*
my question though is around the sleep command. if i wanted to optimize this code to run as fast as it can thru the script, i'd shorten down the sleep pauses as much as possible. question: is there a faster way to confirm a file/directory is ready for writing or appending instead of just using an arbitrary sleep pause time? If i set the sleep command too short, sometimes my later commands get messed up because the system isn't ready.

thanks
swagfag
Posts: 4021
Joined: 11 Jan 2017, 17:59

Re: Sleep command optimization help

22 Jun 2018, 15:19

use a waiting loop:

Code: Select all

while(!(FileExist(AHK_Temp "\qtdadd") ~= "D"))
	Sleep 25
though i must say, what uve posted thusfar is highly suspect and i can think of only one reason why ud structure it that way, that is, u want to only wipe files residing in the directory, but preserve any existing subdirectories.
if that is not the intended behavior, then id just use:

Code: Select all

dir := AHK_Temp "\qtdadd"
FileRemoveDir, % dir, 1
FileCreateDir, % dir
sttrebo
Posts: 66
Joined: 27 Jan 2014, 12:31

Re: Sleep command optimization help

22 Jun 2018, 15:27

hi-
thanks for your help. to your second point, yes.
on your first point: the ~= "D" is directory. i'm assuming i can just use "N" if it's a file ?

thanks for your help.
swagfag
Posts: 4021
Joined: 11 Jan 2017, 17:59

Re: Sleep command optimization help

22 Jun 2018, 16:19

ehhh not necessarily, not all files are flagged as 'normal', some might be system, some archived, some compressed or what have you. a better approach is checking if it isnt a directory. if it isnt, its a file:

Code: Select all

FileExist(filePath) ~= "^[^D]+$"
to match at least one of anything that isnt an uppercase D, or use InStr() but then u also gotta do the null-check

Return to “Ask For Help”

Who is online

Users browsing this forum: Dante, Lem2001, mikeyww, nestea2k and 47 guests