Sign in to follow this  
Followers 0
StooJ

[SOLVED] FileWrite Preserving linefeeds

7 posts in this topic

#1 ·  Posted (edited)

Hi all

My sister has recently moved her music files from one location to another, and her playlists all have absolute paths to her music.

I thought I'd write her a quick script that scanned her playlists folder and changed all of them from absolute to relative paths.

Simple, or so I thought.

I've sucked the m3u file into an autoit variable, run a couple of string-related tasks on them and then was hoping to overwrite the original file with the corrected contents of my variable.

Unfortunately FileWrite doesn't preserve the line feeds in the variable. I hadn't noticed FileWrite (& FileWriteLine) don't preserve line feeds. Is there a way around this? Must I use an array instead? I'm scared <_<

Many thanks

Edited by StooJ

Share this post


Link to post
Share on other sites



FileWriteline?

Share this post


Link to post
Share on other sites

Just read the whole file content into a variable and do a stringReplace for the path after which you do a filewrite for the variable back to the file.

this will preserve all CRLF's

<_<


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Just read the whole file content into a variable and do a stringReplace for the path after which you do a filewrite for the variable back to the file.

this will preserve all CRLF's

<_<

I'm pretty sure this is what I am doing - perhaps someone could point out where I'm going wrong? Here's the relevant code:

