Copy link to clipboard
Copied
Hi
I have a filepath in a datagrid's dataprovider (hidden from user), and visible to the user in the datagrid I have the file name minus the extension - "myFile"
D:\AA\DAT\myFile.txt (hidden, but in dataprovider)
myFile (visible in datagrid)
The datagrid is editable, and when the user edits the entry 'myFile' to whatever eg 'thisIsTheNewName', I want to use moveTo to rename the file on the hard drive to the new file name.
I know how to do it if the file is in one of the AIR diretories eg desktopDirectory, but is it possible to do it if the file is anywhere on the hard drive?
If so, what do I put in the place of desktopDirectory to make AIR successfuly use a native path for the destinationfile? I think nativePath is it, but I cannot get it right:
var sourceFile:File = new File();
sourceFile.nativePath = "D:\AA\DAT\35660.txt";
var destination:File = new File();
destination.nativePath = "D:\AA\DAT\newFileName.txt";
try
{
sourceFile.moveTo(destination, true);
}
catch (error:Error)
{
trace("Error:" + error.message);
}
gives: Error #3003: File or directory does not exist.
Thanks guys
Copy link to clipboard
Copied
You're putting a String in the place moveTo expects a FileReference. Unfortuantely FileReference receives its location data by spawning a dialog, asking the user to select a folder. That's not very silent.
What you can do is create and delete files. Being this is just a text file, open a new FileStream to the file you wish to write, write the contents of the source file, then use the File.deleteFile method to remove the old file.
Copy link to clipboard
Copied
Thanks Sinious
Your reply lead me to practice using file, and to look for a way to also rename non txt type files in an explicet dir:
var mySource:File = File.userDirectory;
mySource = mySource.resolvePath("D://mp3s/myMP3.mp3");
var myDestination:File = File.userDirectory;
myDestination = myDestination.resolvePath("D://mp3s/myNewMP3Name.mp3");
if (mySource.exists)
{
mySource.moveToAsync(myDestination);
}
Copy link to clipboard
Copied
Have you verified if you can specify using a File in replace of FileReference? File is a subclass of FileReference so it contains all its methods, I'm just not certain if resolvePath is populating the same properties as when a browse dialog is spawned allowing the user to select a folder.
public function moveToAsync(newLocation:FileReference, overwrite:Boolean = false):void
If it does, great. moveTo and moveToAsync are definitely a way to move a file. Being your file is so small there's virtually no reason to use the async method. If the file is large then using it can be wise. If it's small (like text) or <10MB then it's more advisable to use moveTo synchronously. Then you don't need to specify listeners.
e.g., less code:
var source:File = File.applicationStorageDirectory.resolvePath("my.mp3");
var dest:File = File.resolvePath("D:\\mp3s\\myNewMP3Name.mp3");
try
{
source.moveTo(dest);
}
catch (e:IOErrorEvent)
{
// handle IOErrorEvent (most common)
}
catch (e:SecurityErrorEvent)
{
// handle SecurityErrorEvent
}
catch (e:IOError)
{
// handle IOError
}
catch (e:Error)
{
// handle general Error
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now