copying files

    Apr 02 2007 | 6:10 pm
    I also posted this in the jitter subforum as a follow up on a different question, however the people interested in this topic would probably be here, also for building the knowledge database.
    I am trying to copy a file using javascript. however when I can not open the copied file. The player will give me an error, unable to open file.
    What am I doing wrong here?
    myfile = new File(filename, "read");; if (myfile.isopen) { newName = new String(myfile.filename); newName = newName.substring(0, newName.length - 4) + "_x.avi"; newFile = new File(location + newName, "readwrite", myfile.filetype);; while (myfile.eof { newFile.writebytes(myfile.readbytes(1)); } newFile.close(); myfile.close();
    There is probably a fundamental flaw in the while loop or the filetype, however I can not see my mistake.

    • Apr 02 2007 | 8:16 pm
      On 02-avr.-07, at 20:10, Maarten Zeinstra wrote:
      > while (myfile.eof
      Shouldn't it be the other way round?
      _____________________________ Patrick Delges
      Centre de Recherches et de Formation Musicales de Wallonie
    • Apr 03 2007 | 11:50 am
      Yes you are absolutely right, I found that one out also.
      However when I run the script the file gets copied ok, however Max crashes afterwards. Is there more stuff wrong here?
    • Apr 03 2007 | 2:29 pm
      Well I've let go of the javascript solution and went into bash programming :)
      With a simple script you can copy the file to a different location and have more control over the copy. using launchbrowser I can execute it, I think haven't tested that part yet..
      So thanks for your help. If you would like to take a look at the bash file you can mail me. I will not post it here because of the file alteration I also do.
    • Jul 08 2021 | 10:08 am
      I stumbled across the same issue, but I found what was crashing Max. Another issue with Maarten's code is that reading byte per byte is SLOW (like 1s to write a 100kb file). However, reading the whole file at once is not allowed either. Therefore I implemented an intermediate solution: function copyFile(orig_path, dest_path) { orig_file = new File(orig_path, "read");; if(orig_file.isopen) { dest_file = new File(dest_path, "readwrite", orig_file.filetype);; var bytes_in_queue = orig_file.eof; var batch_size; while(bytes_in_queue > 0) { batch_size = Math.min(bytes_in_queue, 10000); dest_file.writebytes(orig_file.readbytes(batch_size)); bytes_in_queue -= batch_size; } } orig_file.close(); dest_file.close(); }
      An issue of this process is that if the destination folder does not exist, it will fail silently. That could be an improvement for future versions of Max. EDIT: instead of using a [js] script, it seems to me that [node.script] is better suited for the job.
    • Jul 09 2021 | 8:04 am
      why not use shell and forget the trouble ? or filemanager - jasch for mac or msdpfs externals win/mac