Sign in to follow this  
Followers 0

Why is file locked by script?

2 posts in this topic

G'day everyone

I have written the following glossary script:

; Script to look up all words from a highlighted text in a glossary.

; The glossary is named gloss.txt, and is in the format:
; sourceword[tab]targetword[tab]explanation

; Results are written to a text file, results.txt

#include <file.au3>
AutoItSetOption ("WinTitleMatchMode", 2)

HotKeySet("^m", "glossaryfunc")

While 1
Sleep ("100")

Func glossaryfunc()

; Grab text to clipboard, and save to temporary text file

Send ("^c")
Sleep ("100")
$grabtext = ClipGet()
$grabtextfile = FileOpen("GrabTextFile", 2)
FileWrite ($grabtextfile, $grabtext)
FileClose ($grabtextfile)

; Delete the contents of the results file (if any)

$resultsa = FileOpen("results.txt", 2)
FileClose ($resultsa)

; Segment the grabbed text

$segtexta = FileOpen("GrabTextFile", 0)
$segtextb = FileReadLine ($segtexta, 1)
$segtextc = StringSplit ($segtextb, " .,!?:;<>{}[]()" & @TAB, 0)

; Segment the glossary

$segglossa = FileOpen("gloss.txt", 0)
$results = FileOpen("results.txt", 2)
$glosslines = _FileCountLines("gloss.txt")

For $i = 1 to $segtextc[0]
If StringLen ($segtextc[$i]) > 1 Then

; Check which grabbed words are in the glossary, and
; write their full glossary entries to the results file

For $j = 1 to $glosslines

$segglossb = FileReadLine ($segglossa, $j)
$segglossc = StringSplit ($segglossb, @TAB, 1)

If $segglossc[1] = $segtextc[$i] Then
$results = FileOpen("results.txt", 1)
FileWrite ($results, $segglossb & @CRLF)




; Close all open files


; You can have the results display in a text editor if the text
; editor has an easy way of reloading the file
; WinActivate ("results.txt", "")
; WinWaitActive ("results.txt", "")
; Send ("!r") ; Alt+recent files
; Send ("1") ; select first recent file



The file results.txt is closed by the script, but if the script is still running, the file results.txt is "locked" in such a way that I can edit it in another text editor but I can't save it (unless I save it as another name). This is not a major hassle but I'd like to know how I can make sure that an Autoit script doesn't lock or occupy a file which was opened and closed by the script.



PS: the script's not finished... and I have no idea how it will perform with glossaries of 10 000 entries in them.

Share this post

Link to post
Share on other sites

#2 ·  Posted (edited)

The file results.txt is closed by the script, ...

Actually, no.

Look at the second time you open that file in mode 2 : where are you closing it ?

Suggestion : use indentation. Indent (among other stuff) on every file-open, and de-indend on every file close. That way you can easily track this kind of problems. :lmao:

Edited by BitRot

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0