No announcement yet.

KILL statement question

  • Filter
  • Time
  • Show
Clear All
new posts

  • KILL statement question

    KILL "c:ver8.txt"

    This line placed at the last line (before END FUNCTION) does not delete this file from the directory. What am I doing wrong?


  • #2
    It all depends on where the file is located.

    The statement, as posted, will delete the file you are currently in or, if you are in the "D:" drive, the "C:" partition currenty accessed by your program.

    If the file is in the root directory, add a backslash, i.e., "KILL c:\ver8.txt".
    There are no atheists in a fox hole or the morning of a math test.
    If my flag offends you, I'll help you pack.


    • #3
      Missing backslash?


      Added: Looks like Mel beat me to it.

      -- Eric
      "Not my circus, not my monkeys."


      • #4
        You could have figured out what's wrong by querying ERR after KILL. You should get a 53 file not found error, which surely would have pointed you in the right direction.
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]


        • #5


          Thanks. The real statement is:

          KILL "c:\pclink4\nb_ver8.txt"

          so I had the slash. I did the error trap and it returned error 70

          "You tried to write to a write-protected disk. This error can also be generated as a result of network permission errors, such as accessing a locked file, or a locked record. It can also occur when attempting to open a subdirectory as a file."

          Within the program I create and write to this file:
          OPEN "c:\pclink4\nb_ver8.txt" FOR BINARY LOCK SHARED AS 9

          and specifically close it
          CLOSE #9

          I later open it again:

          OPEN "c:\pclink4\nb_ver8.txt" FOR INPUT LOCK SHARED AS 4

          and specifically close it
          CLOSE #4

          but still get error 70! comments?


          • #6
            If your program crashed while the file was still locked, it may not have been closed.
            Either reboot your machine to clear the lock, or use something like "unlocker" to force the release of the file handle.
            Gary Barnes
            The Control Key

            If you are not part of the solution
            then you are either a gas, solid, plasma or some other form of matter.


            • #7
              If CLOSE fails, that should return an ERR value, too. Worth checking anyway.


              CLOSE is a "when I get around to it" operation with Windows; so the CLOSE may not have been done by the time the KILL was issued. Windows is "supposed" to handle this but maybe you need to code a delay/retry loop on the KILL, giving up after some number of tries and reporting inability to KILL the file. Heck, that's worth a try as well.

              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]


              • #8
                I put
                SLEEP 5000
                ahead of the KILL statement.

                and all worked. Thanks for your imput!



                • #9
                  Oh, my!

                  Say it ain't so!

                  Someone actually employed the "try it" approach!!!

                  Will wonders never cease?
                  Michael Mattias
                  Tal Systems (retired)
                  Port Washington WI USA
                  [email protected]


                  • #10
                    well, at least some of the time...


                    • #11
                      Historical Point:

                      "Lazy Writes" (all file activity for that matter) have been a characteristic of the NT based OS's since 3.1 (the first version) and was always a characteristic of VMS, the "mother" of NT.


                      • #12
                        Written as a SUB to make checking result more likely and has optional timeout

                        SUB Killer(sFileName AS STRING, result AS LONG, OPTIONAL BYVAL Time_Out AS LONG)
                          LOCAL PausePerLoopInMilliseconds AS LONG
                          LOCAL TimeWaited                 AS LONG
                          PausePerLoopInMilliseconds = 100
                            KILL sFileName
                            IF ERR = 0 OR ERR = 53 THEN EXIT DO
                            IF TimeWaited => Time_Out THEN EXIT DO
                            SLEEP PausePerLoopInMilliseconds
                            TimeWaited = TimeWaited + PausePerLoopInMilliseconds
                          result = ERR
                        END SUB


                        • #13
                          Create or use a file which you want deleted when you are done with it.

                          Not tested, not even compiled.

                          May be worth playing with for some applications... and can be played around with, a lot.


                          ' ---------------------------------------------------
                          '     OPEN A FILE Which will be deleted on close 
                          '       when closed with TempFile_Close
                          ' ----------------------------------------------------
                          ' returns: hFile, a PB handle ready for use in specified mode; %NULL on failure
                          ' modes: "BINARY","RANDOM", "INPUT" 
                          ' opening for Output makes no sense since file will be deleted as soon as it's closed. 
                          FUNCTION Tempfile_Open ( szFilename AS ASCIIZ, OpenMode AS STRING, OPTIONAL RecLen AS LONG) AS LONG
                              LOCAL dwAccess as DWORD, dwShare AS DWROD, lpSecurity as DWORD, dwCreate AS DWORD, hTemplate AS DWORD
                              LOCAL dwFlags AS DWORD, hSYS AS DWORD
                              dwFlags   = %NULL              ' initialize 
                              SELECT CASE lcase$(OpenMode)
                                CASE "random"
                                     dwAccess = %GENERIC_READ OR %GENERIC_WRITE
                                     dwFlags  = %FILE_FLAG_RANDOM_ACCESS
                                     dwCreate  = %OPEN_ALWAYS
                                CASE "binary" 
                                     dwAccess = %GENERIC_READ OR %GENERIC_WRITE
                                     dwCreate  = %OPEN_ALWAYS
                                CASE "input" 
                                     dwAccess =  %GENERIC_READ
                                     dwCreate  = %OPEN_EXISTING
                              END SELECT
                              dwShare       = %NULL    ' temp/work file, no sharing needed. 
                              lpSecurity    = %NULL    ' or VARPTR (Security_Attributes var) 
                              dwFlags       = dwFlags  OR %FILE_FLAG_DELETE_ON_CLOSE   ' [b]so it's deleted on close[/b]
                              hTemplate     = %NULL    ' because it has to be %NULL
                              hSys = CreateFile (szFilename, dwAccess, dwShare,BYVAL lpSecurity, dwCreate, dwFlags, hTemplate)
                              IF hSys <> %INVALID_HANDLE_VALUE THEN   ' open Succeeded
                                 hFile = FREEFILE                     ' get PB handle and open 
                                 SELECT CASE lcase$ (OPenMOde) 
                                    CASE "binary"
                                      OPEN HANDLE hSys  FOR BINARY AS hFile 
                                    CASE "random"
                                       OPEN HANDLE hSys FOR RANDOM AS hFile LEN=RecLen
                                    CASE "input"
                                       OPEN HANDLE hSys FOR INPUT  AS hFile 
                                 END SELECT
                               ' Oops open failed!
                                 hfile = %NULL
                              END IF 
                              FUNCTION = hFile 
                          END FUNCTION
                          FUNCTION TempFile_Close (BYVAL hFile AS LONG) AS LONG
                            LOCAL hSys AS LONG
                            hSys  =     FILEATTR (hFile, 2)   ' get system handle for this PB handle
                            CLOSE       hFile                 ' close PB handle
                            CloseHandle hSys                  ' close underlying system handle, which will delete the file
                          END FUNCTION
                          FUNCTION whatever...
                           LOCAL szPrefix AS ASCIIZ * 4              ' only three characters max are ever used
                           LOCAL szTempFile AS ASCIIZ * %MAX_PATH
                             szPrefix  = "~mm"                            ' see support function below
                             CALL        GetTempFileName(szPrefix) TO szTempFile
                             OpenMode   = "BINARY" 
                             hFile      = TempFile_OPen (szTempFile, openmode)
                             ' ----------------------------------------------------------
                             '   Do stuff with the temp file using the supported verbs 
                             '  for BINARY opens: SEEK, PUT, PUT$, GET, GET$
                             ' ---------------------------------------------------------
                             TempFile_Close  hFile    ' done with workfile, close with auto-delete
                              '' ----- FOR SEQUENTIAL WORKFILE -------
                              ' Create a file ready to write to 
                                szPrefix = "~sw"
                                CALL        GetTempFileName (szPrefix) TO szTempFile
                                ' -----------------------------------------
                                ' cannot open for autodelete, since we won't be able to get our data back!
                                ' so use standard PB open:
                                hFile = FREEFILE
                                OPEN szTempFile FOR OUTPUT AS hFIle
                                ' write work records to file using supported verbs 
                                ' for OUTPUT opens: PRINT#, WRITE#
                                ' ------------------------------------
                                CLOSE hFile    ' will not be deleted 
                                ' Read back the file, opening for auto-delete
                                OpenMode = "INPUT" 
                                hFile    = TempFile_open (szTempFile, openMode) 
                                ' read back the data using supported verbs 
                                ' for INPUT opens: LINE INPUT#, INPUT#  
                                ' -----------------------------------------------
                                TempFile_Close hFile   ' close with auto-delete 
                          END FUNCTION
                          ' ------------------------------------------------------------------------
                          '   Get a uniquely-named file in the user's designated temporary directory
                          ' ------------------------------------------------------------------------
                          FUNCTION GetTempFileName (szPrefix AS ASCIIZ) AS STRING
                             ' returns: Name of temp file to use (fully-qualified); in user's "temp" directory 
                             '        other = System error number
                             LOCAL szTempPath   AS ASCIIZ * %MAX_PATH, sTempFile AS STRING
                             LOCAL iStat AS LONG
                             sTempFile = "" default 
                           ' get user's designated temp folder:   
                             iStat            =  GetTempPath(SIZEOF(szTempPath), szTempPath) 
                             IF ISTRUE iStat THEN                            ' GetTempPathSucceeded
                                 sTempFile   = STRING$(0, %MAX_PATH)         ' create buffer for name 
                                 ' get a unique file name in that folder
                                 iStat = GetTempFileName (szTempPath, szPrefix, 0&, BYVAL STRPTR(sTempFile) 
                                 IF ISTRUE iStat THEN                         ' succeeded
                                   FUNCTION = RTRIM$(sTempFile, $NULL)         ' remove excess nulls from string
                                 ELSE   ' GetTempFilename failed
                                    sTempFile = ""                              ' oops, reset!
                                 END IF
                             END IF 
                             FUNCTION = sTempFile                         ' return name (null string on failure) 
                          END FUNCTION
                          Michael Mattias
                          Tal Systems (retired)
                          Port Washington WI USA
                          [email protected]