Faster File Loop for Multiple Queries

Put simple Tips and Tricks that are not entire Tutorials in this forum
steventaitinger
Posts: 20
Joined: 14 Jan 2014, 10:28
Contact:

Faster File Loop for Multiple Queries

10 Jul 2014, 11:03

This is just a few tips on working with a bunch of files. Nothing shocking but I couldn't find exactly what I was looking for oline. Here is where I got some code from http://www.autohotkey.com/board/topic/9 ... -a-folder/.

I implemented the solution of building an index of the files and then doing the for each, object in array type search through the list of filenames. It took my script from 10 seconds to less than a second because I need to do about 240 matches each time I run the script.

Something that is worth mentioning is that if you want your indexing loop to go faster or if you don't want to match the shortname of the file name (annoying!) then do the index loop like below. Also, don't use A_loopfilelongpath as it has to do extra conversions than A_LoopFileLongPath!

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

;build folder list takes takes 200-300 ms
word_files :=[]
loop, M:\* , 0 , 1
{
if instr(A_LoopFileName, "~");this part skips recording the short names
continue
word_files.Insert({path:A_LoopFileFullPath})
}


For searching in the list...

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

find_word_file(section_number) {
global
for index, file in word_files
{
if instr(file.path, product "-" section_number ".doc")
return file.path
}
}
lexikos
Posts: 5938
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Faster File Loop for Multiple Queries

04 Sep 2014, 01:58

if instr(A_LoopFileName, "~");this part skips recording the short names
I would advise against using this in most cases. It will record neither the short name nor the long name whenever A_LoopFileName contains '~'. If A_LoopFileName returns short names for every file, the loop will get no files at all. If a file's long name contains '~' (common for temporary files created by MS Word, for instance), it will also be skipped.

If you're getting the same files listed twice, once with short name and once with long name, then something is wrong with either your system or the program (or you really have duplicate files).

(This is also a syntax error; semicolon must be preceded by a space or tab to indicate a comment.)
steventaitinger
Posts: 20
Joined: 14 Jan 2014, 10:28
Contact:

Re: Faster File Loop for Multiple Queries

30 Sep 2014, 13:02

Thanks for the tips Lexikos.

Our files don't have ~ in it by policy and I don't want temp files because I am publishing released tech unit files to pdf so that wasn't a problem for me. I spent more time formatting our files to be consistent so I didn't have so many different cases to consider and switched to A_LoopFileFullPath and no longer use the line you mentioned. Autohotkey docs and these forums have taught me so much about programming that now I actually have a plan to switch from engineering to programming! Thanks :) I always saw binary text in application or image files and c++ code that confused me whenever I tried to learn some programming earlier but starting with 1 liners in Autohotkey built my confidence to tackle the msdn and now I am learning java and python before c++. Also loving an easier job thanks to AHK automation!

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 0 guests