No announcement yet.

Copy, Rename function?

  • Filter
  • Time
  • Show
Clear All
new posts

  • Copy, Rename function?

    There are times I simply want to copy one filename to another
    filename. I can do this via the SHELL command, but it causes
    screen flickering. Is there another way to accomplish the
    following with no screen flickering please?

    (yes, I know the following is not valid basic code, but it
    seemed the easiest way to express the question)

    If "D:\Resrch\MainTemp.dat" exists then :' delete file if exists
    End if
    If "D:\Resrch\MainOut.dat" exists then :' RENAME file if exists
    RENAME D:\Resrch\MAINOUT.DAT D:\Resrch\MainTemp.dat
    End if

    {{{{I would not object if the second part did this instead}}}}
    If "D:\Resrch\MainOut.dat" exists then :' RENAME file if exists
    Copy D:\Resrch\MainOut.dat D:\RESRCH\MAINTEMP.DAT
    KILL D:\Resrch\MainOut.dat
    End if

    Right now, I \have the above in a batch file called "Fixit.bat"
    and I use the SHELL command to shell & execute to that batch
    file, but the the screen flickers a lot.

    Can the flickering be stopped? It makes for horrible monitor
    watching. Yes, I have tried using ECHO OFF in the batch file
    to prevent characters / information from being echoed to the
    screen, but flickering still remains.

    Thank you.



    [This message has been edited by Robert E. Carneal (edited December 29, 2006).]

  • #2
    copy oldfile==>newfile

    FUNCTION CopyUnderProgramControl (newfile as string, oldfile as string) AS INTEGER
     IF DIR$(newFile)> ""  THEN
       KILL NewFile
     END IF
     hNewFile  = FREEFILE
     OPEN newFile for binary as hNewFile
     hOldFile = FREEFILE
     OPEN oldFile for binary  AS hOldFile
     BlockSize =   8192 
     nBlock    =   LOF(hOldFile) \ BlockSize
     Oddbytes  =   LOF (hOldFile) MOD BlockSize
     FOR block=1 to nBlock
       GET$ hOldFile,BlockSize, S
       PUT$ hNewfile,S
     NEXT block
     IF OddBytes THEN
       GET$ hOldFile,oddbytes, s
       PUT$ hNewFile, S
     END IF
     CLOSE hOldFile, hNewFile

    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]


    • #3
      Doah.. didn't see the "DOS" thing.

      don at DASoftVSS dot com
      "There are 2 Ss in Christmas, Cratchet, and they're both dollar signs!"

      [This message has been edited by Don Schullian (edited December 29, 2006).]


      • #4
        I knew it was MS-DOS when I replied, Don. .. but in all honesty I had to really reach back in Ye Olde Graye Matter to remember how to do this working within the memory limitations.

        But FWIW, posted code will work under Windows, too.

        Michael Mattias
        Tal Systems Inc. (retired)
        Racine WI USA
        [email protected]


        • #5
          And just to clarify...

          The "flicker" is, as you suspected, a byproduct of using SHELL ("batchfile.bat"). Because SHELL causes another process to start (assuming you are running on a Windows O/S) and you have no real control over the task-switching, it's just one more process which has to share processor time with everything else.

          Even when I wrote a lot of MS-DOS code (and I did write a lot of it), I avoided SHELL or EXEC (RUN?) if there was any way to get the job done from within my program. That was mostly because I'm a control freak, but a nice byproduct when I got to Windows was the reduction of 'flicker' and/or "flashing console" when a particular thing needed doing.

          Michael Mattias
          Tal Systems Inc. (retired)
          Racine WI USA
          [email protected]


          • #6
            'PB/CC or PBWin can execute commands for you
              #COMPILE EXE   'doit.bas
              #DIM ALL
              %Display = 1
              #IF %DEF(%PB_CC32)
                #IF %Display = 0
                  #CONSOLE OFF
              DECLARE SUB Display(s AS STRING)
              FUNCTION PBMAIN () AS LONG
                LOCAL buffer AS STRING, TheCommand AS STRING, p AS LONG
                buffer = SPACE$(80)
                OPEN "c:\commands.tmp" FOR BINARY SHARED AS #1
                'Buffer = "NAME,C:\KEEP\UNIQUE.EXE,C:\UNIQUE.EXE"    'DOS program writes command,  END = END
                DO UNTIL LEFT$(BUFFER,3)  = "END"
                  GET #1,1, Buffer
                  Display "Buffer: " + buffer
                  p = PARSECOUNT(buffer)
                  Display "Parameters:" + STR$(p)
                  IF p > 1 THEN
                     TheCommand = PARSE$(Buffer,1)
                     Display "TheCommand: " + TheCommand$
                     SELECT CASE TheCommand
                        CASE "NAME"
                            IF p <> 3 THEN
                              Display STR$(p) + " Parameters must = 3"
                              NAME PARSE$(buffer,2) AS PARSE$(buffer,3)
                              Display "Status of NAME" + STR$(ERRCLEAR)
                            END IF
                     END SELECT
                    SEEK #1,1  'clear buffer
                    SETEOF #1
                  END IF
                  SLEEP 5000  'run in background, update every 5 seconds
            END FUNCTION
            SUB Display(s AS STRING)
              #IF %Display
                 #IF %DEF(%PB_CC32)
                  PRINT s
            END SUB
            'Dos program
            'DEFINT A-Z
            'OPEN "c:\commands.tmp" FOR OUTPUT AS #1
            'Buffer$ = "NAME,C:\KEEP\UNIQUE.EXE,C:\UNIQUE.EXE"
            'Buffer$ = "END"
            'PRINT #1, Buffer$
            'CLOSE #1

            How long is an idea? Write it down.


            • #7
              from the PBDOS reference manual:

              NAME statement
              NAME renames a file or a directory (like the DOS REN command).

              NAME filespec1 AS filespec2

              Filespec1 and filespec2 are string expressions conforming to
              DOS path and file naming conventions. NAME gives the file or
              directory represented by filespec1 the name filespec2. Since
              filespec2 can contain a path name, it's possible to move a
              file(but not a directory) from one directory to another. You
              can't move from one disk to another.

              If filespec1 does not exist, run time error 53 ("File not
              found") occurs. If filespec2 already exists, run time error
              75("Path/File access error") occurs.

              The DOS REN command can only rename files, not directories.

              INPUT "Enter the name of the file to rename: ".OldName$
              INPUT "Enter the new name of the file: ",NewName$
              PRINT OldName$,NewName$
              NAME OldName$ AS NewName$

              Is that anything like what you were looking for?



              [This message has been edited by Buck Huffman (edited December 30, 2006).]


              • #8
                Maybe, I will try it tomorrow.

                Thank you.




                • #9
                  An exercise with Mr. Michael's code.
                  $COMPILE EXE
                  $DIM ALL
                  %False = 0
                  %True  = NOT %False
                  %FullErrorMessages = %True
                  DECLARE SUB PbMain()
                  SUB PbMain()
                    DIM Msg AS LOCAL STRING
                    DIM StrLenght AS LOCAL INTEGER
                    DIM Position AS LOCAL INTEGER
                    Msg = CopyFile("c:\autoexec.bat", "c:\ae.bat")
                    IF Msg = "" THEN
                      PRINT "All done!"
                      StrLenght = LEN(Msg)
                      Position  = INSTR(Msg," - ")-1
                      PRINT UCASE$(LEFT$(Msg,Position))
                      IF ISTRUE(%FullErrorMessages) THEN PRINT RIGHT$(Msg,StrLenght-Position-3)
                    END IF
                  END SUB
                  ' Copy oldfile ==> newfile  // from Michael Mattias //
                  FUNCTION CopyFile(OldFile AS STRING, NewFile AS STRING) AS STRING
                      DIM hNewFile AS LOCAL INTEGER
                      DIM hOldFile AS LOCAL INTEGER
                      DIM BlockSize AS LOCAL INTEGER
                      DIM Block AS LOCAL INTEGER
                      DIM TotalBlock AS LOCAL INTEGER
                      DIM OddBytes AS LOCAL INTEGER
                      DIM S AS LOCAL STRING
                      DIM ErrorType AS LOCAL STRING
                      ON LOCAL ERROR GOTO ExitWithErrorMessage
                      IF DIR$(NewFile) <> ""  THEN
                          KILL NewFile
                      END IF
                      BlockSize  = 8192
                      hNewFile = FREEFILE
                      OPEN NewFile FOR BINARY AS hNewFile
                      hOldFile = FREEFILE
                      OPEN OldFile FOR BINARY AS hOldFile
                      TotalBlock = LOF(hOldFile)  \  BlockSize
                      Oddbytes   = LOF(hOldFile) MOD BlockSize
                      FOR Block = 1 TO TotalBlock
                          GET$ hOldFile, BlockSize, S
                          PUT$ hNewfile, S
                      NEXT Block
                      IF OddBytes THEN
                          GET$ hOldFile, OddBytes, S
                          PUT$ hNewFile, S
                      END IF
                      CLOSE hOldFile, hNewFile
                      FUNCTION = ""
                      EXIT FUNCTION
                      SELECT CASE ERR
                          CASE 53: ErrorType = "File not found - The file name specified could not be found on the indicated drive"
                          CASE 55: ErrorType = "File already open - You attempted to open a file that was already open, or you tried to delete an open file"
                          CASE 57: ErrorType = "Device I/O error - A serious hardware problem occurred when trying to carry out some command."
                          CASE 61: ErrorType = "Disk full - There isn't enough free space on the indicated or default disk to carry out a file operation.  " & _
                                               "Create some more free disk space and retry your program."
                          CASE 62: ErrorType = "Input past end - You tried to read more data from a file than it had to read.  Use the EOF function to avoid " & _
                                               "this problem.  This error can also be caused by trying to read from a sequential file opened for output or append."
                          CASE 64: ErrorType = "Bad file name - The file name specified in a FILES, KILL, or NAME statement contains invalid characters."
                          CASE 67: ErrorType = "Too many files - Caused either by trying to create too many files in a drive's root directory, or by an invalid " & _
                                               "file name that affects the performance of the DOS Create File system call."
                          CASE 70: ErrorType = "Permission denied - You tried to kill/write to a write-protected file/disk"
                          CASE 71: ErrorType = "Disk not ready - The door of a floppy disk drive is open or there is no disk in the drive."
                          CASE 72: ErrorType = "Disk media error - The controller board of a floppy or hard disk indicates a hard media error in one or more sectors."
                          CASE 75: ErrorType = "Path/file access error - During a command capable of specifying a path name, you used the  path inappropriately; " & _
                                               "trying to open a sub-directory or to delete a directory in-use."
                          CASE 76: ErrorType = "Path not found - The path you specified during an OPEN can't be found."
                      CASE ELSE
                          ErrorType = "Error" & STR$(ERR) & "at address" & STR$(ERADR)
                      END SELECT
                      FUNCTION = ErrorType
                      RESUME ExitWithFunctionError    'only to satisfy ON ERROR syntax
                  END FUNCTION
                  Arthur Gomide
                  Como diria nosso profeta da bola, Dadá Maravilha: "Para toda Problemática existe uma Solucionática!"

                  [This message has been edited by Arthur Gomide (edited December 31, 2006).]
                  "The trouble with quotes on the Internet is that you can never know if they are genuine." - Abraham Lincoln.


                  • #10
                    CASE 53: ErrorType = "File not found ...
                    CASE 55: ErrorType = "File already open.. 
                    Using Windows compilers,the ERROR$ intrinsic function would be a really handy thing..

                    [This message has been edited by Michael Mattias (edited December 31, 2006).]
                    Michael Mattias
                    Tal Systems Inc. (retired)
                    Racine WI USA
                    [email protected]