Announcement

Collapse
No announcement yet.

Copy File already in use?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Copy File already in use?

    I am thinking the answer is no, but the back of my mind says you can if done right.

    What I am wondering is if another process has a file open. (For example Notepad, but for real purposes a database) That I would like to open and make a copy of (even if its in use, or in the midst of being updated)

    Am I limited to how the other process opened the file? or would I have to open it first, and watch for changes to signal me to make this duplicate copy? or some other thing I could do that periodically I make a duplicate copy?

    (Oh and for MCM.....this may be a perfect example for me to use your "Waitable Timer Object" as part of the code I had not found a use to investigate this till now)

    Reason I ask this is I am now running into problems with a program that my own prejudice I have long fought against using, but now its being used anyways, and twice in 3 days have resorted to the nightly backup to restore the database (minus all the hours since last backup), so until I can learn enough about databases I could "stop-gap" by making a backup each hour irregardless the current state of the file in use?
    Engineer's Motto: If it aint broke take it apart and fix it

    "If at 1st you don't succeed... call it version 1.0"

    "Half of Programming is coding"....."The other 90% is DEBUGGING"

    "Document my code????" .... "WHYYY??? do you think they call it CODE? "

  • #2
    Hi Cliff,

    Fwiw I do that all the time. No special precautions. Often it's because I keep forgetting to close the notepad file I'm reading while I run the program that is "reading" as well.

    I'm running Win2000 Pro (sp4) on my computer as I do this.

    Now, the database you're dealing with migh have other plans (restricting access or record locking). That's often a big problem with MS Exchange type apps - the database is "open" so it can't be backedup.

    That being said, it obviously can be done (given the number of high $$$ backup prgrams available).

    I don't know if it will help or not - but you might want to check out Terrabyte (http://www.terabyteunlimited.com). Among other things, they have an drive image product that ould backup windows while it was running. Long before Norton Ghost could. BUT it needed a dll called phylock to work properly.

    It was a free download and it may still be on their site somewhere (major re-design in January). But it's worth calling them.

    Fwiw - their software is GREAT!!!
    John,
    --------------------------------
    John Strasser
    Phone: 480 - 273 - 8798

    Comment


    • #3
      snapshot

      Snapshot drive image will back up an operating system. Don't know how it does it though.
      Warped by the rain, Driven by the snow...

      jimatluv2rescue.com

      Comment


      • #4
        Cliff,
        i was trying to copy a file firefox had in use and used filecopy and it worked but i found something MCM posted.
        he suggest the below and this worked for me on this particular file while it was open

        ACCESS READ LOCK SHARED


        'this failed OPEN Tfilename$ FOR BINARY ACCESS READ WRITE LOCK SHARED AS #1
        'this failed OPEN Tfilename$ FOR INPUT ACCESS READ WRITE LOCK SHARED AS #1
        'this worked, his suggestion
        OPEN Tfilename$ FOR BINARY ACCESS READ LOCK SHARED AS #1

        TEMP&=LOF(1)
        A$=SPACE$(TEMP&)
        GET #1,1,A$
        a$=TRIM$(a$)
        'PRINT A$
        PRINT ERR
        PRINT TEMP&
        CLOSE 1
        OPEN "C:\TEMP.PRN" FOR OUTPUT AS 1
        PRINT #1,TRIM$(A$)
        CLOSE 1
        'FILECOPY TFILENAME$, "C:\T.PRN"
        PRINT "done"
        p purvis

        Comment


        • #5
          Paul,
          I gave it a quick test, and got a 1KB file (obviously incorrect just by comparing file sizes) and then I ran again in Debugger, and got the Permission Denied error. So I am guessing I can not simply copy because the file is locked in use by the other process.

          Oh well it was just a stop-gap idea anyways.
          Engineer's Motto: If it aint broke take it apart and fix it

          "If at 1st you don't succeed... call it version 1.0"

          "Half of Programming is coding"....."The other 90% is DEBUGGING"

          "Document my code????" .... "WHYYY??? do you think they call it CODE? "

          Comment


          • #6
            Cliff
            Highly unlikely a data base would be locked, probably just as section. For many years have backed up data bases when network users forget to shut down the programs. If no activity on the program then very simple
            Open "filename" for BINARY ACCESS READ LOCK SHARED as #1
            GET$ #1, LOF(1), t$
            Open "Copyname" for BINARY LOCK SHARED as #2
            PUT$ #2, t$
            CLOSE
            If the data base is in use then you need to GET and PUT it in blocks, usually 2 or 8K blocks if it is a Micrsoft data base, keeping a list of any locked blocks which you can then go back and retry. Of course in this case there will most likely be some inconsistency in the copy so its usefullness will depend on what repair programs you have for the data base. Data bases like MS SQL keep extensive transaction logs for doing incremental backups.
            Also be wary of using any string functions in the processing ie TRIM$ or PRINT$ as there will probaly be many CHR$(0)s in the string.
            John

            Comment


            • #7
              FWIW, Notepad does NOT hold the file open.. it opens and closes 'as required'. (IMNSHO, excellent programming practice, but that's not important right now).

              If you want do your own file opens, you might try opening exclusive (ACCESS READ LOCK READ WRITE) first; if that fails, then attempt a shared open (ACCESS READ LOCK SHARED). I'm not totally sure if it still works this way anymore, but it used to be the two "openers" had to be using compatible SHARE modes. Of course, it the "primary" application is using the file in exclusive mode, you are SOL.

              Or, you could look at using the ShFileOperation Windows API function to perform the copy, letting Windows worry about share-compatibility (and other things, eg buffer sizes).

              MCM
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                Thanks Mike. Who'da thunk that as I play with my little toy programs that I was doing it right (in terms of opening/reading/closing) . And that (M$) was doing it that way. I always wondered *why* it worked.
                John,
                --------------------------------
                John Strasser
                Phone: 480 - 273 - 8798

                Comment


                • #9
                  FWIW, Notepad does NOT hold the file open.. it opens and closes 'as required'.
                  Yep, as seen by opening 2 copies of the same notepad file, make changes in each, and he who gets saved last becomes the new file. (So you could overwrite changes you made in the other copy, but thats for another story)

                  Unfortunately I figured out that I am SOL because, I made a copy when no one had the file open, and then used that copy to test with only me being the one with it open, and running test code on it. Permission denied error, so it must be locked by the program that opened it first.

                  Oh welllllll.....it was worth a shot.
                  Engineer's Motto: If it aint broke take it apart and fix it

                  "If at 1st you don't succeed... call it version 1.0"

                  "Half of Programming is coding"....."The other 90% is DEBUGGING"

                  "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                  Comment


                  • #10
                    Any file which is open in an output compatible mode is useless to another process anyway, because the directory is not updated until the first process closes the file.

                    The directory "might" be updated by FLUSH (or whatever the first process' command is for same), but it's not worth my time to test it, especially since the first process may not even be doing it.

                    This is an example of why I believe trying to cooperate with non-cooperating applications is a stone loser.
                    Michael Mattias
                    Tal Systems (retired)
                    Port Washington WI USA
                    [email protected]
                    http://www.talsystems.com

                    Comment


                    • #11
                      when i would write with another compiler not pb, i would always create my file put a end of file marker in it, then close the file, then reopen the file for output then close the file after writing to it.


                      this method reduced or almost totally removed the occurrence of unallocated file clusters and required a running of the chkdsk /f to remove them.
                      mostly this occurred with msdos and win98, i have not seen much of lately of any unallocated file clusters.

                      paul
                      p purvis

                      Comment

                      Working...
                      X