Jump to content

Autoit not closing files?


Recommended Posts

Has anyone else had any problems with FileClose?

I have a process where data is written to an .asx file, and then the file is closed, and then that .asx plus some other stuff gets zipped up. But, for some reasonduring the compression process I get a message that the .asx file is still in use by another process. But, I definitely ran the FileClose operation on the file. do I need to insert a certain amount of wait for autoit to REALLY close the file?

Link to comment
Share on other sites

Could you post part of the code?

Maybe exit the loop after it has been written? Then the next loop would be to compress it.

Because i think that if it wrote to the file and closed the file, supposedly, technically it would still be trying to write to the file right? So if you exit the loop, the file really closes and you are able to compress it without error.

Am i on the right track?

Edited by acidfear
Link to comment
Share on other sites

Could you post part of the code?

Maybe exit the loop after it has been written? Then the next loop would be to compress it.

Because i think that if it wrote to the file and closed the file, supposedly, technically it would still be trying to

write to the file right? So if you exit the loop, the file really closes and you are able to compress it without error.

Am i on the right track?

Well, if it helps, here's the script in it's entirity with sensitive stuff xxxx'd out.

It's the file called $playListName that isn't getting closed.

#include <Process.au3>

$opf = FileOpenDialog("Open Tab Delimited Text file containing playlist", "C:\DataSession\PL\", "Tab Delimited playlist (*.txt)", 0)
$infile = FileOpen($opf, 0)
$outFolder = FileSelectFolder("Choose a folder to output the Data Session:", "")
$discName = StringMid($opf,StringInStr($opf,"\", 0, 3)+1,StringLen($opf)-StringInStr($opf,"\", 0, 3)-4)
$playListName = $discName & ".asx"

If $infile = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

FileOpen($outFolder & "\" & $playListName, 2)
FileWriteLine($outFolder & "\" & $playListName,"<Asx Version = " & Chr(34) & "3.0" & Chr(34) & ">")
FileWriteLine($outFolder & "\" & $playListName,"<Param Name = " & Chr(34) & "AllowShuffle" & Chr(34) & " Value = " & Chr(34) & "yes" & Chr(34) & " />")
FileWriteLine($outFolder & "\" & $playListName,"<Title>" & $discName & "</Title>")

$doloop = 1
While $doloop = 1
    $line = FileReadLine($infile)
    If @error = -1 Then ExitLoop
    IF StringIsSpace($line) Then 
        $doloop = 0
    Else
        $lineArray = StringSplit($line,"    ",1)
        $trackNumber = $lineArray[1]
        $trackTitle = $lineArray[2]
        $trackArtist = $lineArray[3]
        $newFileName = $discName & "-" & $trackNumber & ".wmv"
            IF StringIsDigit(StringLower(StringLeft($trackTitle,1))) Then
                $trackPath = "S:\XXXXXX\"
            Else
                $trackPath = "S:\XXXXX" & StringLower(StringLeft($trackTitle,1)) & "\"
            EndIf
        FileCopy(StringReplace($trackPath & StringLeft($trackTitle,6) & "*XXXXX" & StringLeft($trackArtist,6)," ","_") & "*.wmv", $outFolder & "\" & $newFileName)
        FileWriteLine($outFolder & "\" & $playListName,"<Entry>")
        FileWriteLine($outFolder & "\" & $playListName,"<Author >" & $trackArtist & "</Author>")
        FileWriteLine($outFolder & "\" & $playListName,"<Copyright > XXXXXXXX</Copyright>")
        FileWriteLine($outFolder & "\" & $playListName,"<Param Name = " & Chr(34) & "Description" & Chr(34) & " Value = " & Chr(34) & "XXXXXXXXXX" & Chr(34) & "/>")
        FileWriteLine($outFolder & "\" & $playListName,"<Param Name = " & Chr(34) & "FileType" & Chr(34) & " Value = " & Chr(34) & "wmv" & Chr(34) & " />")
        FileWriteLine($outFolder & "\" & $playListName,"<Param Name = " & Chr(34) & "Is_Protected" & Chr(34) & " Value = " & Chr(34) & "False" & Chr(34) & " />")
        FileWriteLine($outFolder & "\" & $playListName,"<Param Name = " & Chr(34) & "IsVBR" & Chr(34) & " Value = " & Chr(34) & "True" & Chr(34) & " />")
        FileWriteLine($outFolder & "\" & $playListName,"<Param Name = " & Chr(34) & "SourceURL" & Chr(34) & " Value = " & Chr(34) & $newFileName & Chr(34) & " />")
        FileWriteLine($outFolder & "\" & $playListName,"<Title >" & $trackTitle & " XXXXXXX</Title>")
        FileWriteLine($outFolder & "\" & $playListName,"<Param Name = " & Chr(34) & "WM/Genre" & Chr(34) & " Value = " & Chr(34) & "XXXXXX" & Chr(34) & " />")
        FileWriteLine($outFolder & "\" & $playListName,"<Ref href = " & Chr(34) & $newFileName & Chr(34) & "/>")
        FileWriteLine($outFolder & "\" & $playListName,"</Entry>")
    EndIf
Wend
FileWriteLine($outFolder & "\" & $playListName,"</Asx>")

;
;Here's where all the files that were written to should be closed, but they aren't!
;

FileClose($infile)
FileChangeDir($outFolder)
FileClose($playListName)

;  W   T   F!!!!


Sleep(1000)



Run("C:\program files\xxxxx.exe",$outFolder)
WinWait("Please register","&Online registration")
If Not WinActive("Please register","&Online registration") Then WinActivate("Please register","&Online registration")
WinWaitActive("Please register","&Online registration")
Send("{TAB}{TAB}{TAB}{ENTER}")
Sleep(3000)
Send("{TAB}")
Send($outFolder & "{ENTER}")
Sleep(500)
Send("{TAB}")
send("{CTRLDOWN}a{CTRLUP}")
send("{ALTDOWN}a{ALTUP}")
Sleep(500)
send("{ALTDOWN}z{ALTUP}")
send("{ALTDOWN}a{ALTUP}")
send("media.wmd")
send("{ALTDOWN}c{ALTUP}{UP}{UP}")
Send("{TAB}{TAB}{CTRLDOWN}l{CTRLUP}")
send("{TAB}{TAB}{TAB}{ENTER}")
AutoItSetOption("WinSearchChildren",1)
WinWaitClose("Creating archive media.wmd")
Send("{ALTDOWN}{F4}{ALTUP}")
DirCopy("S:\DataSessionTemplate\",$outFolder & "\" & $discName & "\")
Run("cmd",$outFolder,@SW_MAXIMIZE)
WinWait("C:\WINDOWS\system32\cmd.exe","")
If Not WinActive("C:\WINDOWS\system32\cmd.exe","") Then WinActivate("C:\WINDOWS\system32\cmd.exe","")
WinWaitActive("C:\WINDOWS\system32\cmd.exe","")
Send("move media.wmd " & $discName & "\assets{ENTER}")
Send("exit{ENTER}")

Exit
Edited by J_Y_C
Link to comment
Share on other sites

Try putting an ExitLoop here:

"XXXXXX" & Chr(34) & " />")
        FileWriteLine($outFolder & "\" & $playListName,"<Ref href = " & Chr(34) & $newFileName & Chr(34) & "/>")
        FileWriteLine($outFolder & "\" & $playListName,"</Entry>")
                exitloop

    EndIf

Wend

Or you can try putting the fileclose in the actual loop. Because, i think (i'm probably wrong), if you don't exit the loop it will still be reading the file. So if you exit, or put fileclose in the actual loop it should get rid of the problem.

Edited by acidfear
Link to comment
Share on other sites

First off, let me say thanks for the help.

Putting in the exitloop in that spot not only doesn't fix the problem, but also prevents it from processing all the files it needs to process. So, I tried putting:

FileClose($outFolder & "\" & $playListName)

in that spot. Still open. Damn.

so I tried just:

FileClose($playListName) in that same spot.

Still no change.

Next move?

Link to comment
Share on other sites

You open the output file but never store the handle. Then you use the path of the file to do all your writing. You need to open the file, store the handle, write to the handle and close the handle. I know you know how to do this correctly because you did so with the input file.

Link to comment
Share on other sites

Just for future refrence, there is an {autoit} {/autoit} tag for autoit code, makes it easier to read.

[quote name='DaleHohm']You have a strange habit of posting error messages that don't match your code.[/quote][quote name='SmOke_N']Forget the learning... straight to the scripting :lol: (laugh.gif)[/quote]

Link to comment
Share on other sites

Ok, if it's not obvious enough, let me first apologize for being a total noob here and annoying the pi55 out of every with my questions. I'm pretty new to this (again, obviously).

But, moving forward, I took valik's advice, and tried to make the process for that file in question more like the other file. He noted that I did it the "right" way in one instance, and I knew at the time that it would probably come back to bite me in the a55 later. So there it is. So now, basically, everything gets routed to an $infile and an $outfile. It still didn't work. Here's what I changed it to:

#include <Process.au3>

$opf = FileOpenDialog("Open Tab Delimited Text file containing playlist", "C:\DataSession\PL\", "Tab Delimited playlist (*.txt)", 0)

$infile = FileOpen($opf, 0)
;MsgBox(0,"QC BOX",$opf)
$outFolder = FileSelectFolder("Choose a folder to output the Data Session:", "")
;MsgBox(0,"QC BOX",$outFolder)
$discName = StringMid($opf,StringInStr($opf,"\", 0, 3)+1,StringLen($opf)-StringInStr($opf,"\", 0, 3)-4)
;MsgBox(0,"QC BOX",$discName)
$playListName = $discName & ".asx"
;MsgBox(0,"QC BOX",$playListName)
 
If $infile = -1 Then
            MsgBox(0, "Error", "Unable to open file.")
            Exit
EndIf
 
$outFile = $outFolder & "\" & $playListName
 
FileOpen($outFile, 2)
FileWriteLine($outFile,"<Asx Version = " & Chr(34) & "3.0" & Chr(34) & ">")
FileWriteLine($outFile,"<Param Name = " & Chr(34) & "AllowShuffle" & Chr(34) & " Value = " & Chr(34)  "yes" & Chr(34) & " />")
FileWriteLine($outFile,"<Title>" & $discName & "</Title>")
 
$doloop = 1
While $doloop = 1
            $line = FileReadLine($infile)
            If @error = -1 Then ExitLoop
            IF StringIsSpace($line) Then 
                        $doloop = 0
                        ExitLoop
            Else
                        $lineArray = StringSplit($line,"     ",1)
                        $trackNumber = $lineArray[1]
                        $trackTitle = $lineArray[2]
                        $trackArtist = $lineArray[3]
                        $newFileName = $discName & "-" & $trackNumber & ".wmv"
                                    IF StringIsDigit(StringLower(StringLeft($trackTitle,1))) Then
                                                $trackPath = "S:\XXXXX\XXXXX\#\"
                                    Else
                                                $trackPath = "S:\WXXXXX\XXXXXX\" & StringLower(StringLeft($trackTitle,1)) & "\"
                                    EndIf
                        FileCopy(StringReplace($trackPath & StringLeft($trackTitle,6) & "*XXXXXX" & StringLeft($trackArtist,6)," ","_") & "*.wmv", $outFolder & "\" & $newFileName)
                        FileWriteLine($outFile,"<Entry>")
                        FileWriteLine($outFile,"<Author >" & $trackArtist & "</Author>")
                        FileWriteLine($outFile,"<Copyright >XXXXXX</Copyright>")
                        FileWriteLine($outFile,"<Param Name = " & Chr(34) & "Description" & Chr(34) & " Value = " & Chr(34) & "XXXXXXX" & Chr(34) & "/>")
                        FileWriteLine($outFile,"<Param Name = " & Chr(34) & "FileType" & Chr(34) & " Value = " & Chr(34) & "wmv" & Chr(34) & " />")
                        FileWriteLine($outFile,"<Param Name = " & Chr(34) & "Is_Protected" & Chr(34) & " Value = " & Chr(34) & "False" & Chr(34) & " />")
                        FileWriteLine($outFile,"<Param Name = " & Chr(34) & "IsVBR" & Chr(34) & " Value = " & Chr(34) & "True" & Chr(34) & " />")
                        FileWriteLine($outFile,"<Param Name = " & Chr(34) & "SourceURL" & Chr(34) & " Value = " & Chr(34) & $newFileName & Chr(34) & " />")
                        FileWriteLine($outFile,"<Title >" & $trackTitle & " XXXXXXX</Title>")
                        FileWriteLine($outFile,"<Param Name = " & Chr(34) & "WM/Genre" & Chr(34) & " Value = " & Chr(34) & "XXXXX" & Chr(34) & " />")
                        FileWriteLine($outFile,"<Ref href = " & Chr(34) & $newFileName & Chr(34) & "/>")
                        FileWriteLine($outFile,"</Entry>")
            EndIf
Wend

;
;Here's the spot where I close the $infile and the $outFile. And yes, it also annoys me that I didn't
;uniformly name my variables. I will fix that later.

FileWriteLine($outFile,"</Asx>")
FileClose($infile)
FileClose($outFile)
Sleep(1000)

;and this didn't work either. The compression program keeps telling me that the file
;linked to $outfile is still in use.

Run("C:\xxxxxxxx.exe",$outFolder)
WinWait("Please register","&Online registration")
If Not WinActive("Please register","&Online registration") Then WinActivate("Please register","&Online registration")
WinWaitActive("Please register","&Online registration")
Send("{TAB}{TAB}{TAB}{ENTER}")
Sleep(3000)
Send("{TAB}")
Send($outFolder & "{ENTER}")
Sleep(500)
Send("{TAB}")
send("{CTRLDOWN}a{CTRLUP}")
send("{ALTDOWN}a{ALTUP}")
Sleep(500)
send("{ALTDOWN}z{ALTUP}")
send("{ALTDOWN}a{ALTUP}")
send("media.wmd")
send("{ALTDOWN}c{ALTUP}{UP}{UP}")
Send("{TAB}{TAB}{CTRLDOWN}l{CTRLUP}")
send("{TAB}{TAB}{TAB}{ENTER}")
AutoItSetOption("WinSearchChildren",1)
WinWaitClose("Creating archive media.wmd")
Send("{ALTDOWN}{F4}{ALTUP}")
DirCopy("S:\DataSessionTemplate\",$outFolder & "\" & $discName & "\")
Run("cmd",$outFolder,@SW_MAXIMIZE)
WinWait("C:\WINDOWS\system32\cmd.exe","")
If Not WinActive("C:\WINDOWS\system32\cmd.exe","") Then WinActivate("C:\WINDOWS\system32\cmd.exe","")
WinWaitActive("C:\WINDOWS\system32\cmd.exe","")
Send("move media.wmd " & $discName & "\assets{ENTER}")
Send("exit{ENTER}")
Exit
Edited by J_Y_C
Link to comment
Share on other sites

$outFile = $outFolder & "\" & $playListName

FileOpen($outFile, 2)

FileWriteLine($outFile,"<Asx Version = " & Chr(34) & "3.0" & Chr(34) & ">")

The start of your problem is here. You open the file, do not store the handle and proceed to use the path to the file in all subsequent calls to FileWriteLine().

How did you actually make it behave like the first file? All you did was use a variable to hold the entire path to the file instead of using two separate variables. That's not making it behave like the first file.

Link to comment
Share on other sites

The start of your problem is here. You open the file, do not store the handle and proceed to use the path to the file in all subsequent calls to FileWriteLine().

How did you actually make it behave like the first file? All you did was use a variable to hold the entire path to the file instead of using two separate variables. That's not making it behave like the first file.

Valik, thanks for continuing to help. I thought I needed to actually declare a FileOpen on the $outfile to get it to behave like the $infile. So, apparently, I didn't do what I thought I was doing.

I am currently taking a Java course, but that's the extent of my education in programming (but not my education overall, but that's neither here nor there). So, here's where my confusion lies, and this may be due entirely to my noobiness.

The outfile is a windows media playlist (.asx) file, and the header to the file is written before the loop, the items in the loop, and then closes after the loop. How do I open a file that doesn't exist yet? Honestly, that's what I thought I was doing by doing the fileopen on the $outfile variable. And I had to create the path and filename from other variables. I don't want to explicitly open it, as the $outfile name will be different every time.

And on a side note, and I hope I don't get booted from this forum for asking this, but what is a handle?

Link to comment
Share on other sites

How on earth did you manage to write the code for $infile working perfectly but be completely clueless to how you did it? Go back to whatever example or reference material you used to get $infile working and break down the various components of what is going on to figure it out.

Link to comment
Share on other sites

Ok, if it's not obvious enough, let me first apologize for being a total noob here and annoying the pi55 out of every with my questions. I'm pretty new to this (again, obviously).

But, moving forward, I took valik's advice, and tried to make the process for that file in question more like the other file. He noted that I did it the "right" way in one instance, and I knew at the time that it would probably come back to bite me in the a55 later. So there it is. So now, basically, everything gets routed to an $infile and an $outfile. It still didn't work. Here's what I changed it to:

CODE

#include <Process.au3>

$opf = FileOpenDialog("Open Tab Delimited Text file containing playlist", "C:\DataSession\PL\", "Tab Delimited playlist (*.txt)", 0)

$infile = FileOpen($opf, 0)

;MsgBox(0,"QC BOX",$opf)

$outFolder = FileSelectFolder("Choose a folder to output the Data Session:", "")

;MsgBox(0,"QC BOX",$outFolder)

$discName = StringMid($opf,StringInStr($opf,"\", 0, 3)+1,StringLen($opf)-StringInStr($opf,"\", 0, 3)-4)

;MsgBox(0,"QC BOX",$discName)

$playListName = $discName & ".asx"

;MsgBox(0,"QC BOX",$playListName)

If $infile = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

$outFile = $outFolder & "\" & $playListName

FileOpen($outFile, 2)

FileWriteLine($outFile,"<Asx Version = " & Chr(34) & "3.0" & Chr(34) & ">")

FileWriteLine($outFile,"<Param Name = " & Chr(34) & "AllowShuffle" & Chr(34) & " Value = " & Chr(34) "yes" & Chr(34) & " />")

FileWriteLine($outFile,"<Title>" & $discName & "</Title>")

$doloop = 1

While $doloop = 1

$line = FileReadLine($infile)

If @error = -1 Then ExitLoop

IF StringIsSpace($line) Then

$doloop = 0

ExitLoop

Else

$lineArray = StringSplit($line," ",1)

$trackNumber = $lineArray[1]

$trackTitle = $lineArray[2]

$trackArtist = $lineArray[3]

$newFileName = $discName & "-" & $trackNumber & ".wmv"

IF StringIsDigit(StringLower(StringLeft($trackTitle,1))) Then

$trackPath = "S:\XXXXX\XXXXX\#\"

Else

$trackPath = "S:\WXXXXX\XXXXXX\" & StringLower(StringLeft($trackTitle,1)) & "\"

EndIf

FileCopy(StringReplace($trackPath & StringLeft($trackTitle,6) & "*XXXXXX" & StringLeft($trackArtist,6)," ","_") & "*.wmv", $outFolder & "\" & $newFileName)

FileWriteLine($outFile,"<Entry>")

FileWriteLine($outFile,"<Author >" & $trackArtist & "</Author>")

FileWriteLine($outFile,"<Copyright >XXXXXX</Copyright>")

FileWriteLine($outFile,"<Param Name = " & Chr(34) & "Description" & Chr(34) & " Value = " & Chr(34) & "XXXXXXX" & Chr(34) & "/>")

FileWriteLine($outFile,"<Param Name = " & Chr(34) & "FileType" & Chr(34) & " Value = " & Chr(34) & "wmv" & Chr(34) & " />")

FileWriteLine($outFile,"<Param Name = " & Chr(34) & "Is_Protected" & Chr(34) & " Value = " & Chr(34) & "False" & Chr(34) & " />")

FileWriteLine($outFile,"<Param Name = " & Chr(34) & "IsVBR" & Chr(34) & " Value = " & Chr(34) & "True" & Chr(34) & " />")

FileWriteLine($outFile,"<Param Name = " & Chr(34) & "SourceURL" & Chr(34) & " Value = " & Chr(34) & $newFileName & Chr(34) & " />")

FileWriteLine($outFile,"<Title >" & $trackTitle & " XXXXXXX</Title>")

FileWriteLine($outFile,"<Param Name = " & Chr(34) & "WM/Genre" & Chr(34) & " Value = " & Chr(34) & "XXXXX" & Chr(34) & " />")

FileWriteLine($outFile,"<Ref href = " & Chr(34) & $newFileName & Chr(34) & "/>")

FileWriteLine($outFile,"</Entry>")

EndIf

Wend

;

;Here's the spot where I close the $infile and the $outFile. And yes, it also annoys me that I didn't

;uniformly name my variables. I will fix that later.

FileWriteLine($outFile,"</Asx>")

FileClose($infile)

FileClose($outFile)

Sleep(1000)

;and this didn't work either. The compression program keeps telling me that the file

;linked to $outfile is still in use.

Run("C:\xxxxxxxx.exe",$outFolder)

WinWait("Please register","&Online registration")

If Not WinActive("Please register","&Online registration") Then WinActivate("Please register","&Online registration")

WinWaitActive("Please register","&Online registration")

Send("{TAB}{TAB}{TAB}{ENTER}")

Sleep(3000)

Send("{TAB}")

Send($outFolder & "{ENTER}")

Sleep(500)

Send("{TAB}")

send("{CTRLDOWN}a{CTRLUP}")

send("{ALTDOWN}a{ALTUP}")

Sleep(500)

send("{ALTDOWN}z{ALTUP}")

send("{ALTDOWN}a{ALTUP}")

send("media.wmd")

send("{ALTDOWN}c{ALTUP}{UP}{UP}")

Send("{TAB}{TAB}{CTRLDOWN}l{CTRLUP}")

send("{TAB}{TAB}{TAB}{ENTER}")

AutoItSetOption("WinSearchChildren",1)

WinWaitClose("Creating archive media.wmd")

Send("{ALTDOWN}{F4}{ALTUP}")

DirCopy("S:\DataSessionTemplate\",$outFolder & "\" & $discName & "\")

Run("cmd",$outFolder,@SW_MAXIMIZE)

WinWait("C:\WINDOWS\system32\cmd.exe","")

If Not WinActive("C:\WINDOWS\system32\cmd.exe","") Then WinActivate("C:\WINDOWS\system32\cmd.exe","")

WinWaitActive("C:\WINDOWS\system32\cmd.exe","")

Send("move media.wmd " & $discName & "\assets{ENTER}")

Send("exit{ENTER}")

Exit

When you open a file in AutoIT with FileOpen(), the function returns a HANDLE to the file. That handle needs to be saved for future reference. So you should never see FileOpen() at the start of a code line. It should be preceded by the variable that will save the handle for you: $hMyFile = FileOpen("C:\Temp\MyFile.txt", 2). All the future references to that file will be by the $hMyFile variable, not the file name.

In your code, you open a file correctly with: $infile = FileOpen($opf, 0)

You then correctly reference the file with: $line = FileReadLine($infile)

And you correctly close it with: FileClose($infile)

All done correctly using the handle $infile, not the filename in $opf

But then, you open incorrectly with: FileOpen($outFile, 2)

Incorrect, because you didn't save the handle. Should be: $hOutFile = FileOpen($outFile, 2)

Then your reference to the output file should be by handle: FileWriteLine($hOutFile,"</Asx>") vice FileWriteLine($outFile,"</Asx>")

And you should close it by handle, not file name: FileClose($hOutFile) vice FileClose($outFile)

The help file entry for FileOpen() does not say it, but you see in the entry for FileWrite() the following note:

If a filename is given rather than a file handle, the file will be opened and closed during the function call. For parsing large text files this will be much slower than using filehandles. However, filename will be created if it does not already exist.

Note: Do not mix filehandles and filenames, i.e., don't FileOpen a file and then use a filename in this function. Either use filehandles or filenames in your routines, not both.

:whistle:
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...