Jump to content

FileMove() to rename files


Recommended Posts

I'm new to AutoIt, and for my first project I wrote a simple script to convert Winamp .m3u files to be compatible with my Sandisk Sansa m240 mp3 player. All it does is remove directory and drive information from the file entries. The script opens the playlist, creates a temp file in the working directory, and reads each line of the playlist. If the line doesn't contain a backspace, it's written directly to the temp file. If it contains backspaces, it splits the string and writes the last chunk (hopefully the filename!) to the temp file instead of the whole path/filename.

When it finishes, I'd like to replace the playlist with the temp file (if I knew how to easily edit the original file without destroying the information within, that'd be even better, but I don't). However, this code doesn't work (the playlist's handle is $File, while the temp file's handle is $EditFile):

CODE
FileMove($EditFile, $File, 1)

So I thought perhaps I couldn't overwrite the playlist because I had an open handle on it; it also occurred to me that perhaps FileMove() didn't take handles and needed a string, so I tried:

CODE
$Source = StringFormat( '"' & FileGetLongName($EditFile) & '"')

$Dest = StringFormat( '"' & FileGetLongName($File) & '"')

FileClose($File)

FileClose($EditFile)

FileMove($Source, $Dest, 1)

This also failed to work. Nor did just using FileGetLongName(); I tried throwing the StringFormat() in there to add quotations around the string, just in case. No go. Can anybody tell me what I'm doing wrong? I can't use specific paths, because the script is designed to accept playlists that are dropped onto it; the location varies.

On a side note, the manual says that using FileReadLine() to step through each line of a file is inefficient because AutoIt has to read the entire file up to that line each time. So what's the preferred method? The only thing I can think of would be to read the entire file into memory using FileRead(), then search the resulting string for CR. Read the number of chars up to the CR into $foo, trim them from the main string, then treat $foo the same as the output from FileReadLine(). Rinse and repeat 'til the end of the file. Is that at least on the right track?

Link to comment
Share on other sites

I tried:

CODE
$Source = StringFormat( '"' & FileGetLongName($EditFile) & '"')

$Dest = StringFormat( '"' & FileGetLongName($File) & '"')

FileClose($File)

FileClose($EditFile)

FileMove($Source, $Dest, 1)

Filemove needs strings as the first two arguments. It doen't use file handles.

So your FileMove($EditFile, $File, 1) should work. Try adding some debugging code to see what those two variables are.

For example, before the FileMove line add

consolewrite('$EditFile = ' & $EditFile & ', $File = ' & $File & @LF)

On a side note, the manual says that using FileReadLine() to step through each line of a file is inefficient because AutoIt has to read the entire file up to that line each time. So what's the preferred method? The only thing I can think of would be to read the entire file into memory using FileRead(), then search the resulting string for CR. Read the number of chars up to the CR into $foo, trim them from the main string, then treat $foo the same as the output from FileReadLine(). Rinse and repeat 'til the end of the file. Is that at least on the right track?

If you open the file and leave it open, then every time you read a line it will be the next line. Then when you have finished you can close the file.

$hF = FileOPen('C:\somefile.txt',0)
while 1
  $nextline = FileReadLine($hF)
  if @error then exitloop;have come to end of file

.. do something with $nextline
wend
FileClose($hF)

The whole text can be read into a variable with FileRead in one hit.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Also, I don't see the need for StringFormat()

Compare:

$Source =StringFormat( '"' & FileGetLongName(@ScriptDir)& '"')
MsgBox(0,"",$Source)

$Source =StringFormat(FileGetLongName(@ScriptDir))
MsgBox(0,"",$Source)

$Source =FileGetLongName(@ScriptDir)
MsgBox(0,"",$Source)
Link to comment
Share on other sites

Martin:

I put in some debug text where you suggested. Turns out instead of a path and filename, the handles are just returning their number (1 and 2, respectively). Apparently you can't use a file handle in FileGetLongName() either. I thought of a workaround; just use the CmdLine[1] again to get the path to the playlist, and use the explicit name for the temp file, since it's hardcoded anyway. The point is moot, though, because I just went back and rewrote the entire script to work the way I described at the end of my first post. Easier than I thought, actually. I should have just done that from the beginning. Although now I have a weird bug where it's inserting blank lines where it shouldn't be...

If you open the file and leave it open, then every time you read a line it will be the next line.

So this means the warning in the manual only pertains to situations in which you supply a line number? Cool.

Nahuel:

I used the StringFormat() to place the path/filename returned by FileGetLongName() inside quotations, because I figured FileMove() would require them. I ran the three samples you gave, and I see why it's a bad idea; it didn't even occur to me that StringFormat() would fiddle with the colon and backslashes. So the following code would do what I was trying to do:

CODE
$Source =FileGetLongName(@ScriptDir)

$NewSource = '"' & $Source & '"'

Thanks for the heads up.
Link to comment
Share on other sites

$Source =FileGetLongName(@ScriptDir)
$NewSource = '"' & $Source & '"'
If you are gonna use $NewSource as a parameter for FileMove() then I don't think this will work. You see, FileGetLongName() already returns a string!

If you do this:

$Source =FileGetLongName(@ScriptDir)
$NewSource = '"' & $Source & '"'
FileMove($NewSource,"dest")

It's like doing:

FileMove(""C:\source"","dest")

because the quote marks that you add will be added to the string that FileGetLongName() returns.

Doing this:

$Source =FileGetLongName(@ScriptDir)
FileMove($Source,"dest")

Should work just alright.

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...