Do
    $FileHandle = FileOpen ( $PlaylistsPath & "\" & $Filelist[$n] , 0 )
    $TextBody = FileRead ( $PlaylistsPath & "\" & $Filelist[$n] )
    FileClose ( $FileHandle )
    $numreplacements = 0        ; Reset the counters
    $badstring = 0
    MsgBox ( 0 , "Original" , $TextBody )
    $TextBody = StringReplace ( $TextBody , "C:\Documents and Settings\user\My Documents\My Music\" , "" )
    $numreplacements = @extended
    $TextBody = StringReplace ( $TextBody , "\Documents and Settings\user\My Documents\My Music\" , "" )
    $numreplacements = @extended + $numreplacements
    $TextBody = StringReplace ( $TextBody , "\Music\" , "" )
    $numreplacements = @extended + $numreplacements
    If StringInStr ( $TextBody , "temporary folder" ) > 0 Then
        $badstring = 1
    EndIf
    If $badstring > 0 AND $numreplacements = 0 Then
        MsgBox ( 0 , "Error" , "There's something funny about the '" & $Filelist[$n] & "' playlist." & @CRLF & "The songs aren't in a sensible place. You'll need to do this one yourself - sorry." & @CRLF & "I'll show you what the playlist looks like. Maybe you can work it out." )
        MsgBox ( 0 , $Filelist[$n] , "Here's the playlist file in full. Maybe you can spot what's wrong?" & @CRLF & "It's probably something like the files not being in your music folder." & @CRLF & "Is the playlist looking in your temporary folder?" & @CRLF & @CRLF & $TextBody )
    Else
        $TextBody = StringReplace ( $TextBody , @LF , Chr ( 13 ) & "..\" )      ; Added Chr(13) to try and force a CR to be passed to file
        $TextBody = StringReplace ( $TextBody , "..\#" , "#" )
        $TextBody = StringTrimRight ( $TextBody , 3 )
        MsgBox ( 0 , "Trimmed" , $TextBody )
        $TextBody = StringAddCR ( $TextBody )                           ; Again, added to try and preserve line feeds
        $FileHandle = FileOpen ( $PlaylistsPath & "\" & $Filelist[$n] , 2 )
        FileWriteLine ( $FileHandle , $TextBody )
        FileClose ( $FileHandle )
    EndIf
    $n = $n + 1
Until $n = 1

Here's a before & after from a playlist:

Before

#EXTM3U
#EXTINF:223,Mad'house - Like A Prayer
C:\Documents and Settings\user\My Documents\My Music\Miscellaneous Stuff\Now Dance 2003\Like A Prayer.mp3
#EXTINF:218,Dirty Vegas - Days Go By
C:\Documents and Settings\user\My Documents\My Music\Miscellaneous Stuff\Now Dance 2003\Days Go By.mp3
#EXTINF:211,The Source Featuring Candi Staton - You got the love
C:\Documents and Settings\user\My Documents\My Music\Miscellaneous Stuff\Now That's What I Call Music! 36 (Disc 2)\You got the love.mp3
#EXTINF:227,Sash! - Encore une fois
C:\Documents and Settings\user\My Documents\My Music\Miscellaneous Stuff\Now That's What I Call Music! 36 (Disc 2)\Encore une fois.mp3
#EXTINF:298,The Prodigy - Out Of Space
C:\Documents and Settings\user\My Documents\My Music\The Prodigy\Experience Expanded\Out Of Space.mp3
#EXTINF:198,Oasis - ******' In The Bushes
C:\Documents and Settings\user\My Documents\My Music\Oasis\Standing On The Shoulder Of Giants\******' In The Bushes.mp3
#EXTINF:230,Scooter - Posse
C:\Documents and Settings\user\My Documents\My Music\Singles\Scooter\Posse.mp3
#EXTINF:248,Zombie Nation - Kernkraft 400 (Radio Edit)
C:\Documents and Settings\user\My Documents\My Music\Miscellaneous Stuff\Essential Dance 2000\Kernkraft 400 (Radio Edit).mp3
#EXTINF:145,Sister Nancy 192 - Bam Bam
C:\Documents and Settings\user\My Documents\My Music\Scratch Perverts\Badmeaningood\Bam Bam.mp3
#EXTINF:195,Minnie Ripperton 192 - Les Fleurs
C:\Documents and Settings\user\My Documents\My Music\Scratch Perverts\Badmeaningood\Les Fleurs.mp3
#EXTINF:375,Massive Attack - Angel
C:\Documents and Settings\user\My Documents\My Music\Film Soundtracks\Snatch\Angel.mp3
#EXTINF:212,Mirwais - Disco Science
C:\Documents and Settings\user\My Documents\My Music\Film Soundtracks\Snatch\Disco Science.mp3
#EXTINF:519,Faithless - Insomnia
C:\Documents and Settings\user\My Documents\My Music\Faithless\Reverence\Insomnia.mp3
#EXTINF:342,The Prodigy - Smack My ***** Up
C:\Documents and Settings\user\My Documents\My Music\The Prodigy\The Fat Of The Land\Smack My ***** Up.mp3
#EXTINF:237,Daft Punk - Digital Love
C:\Documents and Settings\user\My Documents\My Music\Miscellaneous Stuff\Now 50 (Disc 2)\Digital Love.mp3
#EXTINF:223,Groove Armada - Superstylin'
C:\Documents and Settings\user\My Documents\My Music\Miscellaneous Stuff\Now 50 (Disc 2)\Superstylin_.mp3

After: (Ironically, IP Board's rich text editor recognises the CRs and put them in. I've replaced them with *s to show what it looks like in notepad.

#EXTM3U**#EXTINF:223,Mad'house - Like A Prayer**..\Miscellaneous Stuff\Now Dance 2003\Like A Prayer.mp3**#EXTINF:218,Dirty Vegas - Days Go By**..\Miscellaneous Stuff\Now Dance 2003\Days Go By.mp3**#EXTINF:211,The Source Featuring Candi Staton - You got the love**..\Miscellaneous Stuff\Now That's What I Call Music! 36 (Disc 2)\You got the love.mp3**#EXTINF:227,Sash! - Encore une fois**..\Miscellaneous Stuff\Now That's What I Call Music! 36 (Disc 2)\Encore une fois.mp3**#EXTINF:298,The Prodigy - Out Of Space**..\The Prodigy\Experience Expanded\Out Of Space.mp3**#EXTINF:198,Oasis - ******' In The Bushes**..\Oasis\Standing On The Shoulder Of Giants\******' In The Bushes.mp3**#EXTINF:230,Scooter - Posse**..\Singles\Scooter\Posse.mp3**#EXTINF:248,Zombie Nation - Kernkraft 400 (Radio Edit)**..\Miscellaneous Stuff\Essential Dance 2000\Kernkraft 400 (Radio Edit).mp3**#EXTINF:145,Sister Nancy 192 - Bam Bam**..\Scratch Perverts\Badmeaningood\Bam Bam.mp3**#EXTINF:195,Minnie Ripperton 192 - Les Fleurs**..\Scratch Perverts\Badmeaningood\Les Fleurs.mp3**#EXTINF:375,Massive Attack - Angel**..\Film Soundtracks\Snatch\Angel.mp3**#EXTINF:212,Mirwais - Disco Science**..\Film Soundtracks\Snatch\Disco Science.mp3**#EXTINF:519,Faithless - Insomnia**..\Faithless\Reverence\Insomnia.mp3**#EXTINF:342,The Prodigy - Smack My ***** Up**..\The Prodigy\The Fat Of The Land\Smack My ***** Up.mp3**#EXTINF:237,Daft Punk - Digital Love**..\Miscellaneous Stuff\Now 50 (Disc 2)\Digital Love.mp3**#EXTINF:223,Groove Armada - Superstylin'**..\Miscellaneous Stuff\Now 50 (Disc 2)\Superstylin_.mp3**

(Songs censored :) )

Although the CRs are obviously kind of preserved, they aren't returned the same as they were taken. Notepad doesn't like them and neither does WinAmp.

I've tried FileOpen in default more and in Unicode UTF16 Little Endian. No difference.

Edited by StooJ

Share this post


Link to post
Share on other sites

Notepad will never accept a single CR or LF without the other.

First off, this is a problem:

$FileHandle = FileOpen ( $PlaylistsPath & "\" & $Filelist[$n] , 0 )
$TextBody = FileRead ( $PlaylistsPath & "\" & $Filelist[$n] )
FileClose ( $FileHandle )

Never mix file handles and paths. If you bother to use FileOpen, use the handle for FileRead. Since you are only making a single call then closing it though, just use FileRead and don't bother with open and close.

Could I see an example of what the playlist ought to look like when finished, so I can further help. Just use a couple songs though, don't bother with that many next time.

Share this post


Link to post
Share on other sites

First off, this is a problem:

$FileHandle = FileOpen ( $PlaylistsPath & "\" & $Filelist[$n] , 0 )
$TextBody = FileRead ( $PlaylistsPath & "\" & $Filelist[$n] )
FileClose ( $FileHandle )

Never mix file handles and paths. If you bother to use FileOpen, use the handle for FileRead. Since you are only making a single call then closing it though, just use FileRead and don't bother with open and close.

Quite right. I hadn't spotted that one. Cheers.

Could I see an example of what the playlist ought to look like when finished, so I can further help. Just use a couple songs though, don't bother with that many next time.

Sure, no problem. Here's the output I'm trying to get:

#EXTM3U
#EXTINF:223,Mad'house - Like A Prayer
..\Miscellaneous Stuff\Now Dance 2003\Like A Prayer.mp3
#EXTINF:218,Dirty Vegas - Days Go By
..\Miscellaneous Stuff\Now Dance 2003\Days Go By.mp3
#EXTINF:211,The Source Featuring Candi Staton - You got the love
..\Miscellaneous Stuff\Now That's What I Call Music! 36 (Disc 2)\You got the love.mp3

Share this post


Link to post
Share on other sites

Solved.

Got it eventually, and it was a silly wee mistake (as usual for me <_< )

; snip....
    Else
        $TextBody = StringReplace ( $TextBody , @LF , @LF & "..\" )
        ;MsgBox ( 0 , "Added ..\" , $TextBody )
        $TextBody = StringReplace ( $TextBody , "..\#" , "#" )
        ;MsgBox ( 0 , "Took away ..\#" , $TextBody )
        $TextBody = StringTrimRight ( $TextBody , 5 )       ; Tidy up the extra LF and ..\
        MsgBox ( 0 , "Trimmed" , $TextBody )                ; Show the result to check it's worked properly
        ;$TextBody = StringAddCR ( $TextBody )
        $FileHandle = FileOpen ( $PlaylistsPath & "\" & $Filelist[$n] , 2 )
        FileWriteLine ( $FileHandle , $TextBody )
        FileClose ( $FileHandle )
    EndIf
; snip....

Thanks for having a look guys, I appreciate it always.

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