Jump to content
Sign in to follow this  
YellowLab

Problem with FileWriteLine

Recommended Posts

YellowLab

I did a search for this topic and couldn't find an answer. I am seeing an issue with the following function.

Func _GenerateCueSheet()
    Local $nSearch=-1, $nTrack=1, $i
    Local $sFile, $sToWrite
    Local $hFile
    Local $arFile, $arTemp, $arTemp2

    $nSearch = FileFindFirstFile($sAudibleDirectory & "\*.aa")
    ShellExecute("cmd.exe")

    Do
        Sleep(100)
    Until WinExists("cmd")
    $hMain=WinGetHandle("cmd")

    While 1
        FileDelete($sAudibleDirectory&'\temp.txt')
        $sFile=FileFindNextFile($nSearch)
        If @error Then ExitLoop
        WinActivate($hMain)
        Send(@ScriptDir&'\AudibleChapters.exe "'&$sAudibleDirectory&'\'&$sFile&'" > "'&$sAudibleDirectory&'\temp.txt"')
        Send("{ENTER}")
        Do
            Sleep(100)
        Until FileExists($sAudibleDirectory&'\temp.txt')

        $hFile=FileOpen($sAudibleDirectory&'\'&StringReplace($sFile,'.aa','.cue'),2)
        FileWriteLine($hFile,'FILE "'&StringReplace($sFile,'.aa','')&'.MP3" MP3')

        _FileReadToArray($sAudibleDirectory&'\temp.txt',$arFile)
        For $i=0 To Ubound($arFile)-1
            If StringRegExp($arFile[$i],'\d\d:\d\d:\d\d:\d\d\d') Then
                $arTemp=StringSplit($arFile[$i],":")
                If StringInStr($arTemp[1],"Total") Then ExitLoop

                If $nTrack<10 Then
                    FileWriteLine($hFile,"TRACK 0"&$nTrack&" AUDIO")
                Else
                    FileWriteLine($hFile,"TRACK "&$nTrack&" AUDIO")
                EndIf

                $nTrack+=1

                FileWriteLine($hFile,'TITLE "'&$arTemp[1]&'"')

                $arTemp=StringRegExp($arFile[$i],'\d\d:\d\d:\d\d:\d\d\d',1) ;$arTemp[0] has the index
                $arTemp2=StringSplit($arTemp[0],':')
                If $nTrack=2 Then
                    FileWriteLine($hFile,"INDEX 01 "&$arTemp2[1]*60+$arTemp2[2]&':'&$arTemp2[3]&':00')
                Else
                    FileWriteLine($hFile,"INDEX 01 "&$arTemp2[1]*60+$arTemp2[2]&':'&$arTemp2[3]-1&':00') ;subtract 1 second for offset
                EndIf
            EndIf
        Next
        FileClose($hFile)
    WEnd
    WinActivate($hMain)
    Send("exit{ENTER}")
EndFunc

The function uses another program to create a text file containing chapter locations in an audible file. The function parses the data to create a .cue file. The temporary text file is deleted before the command line is run and then the script pauses until the program creates the file. The file is successfully read to an array, but when FileWriteLine is used, only the first line in the cue is written. I can't figure out why the other lines in the file are not written to the file. If I change the format to create a string and use "FileWrite" everything works as it should:

Func _GenerateCueSheet()
    Local $nSearch=-1, $nTrack=1, $i
    Local $sFile, $sToWrite
    Local $hFile
    Local $arFile, $arTemp, $arTemp2

    $nSearch = FileFindFirstFile($sAudibleDirectory & "\*.aa")
    ShellExecute("cmd.exe")

    Do
        Sleep(100)
    Until WinExists("cmd")
    $hMain=WinGetHandle("cmd")

    While 1
        FileDelete($sAudibleDirectory&'\temp.txt')
        $sFile=FileFindNextFile($nSearch)
        If @error Then ExitLoop
        WinActivate($hMain)
        Send(@ScriptDir&'\AudibleChapters.exe "'&$sAudibleDirectory&'\'&$sFile&'" > "'&$sAudibleDirectory&'\temp.txt"')
        Send("{ENTER}")
        Do
            Sleep(100)
        Until FileExists($sAudibleDirectory&'\temp.txt')

        ;$hFile=FileOpen($sAudibleDirectory&'\'&StringReplace($sFile,'.aa','.cue'),2)
        ;FileWriteLine($hFile,'FILE "'&StringReplace($sFile,'.aa','')&'.MP3" MP3')
$sToWrite='FILE "'&StringReplace($sFile,'.aa','')&'.MP3" MP3'
        _FileReadToArray($sAudibleDirectory&'\temp.txt',$arFile)
        For $i=0 To Ubound($arFile)-1
            If StringRegExp($arFile[$i],'\d\d:\d\d:\d\d:\d\d\d') Then
                $arTemp=StringSplit($arFile[$i],":")
                If StringInStr($arTemp[1],"Total") Then ExitLoop

                If $nTrack<10 Then
                    ;FileWriteLine($hFile,"TRACK 0"&$nTrack&" AUDIO")
$sToWrite&=@CRLF&"TRACK 0"&$nTrack&" AUDIO"
                Else
                    ;FileWriteLine($hFile,"TRACK "&$nTrack&" AUDIO")
$sToWrite&=@CRLF&"TRACK "&$nTrack&" AUDIO"
                EndIf

                $nTrack+=1

                ;FileWriteLine($hFile,'TITLE "'&$arTemp[1]&'"')
$sToWrite&=@CRLF&'TITLE "'&$arTemp[1]&'"'

                $arTemp=StringRegExp($arFile[$i],'\d\d:\d\d:\d\d:\d\d\d',1) ;$arTemp[0] has the index
                $arTemp2=StringSplit($arTemp[0],':')
                If $nTrack=2 Then
                    ;FileWriteLine($hFile,"INDEX 01 "&$arTemp2[1]*60+$arTemp2[2]&':'&$arTemp2[3]&':00')
$sToWrite&=@CRLF&"INDEX 01 "&$arTemp2[1]*60+$arTemp2[2]&':'&$arTemp2[3]&':00'
                Else
                    ;FileWriteLine($hFile,"INDEX 01 "&$arTemp2[1]*60+$arTemp2[2]&':'&$arTemp2[3]-1&':00') ;subtract 1 second for offset
$sToWrite&=@CRLF&"INDEX 01 "&$arTemp2[1]*60+$arTemp2[2]&':'&$arTemp2[3]-1&':00'
                EndIf
            EndIf
        Next
$hFile=FileOpen($sAudibleDirectory&'\'&StringReplace($sFile,'.aa','.cue'),2)
FileWrite($hFile,$sToWrite)
        FileClose($hFile)
    WEnd
    WinActivate($hMain)
    Send("exit{ENTER}")
EndFunc

While the FileWrite does work, I am curious why the FileWriteLine doesn't. I have attached a sample output text file that is generated by the command line program.

temp.txt


You can't see a rainbow without first experiencing the rain.

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.