Question
· Feb 26, 2018

Copy Files

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

Discussion (16)2
Log in or sign up to continue

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

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. 

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