Copy Files

Caché

Hi Guys,

I'm looking to Remove file from a location to another similar to CUT & Past function, I'm using this :

 s file=##class(%File).%New()
 d file.CopyFile(File,ArchivePath)
 d file.Delete(File)

and the copying is happening but not the Deleting, and help pls?

Thanks

  • 0
  • 0
  • 716
  • 14
  • 2

Answers

doc say:

classmethod Delete(filename As %String, Output return As %Integer) as %Integer
Deletes the file filename. Returns true if it succeeds and false otherwise. Pass return by reference to obtain the low level return value in case of errors

 

so try:

set success = file.Delete(File, .return)

to see the reason for failing

 

Is there a table for the cross-reference to resolve the 'low level return values'?  (I forced a failure, return = '-2', but not sure what that means)

It's a value returned from OS:
So this might be platform dependent.
the call is $zu(140,5,$select($$$isVMS&&(%file'[";"):%file_";*",1:%file))

the part for VMS deletes all versions of the file if the name is not terminated by " ;"

First of all, you're calling class methods, so you don't need a file object. Instead of:

s file=##class(%File).%New()
d file.CopyFile(File,ArchivePath)
d file.Delete(File)

It's enough to write:

do ##class(%File).CopyFile(File,ArchivePath)
do ##class(%File).Delete(File)

Other things to consider:

  • You have file and File variables. That makes understanding what's going on more difficult than it should be.
  • Using full command names (set instead of s, do instead of d, etc.)

 

But, %File class has a method specifically for moving, so it's better to call it:

set Success = ##class(%File).Rename(File, ArchivePath, .RetCode)
write:(Success=$$$NO) "Rename failed with code: " _ RetCode

Thanks Guys,

Just to be aware, the client is running Cache 2008 so the third parameter can't be used for Rename or Delete, and I think i found the problem, where in my code I'm opening the file as a Stream:

set stream = ##class(%Stream.FileCharacter).%New()
 set stream.Filename = FILEPATH
 while 'stream.AtEnd 
 {
 set LINE = stream.ReadLine()
 If LN>0
 {
 ^UTLV("data",LN)=LINE
 ^UTLV("data")=LN
  }

and the reason why the delete didn't work is because the the file (Stream) is opened.

I tried to close or kill the stream using :stream.Flush(),  stream.%Delete(), stream.%KillExtent() or  CLOSE stream 

and didn't work and if I use stream.Clear() the file will be deleted all together, and I don't want to delete is at this stage, I only want to delete the file after all my processes in finish at the end of my last routine.

so how can i close the stream so that the Delete method can function at the end?

thanks Guys

This may look alarming, but all it's doing is destroying the local variable (you named it 'stream') that's referencing your stream object. And assuming it's the only variable referencing the object, the object will be destroyed, thus closing the file it had opened.

Thanks KILL worked, I should've tried that , but that ready strange that stream.Clear() actually delete the physical file!!?

Thanks

Clear() removes all content. The easiest way to CLEAR a file is to remove it. in any file system.

by "content" you mean the physical file?, because to me, the content of a stream not the physical file.

Thanks Robert.

for a file stream it's a physical file, for a global stream it's a global or part of it.
if you clear it you either delete the file or the global (or its part )that holds the stream.
stream in Caché describes a sequence of persistent bytes on disk that you work on with dedicated common methods.

this must not be mixed up with a stream of characters on a network connection. if you miss a character there it's gone. 

Thanks Robert, and that's my understanding as well because in our software we actually stream PDF file in the database and also can restore them as a physical pdf files to read them, but what i though is that the streamed files in the database is a COPY of the physical file that we used first and clearing the stream is only clearing the streamed bytes in the database not the actual original file, but now you mentioned that Clearing the stream will also delete the original physical file, I'll take than into account so that we don't loose any files just in case :)

Thanks Robert. 

there is a method CopyFromStream that really moves the content to a new Stream
So your PDF moves from physical file to a Global stream that you work up and down as a "private" copy.
Without touching the original anymore. This might be a useful option.