Guidance
指路人
g.yi.org
Software / Reginald / Examples / backup.rex

Register 
新用户注册
Search 搜索
首页 
Home Home
Software
Upload

  
/* This script used to backup files whose names are
 * listed in a text file upon each drive.
 *
 * The format of the text file is that each filename
 * is contained upon a separate line (unquoted). The
 * drive is not included on the file/directory name.
 * If the name is a directory, and you wish to back
 * up all sub-directories inside of it as well, then
 * end the name with a backslash.
 *
 * Comment lines may be placed in the text file by
 * putting a semi-colon as the first character in
 * that line.
 *
 * We assume that the backup file is named 'backup.txt'
 * and is located in the root of the drive.
 *
 * For example, a typical backup file may look like this:

; This is my backup file for C drive

;Backup WINDOWS directory
WINDOWS\*.*

;Backup WINDOWS\SYSTEM directory
WINDOWS\SYSTEM\*.*

;Backup autoexec.bat
autoexec.bat

;Backup config.sys
config.sys

 */

again:

/* Use Reginald's DRIVEMAP() built-in to get a list
 * of the fixed (hard) drives.
 */
listing = DRIVEMAP(, 'FIXED')

/* Present the list of drives and let him choose which
 * to backup.
 */
SAY 'Which drive do you wish to backup?'
i = 1
DO UNTIL listing == ""
   PARSE VAR listing drives.i listing
   SAY i '=' drives.i
   i = i + 1
END
SAY i '= All drives'
SAY i + 1 '= Quit'
PULL answer

/* Does he want to quit? */
IF DATATYPE(answer, 'W') == 0 | answer > i THEN RETURN

/* Get which device to backup to. He should enter a
 * name that ends with a backslash, for example,
 * C:\My Backup Dir\
 */
SAY 'Enter backup directory (ie, E:\) >'
PULL destination

/* Does he want to do all drives, or a particular drive? */
IF answer \= i THEN DO

   i = answer

   /* Loop once only */
   answer = 1

END

ELSE DO

	/* Loop for how many drives there are */
	answer = answer - 1

   i = answer

END

/* Do each drive */
DO answer

   /* Here's the name of our backup file */
   filename = drives.i || 'backup.txt'

   /* Open our backup file */
   IF STREAM(filename, 'C', 'OPEN READ') == 'READY:' THEN DO

      /* Trap any errors from LOADTEXT() */
      SIGNAL ON NOTREADY NAME badread

      /* Read all of the lines of our backup file
       * into the stem variable mybackup. mybackup.0
       * will indicate how many lines were loaded.
       * This text file contains a list of what files
       * to backup, one filename per line.
       */
      CALL LOADTEXT('mybackup.', filename, 'TLB')

      /* Process all of the lines */
      DO p = 1 TO mybackup.0

         /* Is this line a comment? (ie, the first
          * character is a semi-colon.
          */
         IF LEFT(mybackup.p, 1) \== ';' THEN DO

            /* It's not a comment, therefore, a filename
             * to backup.
             */
            SAY 'Copying "' || drives.i || mybackup.p || '" to "' || destination || mybackup.p || '"'

            /* Make sure that backup directory exists */
            ERROR = DIR(destination || mybackup.p)
            IF ERROR \== "" THEN SIGNAL copyerr

            /* Is this a directory we're backing up, and we want all
             * sub-directories backed up inside of it too? If so, it
             * ends with a backslash.
             */
            IF RIGHT(mybackup.p, 1) == '\' THEN

               ERROR = copyfolder(drives.i || mybackup.p, destination || mybackup.p, 1)

            ELSE

               /* Copy the file (or if name contains wildcards, all matching files) */
               ERROR = COPYFILE(drives.i || mybackup.p, destination || mybackup.p, 'R')

            IF ERROR \== "" THEN DO
copyerror:     SAY ERROR
               SIGNAL done
            END

         END /* test for a comment line */

      END /* parsing all lines */

      /* Now close the backup file */
      CALL STREAM(filename, 'C', 'CLOSE')

   END /* opening backup text file */

   /* An error opening the backup file */
   ELSE SAY STREAM(filename, 'D')

   i = i - 1

END /* backup of each drive */

done:
/* Alert user that backup is done */
CALL BEEP(1000, 500)

/* See if he wants to backup another drive */
SIGNAL again

badread:
   CALL CONDITION('M')
   CALL STREAM(filename, 'C', 'CLOSE')
   SIGNAL done   


/* Copies the contents of a folder and all sub-folders inside it */
copyfolder: PROCEDURE

   /* Copy all files in this folder to destination folder */
   ERROR = COPYFILE(ARG(1), ARG(2), 'R')
   IF ERROR \== "" THEN RETURN ERROR

   /* DO UNTIL no more sub-folders */
   DO UNTIL ERROR \== ""

      /* Get the name of the next sub-folder inside of this folder */
      ERROR = MATCHNAME(ARG(3), 'name', ARG(1), 'D')

      /* No error? */
      IF ERROR == "" THEN DO

         /* Make sure that the sub-folder exists in the destination */
         ERROR = DIR(ARG(2) || '\' || name)
         IF ERROR \== "" THEN DO
badcopyfolder:
             CALL MATCHNAME()
             RETURN ERROR
         END

         /* Copy all files in this folder to destination folder */
         ERROR = copyfolder(ARG(1) || '\' || NAME, ARG(2) || '\' || NAME, ARG(3) + 1)
         IF ERROR \== "" THEN SIGNAL badcopyfolder

      END

   END

   IF ERROR == 'DONE' THEN ERROR = ""

   /* Return an empty string if success, or an error message */
   RETURN ERROR
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Sun 2017-9-24  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0 Last modified:2010-07-16 20:46:06