Notifications
Clear all

[Closed] How to delete a line from an external file?

 ILS

I have an external txt file that my script accesses to read values for different properties, basically I need to figure out a way to delete a line from that txt file using maxscript.

Any ideas how I can do that?

13 Replies

check out file i/o methods in the mxs reference for ReadLine() and associated methods to find the answer!

 ILS

the problem with that is it can only read and write – I guess I can write a blank space for every character in the line but then how do I get rid of the blank line.

Another method would be to create temp copy of the file and copy line by line to a new file with the exception of the line I want to delete but this seems too cumbersome for a simple function.

oddly enough, that’s what 99% of the editors do when you delete a line… either…

A.
Create a new file
Read a line from the old file
Write that line to the new file
Skip the line you want to delete

or

B.
Overwrite the existing file in-place
Read a line from memory (containing the original file entirely)
Write the line to the new file
Skip the line you want to delete

There’s very few (if any, these days), editors that will work on a file in-place on the harddisk via, say, the description data or metadata in the filesystem (essentially, the line still exists, but as the filesystem contains no record for data including the data for that line, it’s essentially gone from the file.

The only optimization you -can- do is use .NET – it’s still the same procedure, but it can be a bit faster.

 ILS

that is unfortunate that maxscript doesn’t have a built in function for this.

what would be the method for using .net to do this? I’m not really familiar with .net do you know any tutorials.

probably the fastest method (takes a bit of memory as each line is a string in an array)…


-- usage:
--   deleteLine <string>filename <int>lineNumberToDelete
-- result:
--   line 'lineNumberToDelete' is removed from file 'filename'
-- notes:
--   NO backups are made! Use at your own risk
fn deleteLine f linenum = (
	local lines = (dotNetClass "System.IO.File").ReadAllLines f
	deleteItem lines linenum
	(dotNetClass "System.IO.File").WriteAllLines f lines
)

 ILS

Thanks a lot for your help I’ll try this out. I’m not as worried about memory I run win 64 with 8 Gb ram so I have some to spare unfortunately what I don’t have to spare is time which is why I’m trying to write scripts. Thanks again.

 ILS

It works – well sort of.

   I am actually having problems closing the file after I do 
myfile = openFile (mypath)
close myfile  
 returns OK
   
   but I still have issues accessing it. 
   Your script works if I don't do openFile on the file prior to running it.

it gives me this:

MAXScript Rollout Handler Exception: -- Runtime error: dotNet runtime exception: The process cannot access the file 'C:\..\..\..' because it is being used by another process.

I wonder if I should not use maxscript openFIle method and switch to .net for external file handling. What do you think?

well, with the function given, you wouldn’t use openFile at all – just feed the function the (string) path to the file, not a maxscript file handle

If you’re using openFile to open the file for some other operations before removing the lines, you would have to close the file in maxscript first. Alternatively, as you said, you may be able to do all of the processing using .NET … depends a bit on what you’re doing

 ILS

yeah I know about the fact that I don’t need the openFile bit to use the .net script you described above.

Basically the external file is for listing camera names and their respective frame ranges and any other information I need. One part of the script collects all cameras in the scene and sets up a default setting for each and writes it to an external file – there are also other helper objects created with respect to each camera. Because there are so many things that depend on the camera in a scene, if I want to get rid of one or more cameras I also need to delete the assosiated objects and external file references – all of this is prone to human error. So that is basically why I need to figure out a way to delete lines from external files.

I use openFile to read the external file to get information from it. After opening I do use the close function as it is described in max help but the permissions are still not relinquised from either max or the script (i don’t know which), I have to restart max to gain permission to edit the opened file again.

Thanks for all your help thus far.

Page 1 / 2